statedb

4.2.1 • Public • Published

StateDb

Little wrapper around lowdb, for persitence of states.

Usage:

new StateDb (dbPath, [appkey], [options]);

StateDb create a file at dbPath and add .db extensions. So, if you pass /mount/app.settings the result in the filesystem is /mount/app.settings.db

With the appkey you can categorize your app/domain in the db file. For example, if you wanna persist another domain in the same file you can do:

const myDbPath = '/mount/local/file';
const UsersDb = new StateDb(myDbPath, 'users');
const CustomersDb = new StateDb(myDbPath, 'customers');

If the appkey is omitted, the dbPath will be used as appkey. This behavior create an hard link with the db location and the content. If you try to open the Db with another path, the content is reseted.

In general, if you provide a database as ressource, don't ommit the appkey.

saveState ('state key', value)

Save something at state key.

loadState ('state key')

Load something that reside at state key.

Example:

import StateDb from 'statedb';

async function main() {
  const myDbPath = '/mount/local/burritos';
  const savedSettings = new StateDb(myDbPath, 'settings');
  const savedUsers = new StateDb(myDbPath, 'users');

  await savedSettings.init();
  await savedUsers.init();

  // saving
  savedSettings.saveState('bobSettings', {
    x: 800,
    y: 600,
    locale: 'en_US',
  });

  // loading
  const settings = savedSettings.loadState('settings');
  // todo: do some magic with loaded settings...
}

main();

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.2.136latest

Version History

VersionDownloads (Last 7 Days)Published
4.2.136
4.2.00
4.1.10
4.1.00
4.0.01
3.0.321
3.0.20
3.0.10
3.0.00
2.2.06
2.1.00
2.0.30
2.0.20
2.0.10
2.0.00
1.0.00

Package Sidebar

Install

npm i statedb

Weekly Downloads

64

Version

4.2.1

License

MIT

Unpacked Size

13.7 kB

Total Files

12

Last publish

Collaborators

  • schroeterm
  • samlebarbare