A simple wrapper around JSON to implement a persistent psuedo-database.
This module is a wrapper around JSON which makes it easier to work with. This database is persistent, meaning that even after a Node.js process is closed, the data is not lost. This is a local database, meaning it will be in your codebase and ready to view at any time. It is also portable, meaning if you copy this folder to a new directory, the data will still be read in the same way.
This is a Class, which takes in an Options object as a parameter when intializing.
interface Options {
/** The path to read from or write to. */
file: string;
/** Specify whether or not to create backups. Defaults to `false` */
backup?: boolean;
/** Specify whether or not to create verbose logs. Defaults to `true`. */
verbose?: boolean;
/** Whether or not to defer saves to file. `0` means no delay, default is `0`. */
defer?: number;
/** The amount of indents to use when saving to file. Defaults to `0`. */
indents?: number;
}Upon instantiation, this has a property called pool, where all handling of the database occurs.
Options.backup is currently not implemented. In the future, it will store multiple backups of the database locally in the event of data loss. Defaults to false.
Options.defer takes a number (in millisecond format), to save at that specific interval. Default is 0, meaning it saves after every operation.
All methods of Pool are asynchronous, and return a Promise. Will return Promise<void> if Options.defer exists.
Pool.find(callback, all?) retrieves entries given a callback. all defaults to true, determines if all entries should be given back.
Pool.ensure(key, value, path) sets a value to the database if the key does not have a value already assigned to it.
Pool.observe(key, path, cb) observes an object for any changes, and executes the callback if a change occurs.
const { JSONWrapper } = require('jsdatabase.json');
const database = new JSONWrapper({
file: 'db',
verbose: true,
indents: 4
}).pool;
(async function() {
// Get/Set without paths
await database.set('a', 3);
console.log(await (database.get('a'))); // 3
// Get/Set with paths
await database.set('a', { b: 4 });
console.log(await (database.get('a'))); // { b: 4 }
console.log(await(database.get('a', 'b'))); // 4
await database.set('a', 5, 'b');
console.log(await(database.get('a', 'b'))); // 5
})();