ldbjs
A lib to create and manage local db files and data, whit a simple send and get system,
save any data, html content, js objects and arrays, JSON, some texts, anything.
The erros in the operations is automatically returned not include in yours callbacks.
Creating a db location folder
const ldb = require('ldbjs');
ldb.createDB('.', 'myDB');
ldb.createDB('.', 'myJsonDB');
And your db are created.
Creating DB file
const ldb = require('ldbjs');
ldb.createDB('.', 'myDB');
ldb.createDBFile('.', 'myDB', 'myDBFile');
The file extension are .ldb.
Sending data to my dbfile
const ldb = require('ldbjs');
ldb.createDB('.', 'myDB');
ldb.createDBFile('.', 'myDB', 'myDBFile');
let myData = 'Hello World!';
ldb.sendData('.', 'myDB', 'myDBFile', myData, ()=>{
console.log(`${myData} as been sended to my ldb DB!`);
});
Sent data is saved exactly as sent
Getting data from dbfile
const ldb = require('ldbjs');
ldb.getData('.', 'myDB', 'myDBFile', (data)=>{
console.log(data);
});
The returned value is the data of the file.
This function is used only with a callback to get the parameter.
Getting the DB files list
const ldb = require('ldbjs');
ldb.createDB('.', 'db');
ldb.createDBFile('.', 'db', 'test');
ldb.createDBFile('.', 'db', 'hello');
ldb.json.createDBFile('.', 'db', 'world');
ldb.getDBFiles('.', 'db');
To use a callback in that function use like this
ldb.getDBFiles('.', 'db', (files)=>{
console.log(files);
});
This function return the files name.
To overwrite data of the file
const ldb = require('ldbjs');
ldb.createDB('.', 'myDB');
ldb.createDBFile('.', 'myDB', 'myDBFile');
let myData = 'I overwrited data!';
ldb.overwriteData('.', 'myDB', 'myDBFile', myData);
That action is irreversible.
Cloning the content to other dbfile
const ldb = require('ldbjs');
ldb.createDB('.', 'myDB');
ldb.createDBFile('.', 'myDB', 'myDBFile');
let myData = 'I overwrited data!';
ldb.sendData('.', 'myDB', 'myDBFile', myData);
ldb.cloneDBFile('.', 'myDB', 'myDBFile', '.', 'myDB', 'myOtherDBFile', ()=>{
console.log('cloned a db file');
});
To clone a file you need the two files created.
Deleting a dbfile
const ldb = require('ldbjs');
ldb.deleteDBFile('.', 'myDB', 'myDBFile', ()=>{
console.log('the dbfile as been deleted');
});
That action is irreversible.
To rename a dbfile
const ldb = require('ldbjs');
ldb.renameDBFile('.', 'myDB', 'myDBFile', 'myNewDBFileName');
This function not receive a callback, are shown in the console the new stats of the file
Using events
const ldb = require('ldbjs');
ldb.event.on('create a DB', (path, name)=>{
ldb.createDB(path, name);
});
ldb.event.emit('create a DB', '.', 'myDB');
Events are a very simple way to create and manage DBs and DBfiles in your application, don't repeat code anymore.
Sending and getting data from json
const ldb = require('ldbjs');
ldb.json.createDBFile('.', 'myJSONDBName', 'myJSONFileName', ()=>{
console.log('json dbfile created');
});
let myJSObj = {
name: "Jhon",
age: 27
};
ldb.json.sendData('.', 'myJSONDBName', 'myJSONFileName', myJSObj);
ldb.json.getData('.', 'myJSONDBName', 'myJSONFileName', (data)=>{
cosole.log(data);
});
While a data are getted a js obj are return, the api automatically convert the json to a js object for you.
JSON insert and delete
const ldb = require('ldbjs');
ldb.json.createDBFile('.', 'myJSONDBName', 'myJSONFileName');
let myJSObj = {
name: "Jhon",
age: 27,
parents: {
bro: "Fred"
}
};
ldb.json.sendData('.', 'myJSONDBName', 'myJSONFileName', myJSObj);
ldb.json.insert('.', 'myJSONDBName', 'myJSONFileName', null, 'name', 'Alex', ()=>{
});
ldb.json.insert('.', 'myJSONDBName', 'myJSONFileName', 'parents', 'bro', 'Daniel');
ldb.json.insert('.', 'myJSONDBName', 'myJSONFileName', null, 'lastName', 'Drake');
ldb.json.delete('.', 'myJSONDBName', 'myJSONFileName', null, 'parents');
To insert data use the index propertie to put a new value, this function must
have a json file not empty to work.
JSON push, add and remove
const ldb = require('ldbjs');
ldb.json.createDBFile('.', 'myJSONDBName', 'myJSONFileName');
let obj = {
arr: []
};
ldb.json.sendData('.', 'myJSONDBName', 'myJSONFileName', obj);
ldb.json.push('.', 'myJSONDBName', 'myJSONFileName', null, 'arr', 1);
ldb.json.add('.', 'myJSONDBName', 'myJSONFileName', null, 'arr', 2);
ldb.json.remove('.', 'myJSONDBName', 'myJSONFileName', null, 'arr', 1);
Base64 encode and decode
const ldb = require('ldbjs');
let str = 'Hello World!';
ldb.base64.encode(str);
ldb.base64.decode(str);
This process use the base-64 module, this is downloaded like a dependecie.
Backup and rollback
const ldb = require('ldbjs');
ldb.backup('.', 'dbName', 'fileName');
ldb.rollback('.', 'dbName', 'fileName');
ldb.json.backup('.', 'dbName', 'fileName');