A simple "database" that uses JSON file for Node.JS.
Breaking changes since v1.x.x
v2.0.0
JsonDB is now using the concept of async/await for all its calls since we read from the database file on demand and
depending on how the database is configured, we might write at each push.
- You're now forced to use the
Config
object to setup JsonDB - Every method is now asynchronous
Installation
Add node-json-db
to your existing Node.js project.
YARN:
yarn add node-json-db
NPM:
npm i node-json-db
Documentation
Auto Generated
Inner Working
Data
The module stores the data using JavaScript Object directly into a JSON file. You can easily traverse the data to directly reach
the desired 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 also get the full object test : /test
Or even the root : /
Usage
See test for more usage details.
import { JsonDB, Config } from 'node-json-db';
var db = new JsonDB(new Config("myDataBase", true, false, '/'));
await db.push("/test1","super test");
await db.push("/test2/my/test",5);
await db.push("/test3", {test:"test", json: {test:["test"]}});
await db.push("/test3", {
new:"cool",
json: {
important : 5
}
}, false);
await db.push("/test2/my/test/",10,false);
var data = await db.getData("/");
var data = await db.getData("/test1");
try {
var data = await db.getData("/test1/test/dont/work");
} catch(error) {
console.error(error);
};
var data = await db.getObjectDefault<string>("/super/path", "myDefaultValue");
await db.delete("/test1");
await db.save();
await 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, Config } from 'node-json-db';
const db = new JsonDB(new Config("myDataBase", true, false, '/'));
Typing
With TypeScript, you have access to a new method: getObject that will take care of typing your return object.
import { JsonDB, Config } from 'node-json-db';
const db = new JsonDB(new Config("myDataBase", true, false, '/'));
interface FooBar {
Hello: string
World: number
}
const object = {Hello: "World", World: 5} as FooBar;
await db.push("/test", object);
const result = await db.getObject<FooBar>("/test");
Array Support
You can also access the information stored in arrays and manipulate them.
import { JsonDB, Config } from 'node-json-db';
const db = new JsonDB(new Config("myDataBase", true, false, '/'));
await db.push("/arraytest/myarray[0]", {
obj:'test'
}, true);
var testString = await db.getData("/arraytest/myarray[0]/obj");
await db.delete("/arraytest/myarray[0]");
Appending in Array
await db.push("/arraytest/myarray[]", {
obj:'test'
}, true);
await db.push("/arraytest/myarray[]/myTest", 'test', true);
Last Item in Array
await db.push("/arraytest/lastItemArray", [1, 2, 3], true);
await db.getData("/arraytest/lastItemArray[-1]");
await db.delete("/arraytest/lastItemArray[-1]");
await db.getData("/arraytest/lastItemArray[-1]");
Count for Array
await db.push("/arraytest/list", [{id: 65464646155, name: "test"}], true);
let numberOfElements = await db.count("/arraytest/list");
Get Index in Array
await db.push("/arraytest/myarray", {id: 65464646155, name: "test"}, true);
await db.getIndex("/arraytest/myarray", 65464646155);
await db.getIndex("/arraytest/myarray", "test", "name");
await db.delete("/arraytest/myarray[" + await db.getIndex("/arraytest/myarray", 65464646155) + "]");
Nesting in Array
await db.push("/arraytest/myarray",
[
[
{
obj: 'test'
},
{
obj: 'hello'
}
],
[
{
obj: 'world'
}
]
]
, true);
await db.getData("/arraytest/myarray[0][0]");
Exception/Error
Type
Type | Explanation |
---|
DataError | When the error is linked to the Data Given |
DatabaseError | Linked to a problem with the loading or saving of the Database. |
Errors
Error | Type | Explanation |
---|
The Data Path can't be empty | DataError | The Database expects to receive at least the root separator as part of the DataPath. |
Can't find dataPath: /XXX. Stopped at YYY | DataError | When the full hierarchy of the provided DataPath is not present in the Database, it indicates the extent of its validity. This error can occur when using the getData and delete operations. |
Can't merge another type of data with an Array | DataError | This occurs if you chose not to override the data when pushing (merging) and the new data is an array, but the current data isn't an array (an Object for example). |
Can't merge an Array with an Object | DataError | Similar to the previous message, you have an array as the current data and request 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 needs to point to an object or an array to search within it for the desired 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 separator
in key
Object pushed with key containing the separator
character won't be reachable. See #75.
Please consider the separator
as a reserved character by node-json-await db.
Contributors
Special Thanks
James Davis for helping to fix a regular expression vulnerable to catastrophic backtracking.
License
![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FBelphemur%2Fnode-json-await db.svg?type=large)