Features
DreamyOptions : Object
Object
The options for Dreamy-db.
new Dreamy( options = { } )
new Dreamy( options = { } )
Parameters
options
:string
| DreamyOptions
Properties
uri
: string
uri
: string
The connection URI of the database.
namespace
: string
The namespace of the database.
adapter
:string
The storage adapter or backend to use.
store
:*
The options for Dreamy.
serialize
:function
A data serialization function.
deserialize
:function
A data deserialization function.
collection
:string
The name of the collection. Only works for MongoDB.
table
:string
The name of the table. Only works for SQL databases.
keySize
:number
The maximum size of the keys of elements.
Example:
const Database = require("dreamy-db");
// SQLite Adapter
const db = new Database.Dreamy({
uri: "sqlite://DataBase/test.sqlite",
namespace: "dreamy",
table: "dreamy"
});
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 alive = new Database.Dreamy({ namespace: 'alive' });
const dead = new Database.Dreamy({ namespace: 'dead' });
// Setting
await alive.set('zombie', 'alive'); // true
await dead.set('zombie', 'dead'); // true
// getting
await alive.get('zombie'); // 'alive'
await dead.get('zombie'); // 'dead'
// clearing alive key
await alive.clear(); // undefined
await alive.get('zombie'); // undefined
await dead.get('zombie'); // 'dead'
Third-Party Adapters
You can optionally utilize third-party storage adapters or build your own. Dreamy-db will integrate the third-party storage adapter and handle complex data types internally.
const myAdapter = require('./my-adapter');
const db = new Database.Dreamy({ store: myAdapter });
const QuickLRU = require('quick-lru');
const lru = new QuickLRU({ maxSize: 1000 });
const db = new Database.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 db = new Database.Dreamy({
serialize: JSON.stringify,
deserialize: JSON.parse
});
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 Database.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 });
Last updated
Was this helpful?