sags.db
v1.2.7
Published
Fast JSON database system.
Downloads
37
Readme
sagsDB
Sags.db is JSON database system.
Installation
npm install sags.db
Usage
const sagsdb = require("sags");
Default database name is "db".
Db name and folder name is must be String.
Database folder name is optional.
If u dont wanna minify set "minify" to false.
const db = new sagsdb({ name: "database", folder: "foldername", minify: true });
or
const db = new sagsdb({ name: "database", folder: "folder/database"});
or
const db = new sagsdb({ name: "database"});
You can create multi database.
const userdb = new sagsdb({ name: "userdb"});
const itemdb = new sagsdb({ name: "itemdb"});
COMMANDS
Set
Key must be a String or Number.
db.set("key", "apple"); // -> true
Can't set Function.
function test(){
};
db.set("key", test); // -> return Error
Get
Key must be a String or Number.
db.set("key", "apple"); // -> true
db.get("key"); // -> apple
Delete
Delete item from database.
db.set("key", "apple"); // -> true
db.delete("key"); // -> true
db.get("key"); // -> undefined
Type
Check type for this key.
db.set("key", "apple");
db.type("key"); // -> string
db.set("array", [1, 2, 3]);
db.type("array"); // -> array
db.set("object", { a: 1, b: 2 }); // -> true
db.type("object"); // -> object
db.set("number", 1); // -> true
db.type("number"); // -> number
Has
Check database for this key.
db.set("key", "apple"); // -> true
db.has("key"); // -> true
db.has("another"); // -> false
Push
Push item in Array.
db.push("array", "apple"); // -> true
db.get("array"); // -> ["apple"]
db.push("array","banana"); // -> true
db.get("array"); // -> ["apple", "banana"]
Unpush
Unpush item from Array.
db.push("array", "apple"); // -> true
db.get("array"); // -> ["apple"]
db.unpush("array", "apple"); // -> true
db.get("array"); // -> []
Add
Add number to number value.
Value must be a Number.
Default number is 1.
db.set("number", 1); // -> true
db.get("number"); // -> 1
db.add("number", 3); // -> true
db.get("number"); // -> 4
db.add("number"); // -> true
db.get("number"); // 5
db.add("number", "asd"); // return Error
Subtract
Subtract number to number value.
Value must be a Number.
Default number is 1.
db.set("number", 1); // -> true
db.get("number"); // -> 1
db.subtract("number", 3); // -> true
db.get("number"); // -> -2
db.subtract("number"); // -> true
db.get("number"); // -3
db.subtract("number", "asd"); // return Error
Head
Get value first item.
db.set("string", "str"); // -> true
db.head("string"); // -> "s"
db.set("number", 321); // -> true
db.head("number"); // -> 3
db.set("array", [1, 2, 3]); // -> true
db.head("array"); // -> 1
db.set("object", {a : 1, b: 2}); // -> true
db.head("object"); // -> 1
Tail
Get value last item.
db.set("string", "str"); // -> true
db.tail("string"); // -> "r"
db.set("number", 321); // -> true
db.tail("number"); // -> 1
db.set("array", [1, 2, 3]); // -> true
db.tail("array"); // -> 3
db.set("object", {a : 1, b: 2}); // -> true
db.tail("object"); // -> 2
Nth
Get value with index.
db.set("string", "str"); // -> true
db.nth("string", 1); // -> "t"
db.set("number", 321); // -> true
db.nth("number", 1); // -> 2
db.set("array", [1, 2, 3]); // -> true
db.nth("array", 1); // -> 2
db.set("object", {a : 1, b: 2}); // -> true
db.nth("object", 1); // -> 2
All
Return all Database object.
db.set("key", "apple");
db.set("number", 1);
db.all(); // -> {"key" : "apple", "number": 1}
DeleteAll
Delete all database.
db.set("key", "apple"); // -> true
db.get("key"); // -> apple
db.deleteAll(); // -> true
db.get("key"); // -> undefined
Dbsize
Get database size.
Number type is kilobyte (kb).
db.set("key", "apple"); // -> true
db.dbSize(); // -> 15
Extra
U can set object in object.
db.set("key.value","apple"); // -> true
db.get("key"); // => {key: { value : "apple" } }