idkeyvalue
v0.0.6
Published
Unified (ID), Key - Value adapters for node databases
Downloads
3
Readme
idkeyvalue
Unified (ID), Key - Value adapters for node databases.
Interface
.set(key, value (,done[err]))
The done callback is optional and called with any errors that occurred while saving..get(key, (defaultValue,) done[err, value])
The done callback is called with the stored value or any errors that occurred while getting.
IfdefaultValue
is given, that value will be returned in case no entries were found in database..remove(key (,done[err]))
The done callback is optional and called with any errors that occurred while deleting..update(key, (defaultValue,) updater[value, done[err, updatedValue]] (,done[err, updatedValue]))
Shorthand for get, update, set operations. updater is called with stored/default value and is expected to calldone
with any error and the updated value. The done callback is optional
Id ?!?
Multiple Adapter Instances can initiated with the same database but different ids/namespaces.
This way we crate multiple key - value stores in the same object/file/table.
var myDatabase = new DB();
// with ID
var adapterA = Adapter(myDatabase, 1);
var adapterB = Adapter(myDatabase, 2);
adapterA.set('foo', 'bar');
adapterB.get('foo', function(err, value) {
// error 'foo not found'
});
// without ID
var adapterC = Adapter(myDatabase);
var adapterD = Adapter(myDatabase);
adapterA.set('lorem', 'ipsum');
adapterB.get('lorem', function(err, value) {
// value === 'ipsum'
});
Adapters
ObjectAdapter
The simplest store we can get. A JS-Hash.
var ObjectAdapter = require('idkeyvalue').ObjectAdapter;
var store = {};
var objectAdapter = new ObjectAdapter(store); // optional id as 2nd param
// objectAdapter.set( ...
// objectAdapter.get( ...
// objectAdapter.remove( ...
// objectAdapter.update( ...
NedbAdapter
See NeDB.
Embedded persistent database for Node.js
var Datastore = require('nedb');
var ObjectAdapter = require('idkeyvalue').NedbAdapter;
var database = new Datastore(); // see nedb documentation
var nedbAdapter = new NedbAdapter(database); // optional id as 2nd param
// nedbAdapter.set( ...
// nedbAdapter.get( ...
// nedbAdapter.remove( ...
// nedbAdapter.update( ...
Contributing
I'll add more adapters when needed, feel free to ask for or add some on your own.
The tests are written in a way, that we do not need to repeat them for every new adapter.
We just need to configure the test helper an give it some instances to play with.
See objectAdapterSpec, it's below 10 lines.
License
The MIT License
Copyright (c) 2014 Hannes Diercks
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.