1.0.3 • Published 4 years ago

declarative-db v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

declarative-db

NpmVersion npm NodeVersion GitHub GitHub stars GitHub issues GitHub closed issues

An easy-to-use declarative json-based database for Node

Installation

npm install declarative-db

Usage

const path = require('path');
const declare = require('declarative-db');

// Declare json-based database
declare({
  filename: path.join(__dirname, './db.json'),
}).then(db => {
  // Use it as you would normally use an object (or array)
  db.users = [{
    username: 'loarca',
  }];

  // It will automatically save to disk asynchronously when appropriate
});

Since it's Promise based you can also use async-await

const path = require('path');
const declare = require('declarative-db');

(async () => {
  // Declare json-based database
  let db = await declare({
    filename: path.join(__dirname, './db.json'),
  });

  // Use it as you would normally use an object (or array)
  db.users = [{
    username: 'loarca',
  }];

  // It will automatically save to disk asynchronously when appropriate
})();

You should declare() only once per file and share declared objects/arrays across all modules, as every declared object/array is... exactly that... an object/array in memory that automatically saves to disk when appropriate

Options

{
  filename: path.join(__dirname, './db.json'),
  initialState: {},
  compression: 0,
}

filename

File to store data

initialState (default = {})

When no database file exists initialState is used, it may be an object or array

compression (default = 0)

Compression level ranging from 0 to 9

Level 0 outputs database to readable json

{
  "users": [
    {
      "username": "loarca"
    }
  ]
}

Levels from 1 to 9 match DEFLATE compression levels

To do

  • Documentation
  • Extensive testing
  • Smart scheduled saving
  • Protect scheduled saving from unexpected process termination
  • When filename option is not present, manage database in memory
  • Force saving when desired