sagdb
v2.3.0
Published
Local json database.
Downloads
17
Readme
Sagdb
Sagdb is JSON database system.
Installation
$ npm install sagdb
Usage
//JavaScript version
const Sagdb = require("sagdb").default;
//TypeScript version
import Sagdb from "sagdb";
Default database name is "db".
Db name and folder name is must be String.
Database folder name is optional.
If you want readable database, set "minify" to false.
const db = new Sagdb({ name: "database", folder: "foldername", minify: true });
const db = new Sagdb({ name: "database", folder: "folder/database" });
const db = new Sagdb({ name: "database" });
Method's
set
- Set any value with key.
db.set("key", "apple"); // -> apple
Can't set Function.
function test() {}
db.set("key", test); // -> return Error
get
- Get any value with key.
db.set("key", "apple"); // -> apple
db.get("key"); // -> apple
update
- Update get a callback function, give old data and return must be a same type old data.
db.update("key", (old_data) => {
const new_data = old_data.toUpperCase();
return new_data;
});
delete
- Delete any key from database.
db.set("key", "apple"); // -> apple
db.delete("key"); // -> apple
db.get("key"); // -> undefined
add
Add number to number value.
Value must be a Number.
Default number is 1.
db.set("number", 1); // -> 1
db.get("number"); // -> 1
db.add("number", 3); // -> 4
db.get("number"); // -> 4
db.add("number"); // -> 5
db.get("number"); // 5
db.add("number", "asd"); // return false
Extra
U can set object in object.
db.set("key.value", "apple"); // -> apple
db.get("key"); // -> { value : "apple" }
Table
import Sagdb, { Table } from "sagdb";
const db = new Sagdb({ name: "database" });
const table = new Table(db, "table_name");
add
table.add(1); // -> {_id: "uuid", data: 1, createdAt: number, updatedAt: number}
find
table.find(callback | data_object); // -> {_id: "uuid", data: 1, createdAt: number, updatedAt: number} | undefined
findById
table.findById("uuid"); // -> {_id: "uuid", data: 1, createdAt: number, updatedAt: number} | undefined
update
table.update(callback | data_object, new_data, force?: boolean); // -> {_id: "uuid", data: 1, createdAt: number, updatedAt: number} | undefined
filter
table.filter(callback | data_object, new_data); // -> old_data: {_id: "uuid", data: 1, createdAt: number, updatedAt: number}[] | undefined
remove
table.update(callback, all?: boolean); // -> {_id: "uuid", data: 1, createdAt: number, updatedAt: number} | undefined
Listeneer's
On
table.on("event_name", callback()); // -> Listening.
Example
table.on("add", callback(new_data));
table.on("update", callback(old_data, new_data));
table.on("remove", callback(old_data));
License MIT