An NPM package for creating a local JSON-based database
- Install the package
npm install belin.db
- Import the installed package
const { Database } = require("belin.db");
- Init the database
const db = new Database(
"./database.json", // The file path for the JSON file to save the data
{
separator: ".", // The separator symbol that you will use to split the data
belowZero: false, // If the numbers on the saved data can go below 0
}
);
Key | Value type | Description | Default value | Optional? |
---|---|---|---|---|
separator | String | The separator symbol that you will use to split the data | . | Yes |
belowZero | Boolean | If the numbers on the saved data can go below 0 | false | Yes |
Set the value of a key
Get the value of a key
Delete a key
Check if a key exists
Get the JSON file
Delete all saved data
Import data from another JSON file
Push an item into an array
Pull an item from an array
Returns the elements of an array that meet the condition specified in a callback function
Returns the value of the first element in the array where predicate is true, and undefined otherwise
Calls a defined callback function on each element of an array, and returns an array that contains the results
Get an item of an array randomly
Get the size of an array
Determines whether the specified callback function returns true for any element of an array
Sorts an array in place
Add a number to a key value
Remove a number from a key value
const { Database } = require("belin.db");
const db = new Database("./database/test.json", {
separator: "_",
belowZero: true,
});
db.set("a_b_c", "value"); // { "a": { "b": { "c": "value" }}}
db.get("a"); // { "b": { "c": "value" }}
db.delete("a_b_c"); // { "a": { "b": {}}}
db.has("a_b_c"); // true
db.all(); // { "a": { "b": { "c": "value" }}}
db.clear(); // {}
db.importFrom("./file.json"); // {}
db.push("a_b", "item"); // { "a": { "b": ["item"] }}
db.pull("a_b", "item"); // { "a": { "b": [] }}
db.add("a_c", 4); // { "a": { "b": [] }, { "c": 4 }}
db.remove("a_c", 2); // { "a": { "b": [] }, { "c": 2 }}