npm

Need private packages and team management tools?Check out npm Orgs. »

endb

0.16.3 • Public • Published

endb


Version Build Status Dependencies Downloads Stars License

Simple key-value storage for multi adapter. Officially supported adapters are LevelDB, MongoDB, MySQL, PostgreSQL, Redis, and SQLite. You can also integrate your own adapter (open a pull-request).

New to Endb? Check out the API Reference

Features

  • High performance, efficiency, and simplicity.
  • Simple Promise-based API.
  • Suitable as cache or persistent database.
  • Supports adapters, namespaces, serializers.
  • Handles all JSON types including Buffer.
  • Connection errors are sent to instance (connection errors won't kill the process).

Installation

npm install endb

By default, data is stored in memory. Optionally, You can install an adapter.

$ npm install level # LevelDB 
$ npm install mongojs # MongoDB 
$ npm install redis
 
# To use SQL database, install an additional 'sql' package and the adapter 
$ npm install sql
 
$ npm install mysql2 # MySQL 
$ npm install pg # PostgreSQL 
$ npm install sqlite3 # SQLite 

Usage

const Endb = require('endb');
 
const endb = new Endb();
const endb = new Endb('leveldb://path/to/database');
const endb = new Endb('mongodb://user:pass@localhost:27017/dbname');
const endb = new Endb('mysql://user:pass@localhost:3306/dbname');
const endb = new Endb('postgresql://user:pass@localhost:5432/dbname');
const endb = new Endb('redis://user:pass@localhost:6379');
const endb = new Endb('sqlite://path/to/database.sqlite');
 
// Handles database connection error
endb.on('error', err => console.log('Connection Error: ', err));
 
await endb.set('foo', 'bar'); // true
await endb.set('exists', true); // true
await endb.set('num', 10); // true
await endb.math('num', 'add', 40); // true
await endb.get('foo'); // 'bar'
await endb.get('exists'); // true
await endb.all(); // { ... }
await endb.has('foo'); // true
await endb.has('bar'); // false
await endb.find(value => value === 'bar'); // { ... }
await endb.delete('foo'); // true
await endb.clear(); // undefined

Namespaces

You can set a namespace to avoid key collisions and namespaces allow you to clear only a certain namespace while using the same database.

const users = new Endb('redis://user:pass@localhost:6379', { namespace: 'users' });
const cache = new Endb('redis://user:pass@localhost:6379', { namespace: 'cache' });
 
await users.set('foo', 'users'); // true
await cache.set('foo', 'cache'); // true
await users.get('foo'); // 'users'
await cache.get('foo'); // 'cache'
await users.clear(); // undefined
await users.get('foo'); // undefined
await cache.get('foo'); // 'cache'

Third-party Adapters

You can integrate and use a third-party adapter or build your own.

const Endb = require('endb');
const myAdapter = require('./my-adapter');
 
const endb = new Endb({ store: myAdapter });

Any store that follows the Map API will work.

new Endb({ store: new Map() });

For example, quick-lru is an unrelated module that implements the Map API. However, extension methods (all and find) may not work.

const Endb = require('endb');
const QuickLRU = require('quick-lru');
 
const endb = new Endb({ store: new QuickLRU() });

Custom Serializers

Endb uses its own parse and stringify functions for serialization and deserialization of data to ensure consistency. Optionally, You can pass your own (de)serialization functions to support extra data types or to (de)serialize to something else.

const endb = new Endb({
    serialize: JSON.stringify,
    deserialize: JSON.parse
});

install

npm i endb

Downloadsweekly downloads

153

version

0.16.3

license

MIT

homepage

endb.js.org

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability