

A simple "database" that use JSON file for Node.JS.
Installation
Add node-json-db
to your existing Node.js project.
npm install node-json-db --save
Inner Working
###Data
The module store the data using JavaScript Object directly into a JSON file. You can easily traverse the data to reach
directly the interesting property using the DataPath. The principle of DataPath is the same as XMLPath.
###Example
{
test: {
data1 : {
array : ['test','array']
},
data2 : 5
}
}
If you want to fet the value of array, the DataPath is /test/data1/array
To reach the value of data2 : /test/data2
You can of course get also the full object test : /test
Or even the root : /
Usage
See test for more usage details.
var JsonDB = require('node-json-db');
var db = new JsonDB("myDataBase", true);
db.push("/test1","super test");
db.push("/test2/my/test/",5);
db.push("/test3", {test:"test", json: {test:["test"]}});
db.push("/test3", {new:"cool", json: {important : 5}}, false);
db.push("/test2/my/test/",10,false);
var data = db.getData("/");
var data = db.getData("/test1");
try {
var data = db.getData("/test1/test/dont/work");
} catch(error) {
console.error(error);
}
db.delete("/test1");
db.save();
Exception/Error
JsonDB use 2 type of Error/Exception :
DataError | When the error is linked to the Data Given |
DatabaseError | Linked to a problem with the loading or saving of the Database. |
####"The Data Path can't be empty" (DataError)
The Database expect to minimum receive the root / as DataPath.
####"Can't find dataPath: /" + dataPath.join("/") + ". Stopped at " + property (DataError)
When the full hierarchy of the DataPath given is not present in the Database.
It tells you until where it's valid. This error can happen when using getData
and delete
####"Can't merge another type of data with an Array" (DataError)
If you chose to not override the data (merging) when pushing and the new data is an array
but the current data isn't an array (an Object by example).
####"Can't merge an Array with an Object" (DataError)
Same idea as the previous message. You have an array as current data and ask to
merge it with an Object.
####"Can't Load Database: " + err (DatabaseError)
JsonDB can't load the database for "err" reason.
You can find the nested error in error.inner
####"Can't save the database: " + err (DatabaseError)
JsonDB can't save the database for "err" reason.
You can find the nested error in error.inner
####"DataBase not loaded. Can't write" (DatabaseError)
Since the database hasn't been loaded correctly, the module won't let you save the data to avoid erasing your database.