npm install nlevelnlevel - node.js odm for leveldb.
It is built on top of levelup.
The main ideas are to split db on sections (like collections in mongodb)
and provide additional operations for this sections including manipulations
with set of objects.
```
npm install nlevel
`js
var nlevel = require('nlevel');
// create db
var ldb = nlevel.db('./mydb', {valueEncoding: 'json'});
// store all sections in one object for convenience
var db = {};
// create value section which associate name with arbitrary value
db.cities = new nlevel.ValSection(ldb, 'cities');
db.cities.put(['Elina', 'Dillon', 'Saundra', 'Harmony'], function(err) {
db.cities.get(function(err, cities) {
if (err) throw err;
// prints our array: [ 'Elina', 'Dillon', 'Saundra', 'Harmony' ]
console.log(cities);
});
});
// create documents section with projections (key order matters)
db.tasks = new nlevel.DocsSection(ldb, 'tasks', {
projections: [
// projection 1
{key: {project: 1, version: 1, assignee: 1, id: 1}},
// projection 2
{key: {assignee: 1, project: 1, version: 1, id: 1}}
]
});
// put all tasks in a batch
db.tasks.put([{
id: 1, project: 'proj 1', version: '1.0.0', assignee: 'bob', done: true
}, {
id: 2, project: 'proj 1', version: '1.0.0', assignee: 'jane', done: false
}, {
id: 3, project: 'proj 2', version: '2.0', assignee: 'bob', done: true
}, {
id: 4, project: 'proj 2', version: '2.0', assignee: 'jane', done: true
}, {
id: 5, project: 'proj 3', version: '0.1', assignee: 'sam', done: true
}, {
id: 6, project: 'proj 3', version: '0.2', assignee: 'sam', done: false
}], function(err) {
if (err) throw err;
// NOTICE: in all calls key order matters
// find task for selected assignee and project (it uses projection 2)
db.tasks.find({
start: {assignee: 'jane', project: 'proj 2'}
}, function(err, tasks) {
if (err) throw err;
// prints [ 4 ]
console.log(tasks.map(function(task) {return task.id;}));
});
// find tasks in specific project and version (it uses projection 1)
db.tasks.find({
start: {project: 'proj 1', version: '1.0.0'}
}, function(err, tasks) {
if (err) throw err;
// prints [ 1, 2 ]
console.log(tasks.map(function(task) {return task.id;}));
});
// get by full key (it uses projection 1)
db.tasks.get({
project: 'proj 1',
version: '1.0.0',
assignee: 'bob',
id: 1
}, function(err, task) {
if (err) throw err;
// prints 1
console.log(task.id);
});
});
`
Value section constructor accepts db, name and returns instance of section
Put value to section accepts
Get value from section
Delete value from section
Documents section stores objects in different projections. Constructor
accepts db, name of section and options, options.projections is akey
list of target projections in which documents will be stored.
Each projection defines by which document will be accessible andvalue
which is the presentation of document for projection. key is anvalue
object of field names and values for them. If value of key field is a
function object will be passed to it and it should return string key
otherwise value for this key from object will be get. If is aid
function it will accept object and should return new object which will be
stored for this projection. Any document should have an unique identifier -
field. Projection keys stores in alphabetical order and you can easilyfind
find documents (their presentations) between [start..end] (see methodid
api). Each document will have one key for each projection because of that
you usually should put field as last for projection. Field order atkey
object (and at find) matters.
Put one or array of documents to the section
Find documents
- params.by - id of projection to use, by default it detects projectionparams.start
using condition (start, end)
- - start keyparams.end
- - end key, by default it equals to params.start (with addedparams.reverse
boundary symbol)
- - a boolean, set to true if you want to go in reverse orderparams.filter
- - function(value) if it returns falsy value document will beparams.offset
excluded from result
- - integer, skip selected documents countparams.limit
- - limit count of documents in resultparams.usingValues
- - (false by default) optimization flag, which can bevalues
set to force of using for some operations which uses keys by default
(e.g. counting)
Count documents using findParams (see find).
Notice: It counts keys (or values) internally (can take long time e.g. on
large dataset)
Get document by full key
Update document by key using modifier which could object of fields and
values to be updated or function which accepts document and returns modified
document
Find documents using findParams (see find) and update them usingmodifier
(see modifier description at update). Count of updatedcallback
documents will be passed to (it could be zero).
document
Delete documents by array of their ids or array of objects with id field
into cloned repository run
```
npm test