ranidb
v0.0.72
Published
> A simple "database" that use JSON file for Node.JS.
Downloads
5
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 ranidb
or with yarn:
yarn add 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 }
]
*/
Sub-functions
- delete
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 }
]
*/
//this anthor way to used filter "db.filter({age: 1})" 😉
db.filter({ age: 1 }).delete();
/* in file json :
[
{ _id: 'DniDQHMNpo', user: 'pebbles', age: 1, active: true },
{ _id: 'mL7Np2hr_Z', user: 'pebbles', age: 1, active: true }
]
*/
- updata
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.filter({ age: 1 }).updata([
{ name: "update 1" },
{ name: "update 2", active: false },
]);
/* in file json :
[
{ _id: 'Wtl9v2x-Q', user: 'barney', age: 36, active: true },
{ name:"update 1" },
{ _id: 'SflmjJaVN', user: 'barney', age: 36, active: true },
{ name:"update 2" , active: false }
]
*/
- put
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 }
]
*/
//this anthor way to used filter "db.filter({age: 1})" 😉
db.filter({ age: 1 }).put({ user: "new user" });
/* in file json :
[
{ _id: 'Wtl9v2x-Q', user: 'barney', age: 36, active: true },
{ _id: 'DniDQHMNpo', user: 'new user', age: 1, active: true }
]
*/
in filter.put() or .update() if you noUsed array in param will edit all item with same value
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 }
*/
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', ... } */