README
Small-db
Small-db is a database useful when you would have to keep small information persistent on the server.
Installing
npm install small-db
Use-cases
A good example would be a news section on a basic website. Using this module you could easily add, remove, modify and show the information without really worrying about running out of space or the program slowing down.# How it works
How it works
The databased is loaded on RAM and after the necessary operations have been made, it can be stored on disk with the commit() method. This makes it fast but it’s impractical to handle big data with it. All the information is saved in a JSON file which has its limitations.
Terminology
There are collections which have documents in them. The collection is a way to group your documents, it can be seen as an RDBS table. Each document has a unique id that can be used to reference it.# Guide
Importing the Database
const DataBase = require('small-db');
Creating or opening a collection
let clientsColection = new DataBase().collection('clients.json');
Seeing the documents (returns the map of the documents)
clientsColection.documents;
Adding a document (returns the id of the document created)
let id = clientsColection.addDocument({a : "b"});
Removing a document (return true if the document could be removed)
clientsColection.removeDocument(id);
Checking if the id exist
clientsColection.has(id);
Updating a document (if the id doesn't exist it creates one)
clientsColection.updateDocument(id,{ c : "d"});
Writing the collection on the disk
clientsColection.commit();
Querying "." is reserved for traversing the json object
clientsColection.addDocument({a :{b : "c"}});
clientsColection.where("a.b == c");
Where function ( returns a query object)
clientsColection.where("name == George").where("age > 25");
clientsColection.where("number.mobile == 000");
Remove documents
clientsColection.where("age >=50").remove();
Update documents
clientsColection.where("age > 18").update("prefix",(data)=>{
if(data["sex"]="M")
return "Mr";
return "Mrs";
});
Sorting the documents
clientsColection.where("salary < 500").sortBy('age');
// or if we want to specify the direction (by default is 'asc')
clientsColection.where("salary < 500").sortBy('age','desc');
Getting the first documents with limit method
clientsColection.where("age > 18").limit(10);
Getting the number of clients that are underaged
clientsColection.where("age < 18").size;
Values method returnes the documents values as an array
for(val of clientsColection.values){
console.log(val)
}
Keys returnes the documents keys
for(val of clientsColection.keys){
console.log(val)
}
Usage
npm install small-db
Each collection function generates or works with an unique id, addDocument returns and id, removeDocument and updateDocument takes in an id. When working with a query the id is not needed.
A simple example showing a tipical usecase for populating and interogating the database.
let clientsColection = new DataBase().collection('clients.json');
clientsColection.addDocument({ "first-name": "Jhon",
"last-name" : "Lake",
age : "18",
contacts : { mobilePhone : 12345,
homePhone : 23456
} });
clientsColection.addDocument({ "first-name": "Kayle",
"last-name" : "Martin",
age : "25",
contacts : { mobilePhone : 98765,
homePhone : 87654
} });
clientsColection.addDocument({ "first-name": "Don",
"last-name" : "Dibble",
age : "30",
contacts : { mobilePhone : 99999,
homePhone : 89999
} });
/* The first where statment would have returned 2 results, but
because we further searched for a specific mobile number, only the
last object remains, so the update changes the last object first
name from Don to Ned. */
clientsColection.where("age > 20")
.where("contacts.mobilePhone == 99999")
.update("first-name", (obj) => {return "Ned";});
//Displaying all the collection documents
for(val of clientsColection.values){
console.log(val)
}
//Displaying information based on query
for(val of clientsColection.where("age > 0").sortBy("age").values){
console.log(val);
}
//This removes documents form a collection based on a query
clientsColection.where("first-name == Jhon").remove();
//But we can remove the keys directly from the collection if we want to
for(key of clientsColection.where("first-name == Kayle").keys){
clientsColection.removeDocument(key);
}
clientsColection.commit();