1.0.6 • Published 4 months ago

belin.db v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

belin.db

An NPM package for creating a local JSON-based database

Getting started

  • 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
  }
);

Database options

KeyValue typeDescriptionDefault valueOptional?
separatorStringThe separator symbol that you will use to split the data.Yes
belowZeroBooleanIf the numbers on the saved data can go below 0falseYes

All Methods

set(key, value)

Set the value of a key

get(key)

Get the value of a key

delete(key)

Delete a key

has(key)

Check if a key exists

all()

Get the JSON file

clear()

Delete all saved data

importFrom(path)

Import data from another JSON file

push(key, item)

Push an item into an array

pull(key, item)

Pull an item from an array

filter(key, predicate, thisArg?)

Returns the elements of an array that meet the condition specified in a callback function

find(key, predicate, thisArg?)

Returns the value of the first element in the array where predicate is true, and undefined otherwise

map(key, callbackfn, thisArg?)

Calls a defined callback function on each element of an array, and returns an array that contains the results

random(key)

Get an item of an array randomly

size(key)

Get the size of an array

some(key, predicate, thisArg?)

Determines whether the specified callback function returns true for any element of an array

sort(key, compareFn)

Sorts an array in place

add(key, number)

Add a number to a key value

remove(key, number)

Remove a number from a key value

Example

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 }}
1.0.6

4 months ago

1.0.5

7 months ago

1.0.4

9 months ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago