1.10.2 • Published 3 years ago

ls-jsondb v1.10.2

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

JSON Database

What's new?

If you want to read documentation, move down to Documentation header. in 1.9 we introduce increment backups. Now your increment value won't be lost:

// simply use this function:
db.checkIncrement();

If it couldn't find increment.json file in your directory, it will create a new one, restoring old data. Backups are stored inside /ls-jsondb/backups/ folder. Also worth mentioning that code now requires to have increment.json file.

in 1.9 we also fixed some issues with increment. Now db.readIncrement() also returns the object of all increments, instead of just logging it.

Contact with me

You can add me in Discord: Squeens#6280Or join my test Discord server: https://discord.gg/mKYuStucmp

Documentation

Variables

Main module declaration:

const database = require("ls-jsondb");

Declare a single database:

const anyVarName = new database("path", { settings });

settings: you can leave this empty. parameters:

{
  usealerts: Boolean;
}

path is a String option to declare path to a folder with .json files aka tables.you can use multiple amount of databases by creating new paths:

// for example:
const first = new database("./first");
const second = new database("./second");

Functions

tip: there are tooltips for every parameter and argument if you hover on any function or constructor.

createTable(tablename)

tablename(String) is a .json file inside path you declared.

removeTable(tablename)

tablename(String) is a .json file inside path you declared.

write(tablename, key, value)

tablename(String) is a .json file inside path you declared.key(String) is a header of an Object.value(Any) is a value lol

{
  key: value;
}

read(tablename, key)

tablename(String) is a .json file inside path you declared.key(String) is a header of an Object.Returns Any

{
  key: value;
}

edit(tablename, key, value, subvalue?)

tablename(String) is a .json file inside path you declared.key(String) is a header of an Object.value(Any) is a value that you are editing inside of an Object.subvalue(Any)(not necessary) a value of parent value.UPDATED: if subvalue === null or !subvalue it changes whole key.

{
  key: value;
}

remove(tablename, key)

tablename(String) is a .json file inside path you declared.key(String) is a header of an Object.

{
  key: value = undefined;
}

check(tablename, key)

tablename(String) is a .json file inside path you declared.key(String) is a header of an Object.Returns Boolean

{
  key: value;
}

readIncrement()

returns stringified Object of all increment values in console.

editIncrement(tableName, value)

tableName(String) is a .json file inside path you declared (and also stored as a key for increment).value(Number) is a numberic value-ofset of increment.

generateToken(length, {parameters?})

length(Number) is a length of the token.parameters(Object)(not necessary).alphabet is a String value of all possible characters. If not stanted, uses "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" by default.notallowed is an Array 'upperCaseString', 'lowerCaseString', 'numbers, 'others?' of letters, that won't be used by the generator.UPDATED: "others" works only if parameter alphabet stated.Returns String

{
  alphabet: String,
  notallowed: Array
}

other parameters

"AUTO_INCREMENT"

Put this inside value parameter or inside mother {Object} For example:

database.write("test", "USER", { id: "AUTO_INCREMENT" });

and it will write it like that:

{
  "USER": {
    "id": 1
  }
}

increment value is written inside ./increment.json and writes like that:

{
  TABLENAME: VALUE; // Number
}

WARNING: can be used only once per table! WARNING: do not use AUTO_INCREMENT value in edit() function. WARNING: write() function creates a new version of the key so it always reroll AUTO_INCREMENT value.

STRICT MODE

This is a very complicated thing. Let's talk about it a little bit more. When using createTable() function, you can add {parameters} after tableName property. It automaticaly makes this table in strict mode: creates tableName-rules.json file. Example of usage:

db.createTable("example", { id: "AUTO_INCREMENT", roles: "ARRAY" });

It creates empty file example.json and example-rules.json with:

{
  "id": "AUTO_INCREMENT",
  "roles": "ARRAY"
}

When you write or edit something in this table, it uses strict parameters:

db.write("example", "user", {
  id: "AUTO_INCREMENT",
  roles: ["ADMIN", "MODER"],
});

If, for example, you make "roles" as a STRING, it will throw an error:

db.write("example", "user", {
  id: "AUTO_INCREMENT",
  roles: "ADMIN",
  //     ^^^^^^^ typeof ≠ array.
});

console:

Error: in rule-file "roles" is instance of ARRAY

that's it.

Examples

write()

Default mode:

db.write("users", db.generateToken(12), {
  id: "AUTO_INCREMENT",
  username: "user",
});

Strict mode:

// rules:
{
  id: "AUTO_INCREMENT",
  username: "STRING",
}

db.write("users", db.generateToken(12), {
  id: "AUTO_INCREMENT", // rule => returned true
  username: "user" // rule => returned true
});

edit()

Default mode:

db.edit("users", db.generateToken(12), {
  // do not edit AUTO_INCREMENT
  username: "lissa",
});

// if you need to add a new line, just paste it:
db.edit("users", db.generateToken(12), {
  description: "example description.",
});

Strict mode:

// rules:
{
  id: "AUTO_INCREMENT",
  username: "STRING",
}

db.edit("users", db.generateToken(12), {
  // do not edit AUTO_INCREMENT
  username: "lissa" // rule => returned true
});

// you can't add new lines in strict mode.

remove()

db.remove("users", "F3W9hk4klaHl");

check()

if (db.check("users", "F3W9hk4klaHl") == true) doSomething();

read()

const user = db.read("users", "F3W9hk4klaHl");

// for example returns:
F3W9hk4klaHl: {
  "id": 1,
  "username": "Lissa",
  "description": "Example description."
}

console.log(user.id) // returns 1
console.log(user.username) // returns "Lissa"
console.log(user.description) // returns "Example description."

generateToken()

const token = db.generateToken(12, {
  alphabet: "abcdefABCDEF123456.",
  notallowed: ["upperCaseString"],
});
// generates token using "abcdefABCDEF123456." characters but without "ABCDEF".

createTable()

Default mode:

db.createTable("example");

Strict mode:

db.createTable("example", {
  id: "AUTO_INCREMENT",
  username: "STRING",
});

removeTable()

db.removeTable("example");

editIncrement()

db.editIncrement("example", 1);

readIncrement()

db.readIncrement();
1.10.2

3 years ago

1.9.5

3 years ago

1.9.4

3 years ago

1.9.3

3 years ago

1.9.2

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

2.0.0

3 years ago

2.0.1-DEV

3 years ago

2.0.0-DEV

3 years ago

1.8.4

3 years ago

1.8.3

3 years ago

1.8.2

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.9

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.5

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago