dreamy-db
v2.0.6
Published
Dreamy-db - A Powerful database for storing, accessing, and managing multiple databases.
Downloads
23
Maintainers
Readme
About
Dreamy-db - A Powerful database for storing, accessing, and managing multiple databases.
Dreamy-db is a powerful node.js module that allows you to interact with the databases very easily.
Why?
- Object-oriented
- Feature-rich
- Performant
- Configurable
- 100% Promise-based
- Speedy and efficient
- Persistent storage
Features
- Adapters: By default, data is cached in memory. Optionally, install and utilize a "storage adapter".
- Namespaces: Namespaces isolate elements within the database to enable useful functionalities.
- Custom Serializers: Utilizes its own data serialization methods to ensure consistency across various storage backends.
- Third-Party Adapters: You can optionally utilize third-party storage adapters or build your own.
- Embeddable: Designed to be easily embeddable inside modules.
- Data Types: Handles all the JSON types including
Buffer
. - Error-Handling: Connection errors are transmitted through, from the adapter to the main instance; consequently, connection errors do not exit or kill the process.
Installation
Node.js 12.x or newer is required.
Using npm:
$ npm install dreamy-db
Using yarn:
$ yarn add dreamy-db
By default, data is cached in memory. Optionally, install and utilize a "storage adapter".
Officially supported adapters are
- LevelDB
- MongoDB
- NeDB
- MySQL
- PostgreSQL
- Redis
- SQLite.
$ npm install level # LevelDB
$ npm install mongojs # MongoDB
$ npm install ioredis # Redis
# To use SQL database, an additional package 'sql' must be installed and an adapter
$ npm install sql
$ npm install mysql2 # MySQL
$ npm install pg # PostgreSQL
$ npm install sqlite3 # SQLite
Usage
const { Dreamy } = require('dreamy-db');
// Choose One of the following:
const db = new Dreamy();
const db = new Dreamy('leveldb://path/to/database');
const db = new Dreamy('mongodb://user:pass@localhost:27017/dbname');
const db = new Dreamy('mysql://user:pass@localhost:3306/dbname');
const db = new Dreamy('postgresql://user:pass@localhost:5432/dbname');
const db = new Dreamy('redis://user:pass@localhost:6379');
const db = new Dreamy('sqlite://path/to/database.sqlite');
// Handles connection errors
db.on('error', error => console.error('Connection Error: ', error));
await db.set('foo', 'bar'); // true
await db.find(data => data === 'bar'); // { key: 'foo', value: 'bar' }
await db.get('foo'); // 'bar'
await db.math('dreamy', 'add', 200); // true
await db.has('foo'); // true
await db.all(); // [ { key: 'foo', value: 'bar' } ]
await db.delete('foo'); // true
await db.clear(); // undefined
Namespaces
Namespaces isolate elements within the database to avoid key collisions, separate elements by prefixing the keys, and allow clearance of only one namespace while utilizing the same database.
const users = new Dreamy({ namespace: 'users' });
const members = new Dreamy({ namespace: 'members' });
await users.set('foo', 'users'); // true
await members.set('foo', 'members'); // true
await users.get('foo'); // 'users'
await members.get('foo'); // 'members'
await users.clear(); // undefined
await users.get('foo'); // undefined
await members.get('foo'); // 'members'
Third-Party Adapters
You can optionally utilize third-party storage adapters or build your own. Dreamy will integrate the third-party storage adapter and handle complex data types internally.
const myAdapter = require('./my-adapter');
const database = new Dreamy({ store: myAdapter });
For example, quick-lru
is an unrelated and independent module that has an API similar to that of Dreamy.
const QuickLRU = require('quick-lru');
const lru = new QuickLRU({ maxSize: 1000 });
const database = new Dreamy({ store: lru });
Custom Serializers
Dreamy-db handles all the JSON data types including Buffer using its data serialization methods that encode Buffer data as a base64-encoded string, and decode JSON objects which contain buffer-like data, either as arrays of strings or numbers, into Buffer instances to ensure consistency across various backends.
Optionally, pass your own data serialization methods to support extra data types.
const database = new Dreamy({
serialize: JSON.stringify,
deserialize: JSON.parse
});
| :warning: warning | Using custom serializers means you lose any guarantee of data consistency. | |----------------------|:---------------------------------------------------------------------------|
Embeddable
Dreamy-db is designed to be easily embeddable inside modules. It is recommended to set a namespace for the module.
class MyModule {
constructor(options) {
this.db = new Dreamy({
uri: typeof opts.store === 'string' && opts.store,
store: typeof opts.store !== 'string' && opts.store
namespace: 'mymodule'
});
}
}
// Caches data in the memory by default.
const myModule = new MyModule();
// After installing ioredis.
const myModule = new MyModule({ store: 'redis://localhost' });
const myModule = new AwesomeModule({ store: thirdPartyAdapter });