A simple, fast and pure Javascript key-value in memory database for Node JS.
This lightweight engine can be used in place of things like LevelDB, Redis while keeping things light and very simple. Internaly, memdb uses json as storage format with an opt-in support for encryption. In order to achieve performance, memdb keeps things in a staging area for fast access. No worries, all the booking is handled for you :). We are developers too, thus we hate keeping tract of things.
const MemDB = require('memdb')
const db = new MemDB('_starwars', {encryptionKey: 'secret'})
db.put('skywalker', {
force: 'light',
name: 'Luke Skywalker'
}).then((status) => {
console.log(status) // => true
})
db.get('skywalker').then((rsp) => {
console.log(rps) // => [Object skywalker...]
})
$ npm install evansofts-memdb
MemDB's API is pretty simple and straigth forward. Some api methods return a promise.
version()
: Get MemDB current versionrevision()
: Get the current database revisionoptions()
: Get the database instance optionsput(keychain, value, loose)
: Save data in the databaseget(keychain, defaultValue)
: Get data from the databaseall()
: Get all data fron the databasedelete(keychain)
: Delete data from the database
NB: What the heck is keychain ?
keychain is a concatenation of multiple object keys to obtain a path for accessing an object property in a deep level. This is a convenient way of puting/getting deeply nested object properties. The following keychain server.dev.host
means accessing the host
property form dev
object which in turn is available in the server
object as a property.
/*
By default, the databse is not encrypted and the stagging
operation size limit is 600 operations; of course you can move it up or down
*/
const db = new MemDB('db_file_path', {
stagingSize: 600, // maximum staging operation size before flushing the data
encryptionKey: null // optional if you need to encypt your data
})
put(keychain, value, loose)
Parameters | Description |
---|---|
keychain | Path where the data should be stored |
value | The value to be stored |
loose | Will create internal object of the chain if non existant, default to false |
db.put('server.dev.host', 'https://dev.alex.com', true).then((status) => {
console.log(status) // => true
})
get(keychain, defaultValue)
Parameters | Description |
---|---|
keychain | Path where the data should be retrieved |
defaultValue | The default value to be returned if not found |
db.get('server.dev.host', 'https://dev.alex.com').then((rsp) => {
console.log(rsp) // => https://dev.alex.com
})
all()
db.all().then((rsp) => {
console.log(rsp) // => whole database document
})
delete(keychain)
Parameters | Description |
---|---|
keychain | Path where the data should be deleted |
db.delete('server.dev.host').then((rsp) => {
console.log(rsp) // => true
})
After cloning the repo and installing all dependencies (using npm install
) you can run all tests using mocha:
$ npm test