

A simple "database" that use JSON file for Node.JS.
Installation
Add node-json-db
to your existing Node.js project.
yarn add node-json-db
Documentation
Auto Generated
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 fetch 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.
import { JsonDB } from 'node-json-db';
import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
var db = new JsonDB(new Config("myDataBase", true, false, '/'));
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();
db.reload();
TypeScript Support
v0.8.0
As of v0.8.0, TypeScript types are
included in this package, so using @types/node-json-db
is no longer required.
v1.0.0
JsonDB isn't exported as default any more. You'll need to change how you load the library.
This change is done to follow the right way to import module.
import { JsonDB } from 'node-json-db';
import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
const db = new JsonDB(new Config("myDataBase", true, false, '/'));
Array Support
You can also access the information stored into arrays and manipulate them.
import { JsonDB } from 'node-json-db';
import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
const db = new JsonDB(new Config("myDataBase", true, false, '/'));
db.push("/arraytest/myarray[0]", {
obj:'test'
}, true);
var testString = db.getData("/arraytest/myarray[0]/obj");
db.delete("/arraytest/myarray[0]");
Appending in Array
db.push("/arraytest/myarray[]", {
obj:'test'
}, true);
db.push("/arraytest/myarray[]/myTest", 'test', true);
Last Item in Array
db.push("/arraytest/lastItemArray", [1, 2, 3], true);
db.getData("/arraytest/lastItemArray[-1]");
db.delete("/arraytest/lastItemArray[-1]");
db.getData("/arraytest/lastItemArray[-1]");
Exception/Error
Type
DataError | When the error is linked to the Data Given |
DatabaseError | Linked to a problem with the loading or saving of the Database. |
Errors
The Data Path can't be empty | DataError | The Database expect to minimum receive the root separator as DataPath. |
Can't find dataPath: /XXX. Stopped at YYY | 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. |
DataPath: /XXX. YYY is not an array. | DataError | When trying to access an object as an array. |
DataPath: /XXX. Can't find index INDEX in array YYY | DataError | When trying to access a non-existent index in the array. |
Only numerical values accepted for array index | DataError | An array can only use number for its indexes. For this use the normal object. |
The entry at the path (/XXX) needs to be either an Object or an Array | DataError | When using the find method, the rootPath need to point to an object or an array to search into it for the wanted value. |
Can't Load Database: XXXX | DatabaseError | JsonDB can't load the database for "err" reason. You can find the nested error in error.inner |
Can't save the database: XXX | 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. |
Limitations
Object with seperator
in key
Object pushed with key containing the seperator
character won't be reachable. See #75.
Please consider the seperator
as a reserved character by node-json-db.
Thanks
James Davis for helping to fix a regular expression vulnerable to catastrophic backtracking.
License
