zerodb

0.2.2 • Public • Published

ZeroDB

Easy to use JSON database for Node.js with encryption.


Table of Contents

Install

npm:

npm install zerodb

Yarn:

yarn add zerodb

GitHub:

git clone https://github.com/mazecodes/zerodb.git

Usage

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', 'john@doe.com');

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'); // true

Delete a propery:

db.delete('user.name');
db.has('user.name'); // false

Reset 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 5

Decrease the value:

db.decrement('user.age'); // Decrease by 1
db.decrement('user.age', 5); // Decrease by 5

Update 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.

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AwesomeFeature)
  3. Commit your changes (git commit -m "Add Awesome Feature")
  4. Push to the branch (git push origin feature/AwesomeFeature)
  5. Open a Pull Request

Author

Maze Peterson:

Show your support

Give a if you liked this project!

License

MIT © Maze Peterson

Package Sidebar

Install

npm i zerodb

Weekly Downloads

6

Version

0.2.2

License

MIT

Unpacked Size

25.8 kB

Total Files

11

Last publish

Collaborators

  • mazecodes