npm

Does your artifact manager get in the way? Join us on Oct. 8 at 10am PT, to discuss how npm can help.Sign up »

endb

0.16.0 • Public • Published

Endb

NPM Version Build Status NPM Dependencies NPM Downloads GitHub Stars License

Simple key-value database with cache and multi adapter support. Supported adapters are LevelDB, MongoDB, MySQL, PostgreSQL, Redis, and SQLite.

New to Endb? Check out the API Reference

  • High performance, efficiency, and simplicity
  • Simple Promise-based API
  • Suitable as cache or persistent database
  • Supports adapters & namespaces
  • Handles all JSON types including Buffer

Installation

npm install --save endb

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

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

Usage

const Endb = require('endb');
 
// Supported Adapters
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.get('foo'); // 'bar'
await endb.get('foo', { raw: true }); // { value: 'bar' }
await endb.has('foo'); // true
await endb.has('bar'); // false
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'

Custom Serializers

It uses JSON buffer for serialization and derialization of data to ensure consistency. You can optionally pass your own (de)serialization functions to support extra data types or to (de)serialize to something other than JSON.

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

Third-party Adapters

You can also use third-party adapters or build & integrate your own. It will integrate these adapters and handle complex types internally.

const custom = require('./custom');
const endb = new Endb({ store: custom });

For instance, quick-lru is compatible with Endb since it implements Map.

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

The following are third-party adapters compatible:

  • quick-lru - Simple "Least Recently Used" (LRU) cache
  • To include your own third-party adapter here, open a pull-request

License

Copyright (c) enhancd. All rights reserved.

Licensed under the MIT license.

install

npm i endb

Downloadsweekly downloads

96

version

0.16.0

license

MIT

homepage

endb.js.org

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability