1.1.0 • Published 4 years ago

@molo_7/db.json v1.1.0

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

Table Of Content


What's db.json

a light module to store data in easy way for beginners in JSON format, fast, simple and powerfull.


Features

  • Easy to use
  • Supports all datatypes < string | number | bigint | boolean | symbol | undefined | object | function | array >
  • Dot notation support
  • Key value based

Get Started

Installation

npm install db.json

Import json.db

const Database = require("db.json"); // Constructor
const db = new Database("test.json"); // create new json storage file

new Database(filename)

  • passing file name or file path is required
  • file format must be json !
  • If the file dosen't exist, it will create it

Multiple Files

easly create multiple storage files

const Database = require("db.json");
const users = new Database("users.json");
const guilds = new Database("guilds.json");

users.set("user_1", {
  id: 1,
  name: "Jhon Doe",
  ownGuilds: [],
});

let userOne = users.get("user_1");
// -> { id: 1, name: "Jhon Doe", ownGuilds: [] }

guilds.push("guilds", {
  name: "new Guild",
  owner: userOne,
});

guilds.fetchAll("guilds");
// -> { guilds: [ { name: "new Guild", owner: { id: 1, name: "Jhon Doe" } } ] }

users.push("user_1.ownGuilds", "new Guild");

Properties

.entries

returns the db entries length

Example

console.log(`total entries: ${db.entries}`);

.size

returns the file size in bytes

db.set("foo", "bar");

console.log(db.size); // 13 bytes

Methods

set(key, value)

Sets a key-value to db!

NameTypeRequiredDescription
KeyStringtrueKey
ValueAnytrueData

Example

// set any datatype !

db.set("userProfile", { username: "jhon doe", views: 10 });
db.set("userProfile.bigint", BigInt(9007199254740991));
db.set("userProfile.getViews", function getViews() {
  return this.views;
});

get(key)

returns the value of a key

NameTypeRequiredDescription
KeyStringtrueKey

To complete the previous example

let userProfile = db.get("userProfile");
// -> { username: 'jhon doe', views: 10, bigint: 9007199254740991n, getViews: [Function: anonymous] }

console.log(userProfile.getViews());
// -> 10

random(n)

returns 2d array with n number of random unique entries, by default returns 1 entry

NameTypeRequiredDefaultDescription
nNumberfalse1number of random entries

Eample

db.set("one", 1);
db.set("two", 2);

let randomEntries = db.random(2); // array with 2 unique random entry

console.log(randomEntries);
// -> [["one", 1], ["two", 2]]

console.log(
  "First Random Entry",
  "key: " + randomEntries[0][0],
  "value: " + randomEntries[0][1]
);

typeOf(key)

returns the type of key,

datatypes : < "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" >

NameTypeRequiredDescription
KeyStringtrueKey

Exmaple

db.set("myBigint", BigInt(9007199254740991));
db.set("mySymbol", Symbol("symboool"));
db.set("myFunction", (msg) => console.log(msg));

console.log(db.typeOf("bigint"));
// -> bigint

console.log(db.typeOf("mySymbol"));
// -> symbol

console.log(db.typeOf("myFunction"));
// -> function

has(key)

checks if the db has key, returns Boolean

NameTypeRequiredDescription
KeyStringtrueKey

Exmaple

db.set("foo", "bar");

console.log(db.has("foo"));
// -> true

push(key, value)

Pushs to Array, if typeof key wasn't array, automatically convert it to array and append the new value. and returns array length

NameTypeRequiredDescription
KeyStringtrueKey
ValueAnytrueData

Example

db.push("items", "new item");

console.log(db.get("items"));
// -> [ "new value" ]

db.set("myWepon", "Pistol");

console.log(db.get("myWepon"));
// -> Pistol

db.push("myWepon", "Riffle");

console.log(db.get("myWepon"));
// -> ["Pistol", "Riffle"]

Search keys with startsWith(str), endsWith(str)

search keys that starts with or ends with str, returns array with the matched keys otherwise returns empty array

NameTypeRequiredDescription
StrStringtruestring to search

Example

db.set("user_a", "jhon 1");
db.set("user_b", "jhon 2");
db.set("abba", "someText");

let keysStartsWithUser = db.startsWith("user");
let keysEndsWithA = db.endsWith("a");

console.log(keysStartsWithUser);
// -> ["user_a", "user_b"]

console.log(keysEndsWithA);
// -> ["user_a", "abba"]

remove(key) or delete(key)

removes key from db, returns Boolean

no difference between remove() and delete()

NameTypeRequiredDescription
KeyStringtrueKey

Exmaple

db.set("foo", "bar");

console.log(db.remove("foo"));
// -> true

console.log(db.remove("foo"));
// -> false

fetchAll()

returns everything from the db as a Javascript object

Exmaple

let allData = db.fetchAll();

console.log(allData);

clear()

clears everything in the db

Exmaple

db.clear();

console.log("Everything Has Been Cleared");