zerodb
v0.2.2
Published
Easy to use JSON database for Node.js with encryption.
Readme
Table of Contents
Install
npm:
npm install zerodbYarn:
yarn add zerodbGitHub:
git clone https://github.com/mazecodes/zerodb.gitUsage
Load a database:
const ZeroDB = require('zerodb');
const db = new ZeroDB('./database.json');If the database doesn't exist, ZeroDB will create a new one.
Adding defaults:
db.init({
posts: [],
user: {},
});You can also force the database to replace the current state with the initial state:
db.init(
{
posts: [],
user: [],
},
{ force: true }
);Save the database:
await db.save();Set a value:
db.set('user.name', 'John Doe');You can set multiple values with chaining:
db.set('user.name', 'John Doe')
.set('user.age', 18)
.set('user.email', '[email protected]');Get a value:
db.get('user.name');You can also provide a fallback value:
db.get('user.admin', false);Push to an array:
db.push('posts', {
id: 0,
title: 'Hello World',
});You can also push multiple values with chaining:
db.push('posts', {
id: 0,
title: 'Hello World 1',
})
.push('posts', {
id: 1,
title: 'Hello World 2',
})
.push('posts', {
id: 2,
title: 'Hello World 3',
})
.push('posts', {
id: 3,
title: 'Hello World 4',
});ZeroDB will set a new array if the pushing path doesn't exist.
Check if a property exists:
db.has('user.name'); // trueDelete a propery:
db.delete('user.name');
db.has('user.name'); // falseReset the database to its initial state:
db.reset();Find all the matches:
db.find('posts', { author: 'John' });You can also use RegExp:
db.find('posts', { title: /^Hello/ });Find only the first match:
db.findOne('post', { id: 0 });Increase the value:
db.increment('user.age'); // Increase by 1
db.increment('user.age', 5); // Increase by 5Decrease the value:
db.decrement('user.age'); // Decrease by 1
db.decrement('user.age', 5); // Decrease by 5Update a property based on its last value:
db.update('user.name', name => name.toLowerCase());Get the current state of the database:
db.getState();Replace the current state:
db.setState({ foo: 'bar' });Destory the database:
db.destory();This will also delete the database file. If you don't want that, you can specify it like this:
db.destroy(false);Encryption
Using encryption with ZeroDB is pretty simple. All you have to do is:
new ZeroDB('./database.json', {
encryption: true,
secret: 's3cr3t',
iterations: 50_000,
});iterations is the number of iterations used for key derivation. The encryption key will be derived from secret.
ZeroDB uses PBKDF2 for key derivation with default iterations set to 50,000 and uses AES256 for encryption. It will also use HMAC-SHA256 for signing the state.
Note: The encryption will only happen when the database is being saved.
Contributing
All contributions, issues and feature requests are welcome! Please feel free to check issues page.
- Fork the project
- Create your feature branch (
git checkout -b feature/AwesomeFeature) - Commit your changes (
git commit -m "Add Awesome Feature") - Push to the branch (
git push origin feature/AwesomeFeature) - Open a Pull Request
Author
Maze Peterson:
Show your support
Give a ⭐ if you liked this project!
License
MIT © Maze Peterson
