@salemkode/ranidb
v0.0.75
Published
> A simple "database" that use JSON file for Node.JS.
Downloads
1
Readme
Ranidb
A simple "database" that use JSON file for Node.JS.
Installation
Add ranidb
to your existing Node.js project.
with npm :
npm i @salemkode/ranidb
or with yarn:
yarn add @salemkode/ranidb
Used with node
const ranidb = require("ranidb");
API
- Ranidb()
- db.save()
- db.getAll()
- db.push()
- db.find()
- db.filter()
- db.findIndex()
- db.clear()
- db.updata()
- Options
Ranidb(path_db)
The main entry point for creating a new Ranidb
instance.
path_db
it must be string for path to file json, for example :"./db/data.json"
let db = new ranidb("./db/data.json");
save(data)
This function will delete all data and then rewrite it again.
data
it must be Array data.
let db = new ranidb("./db/data.json");
let arr = [
{ id: 1, name: "sami", age: 21 },
{ id: 2, name: "was", age: 19 },
{ id: 3, name: "ahmed", age: 17 },
];
db.save(arr);
getAll()
This function will return data in file json.
let db = new ranidb("./db/data.json");
db.getAll();
/* Output :
[
// all data in DB (File json)
]
*/
push(data)
This function will push data in file json.
data
it must be object data.
let db = new ranidb("./db/data.json");
let data = { name: "ahmed", age: 17 };
db.push(data);
/* Output :
{
{"_id": "R_Ddwtu1A", name:"ahmed", age:17}
}
*/
filter(data)
This function will data to need find.
data
it must be object data or function.
let db = new ranidb("./db/data.json");
db.filter((user) => user.age > 12);
/* Output :
[
{ _id: 'Wtl9v2x-Q', user: 'barney', age: 36, active: true },
{ _id: 'rqACSWx6kA', user: 'fred', age: 40, active: false }
]
*/
db.filter((user) => user.active == true);
/* Output :
[
{ _id: 'Wtl9v2x-Q', user: 'barney', age: 36, active: true },
{ _id: 'DniDQHMNpo', user: 'pebbles', age: 1, active: true },
{ _id: 'SflmjJaVN', user: 'barney', age: 36, active: true },
{ _id: 'mL7Np2hr_Z', user: 'pebbles', age: 1, active: true }
]
*/
find(data)
This function will data to need find.
data
it must be object data.
let db = new ranidb("./db/data.json");
db.find({ _id: "rqACSWx6kA" });
/* Output :
{"_id": "rqACSWx6kA","user": "fred","age": 40,"active": false}
*/
db.find({ _id: "rqACSWx6kA", age: 40 });
/* Output :
{"_id": "rqACSWx6kA","user": "fred","age": 40,"active": false}
*/
all Sub-functions work in find like filter ()
findIndex(data)
This function will find index for this data.
data
it must be object data.
let db = new ranidb("./db/data.json");
/*
[
{
"_id": "Wtl9v2x-Q",
"user": "barney",
"age": 36,
"active": true
},
{
"_id": "rqACSWx6kA",
"user": "fred",
"age": 40,
"active": false
},
{
"_id": "DniDQHMNpo",
"user": "pebbles",
"age": 1,
"active": true
}
]
*/
db.findIndex({ _id: "Wtl9v2x-Q" });
/* Output :
0
*/
db.findIndex({ _id: "rqACSWx6kA", age: 40 });
/* Output :
1
*/
// for not find
db.findIndex({ _id: "rqACSWx6kA", age: 42 });
/* Output :
-1
*/
clear()
This function will clear all data in ranidb.
let db = new ranidb("./db/data.json");
/* in file json :
[
{ _id: 'Wtl9v2x-Q', user: 'barney', age: 36, active: true },
{ _id: 'DniDQHMNpo', user: 'pebbles', age: 1, active: true },
{ _id: 'SflmjJaVN', user: 'barney', age: 36, active: true },
{ _id: 'mL7Np2hr_Z', user: 'pebbles', age: 1, active: true }
]
*/
db.clear();
/* Output :
[]
*/
updata(find, data)
This function will data to need find.
find
it must be opject.data
it must be object.
let db = new ranidb("./db/data.json");
/* in file json :
[
{ _id: 'Wtl9v2x-Q', user: 'barney', age: 36, active: true },
{ _id: 'DniDQHMNpo', user: 'pebbles', age: 1, active: true },
{ _id: 'SflmjJaVN', user: 'barney', age: 36, active: true },
{ _id: 'mL7Np2hr_Z', user: 'pebbles', age: 1, active: true }
]
*/
let data = {
_id: "Wtl9v2x-Q",
user: "barney",
age: 42,
active: true,
};
db.updata({ _id: "Wtl9v2x-Q" }, data);
/* Output :
{ _id: 'Wtl9v2x-Q', user: 'barney', age: 42, active: true }
*/
delete(find)
This function will data to need find.
find
it must be opject.
let db = new ranidb("./db/data.json");
/* in file json :
[
{ _id: 'Wtl9v2x-Q', user: 'barney', age: 36, active: true },
{ _id: 'DniDQHMNpo', user: 'pebbles', age: 1, active: true },
{ _id: 'SflmjJaVN', user: 'barney', age: 36, active: true },
{ _id: 'mL7Np2hr_Z', user: 'pebbles', age: 1, active: true }
]
*/
let data = {
_id: "Wtl9v2x-Q",
user: "barney",
age: 42,
active: true,
};
db.delete({ _id: "Wtl9v2x-Q" });
/* Output : true */
/* in file json :
[
{ _id: 'DniDQHMNpo', user: 'pebbles', age: 1, active: true },
{ _id: 'SflmjJaVN', user: 'barney', age: 36, active: true },
{ _id: 'mL7Np2hr_Z', user: 'pebbles', age: 1, active: true }
]
*/
Options
now we're going to make changes to some of the virtual things
idType
it must be string.- random (default)
let db = new ranidb("./db/data.json", { idType: "random" }); /* is like let db = new ranidb("./db/data.json") */ /* Output : { _id: 'Wtl9v2x-Q', ... } */
- empty
let db = new ranidb("./db/data.json", { idType: "empty" }); /* Output : { ... } */
- gradual
let db = new ranidb("./db/data.json", { idType: "gradual" }); /* Output : { _id: '1', ... }, { _id: '2', ... }, { _id: '3', ... }, { _id: '4', ... }, { _id: '5', ... } */