Another indexing solution for leveldb. Adapted from map-reduce. The API is unstable because this module is a by-product of refactoring an ugly ORM.
Jump to: api / install / license
// create your database
var sublevel = require('level-sublevel')
, level = require('levelup')
, db = sublevel(level('./data'), {valueEncoding: 'json'})
// install
require('level-map-index')(db)
// create indices
db.index('title')
db.index(['author.name', 'price'])
// a custom index (should return an array)
db.index('custom', function map(key, value){
return [value.title.indexOf('f')]
})
var author = { name: 'bob' }
var book = { title: 'foo', price: 10, author: author }
db.put('foo', book, search)
function search() {
// streams will wait for indexing to complete
// every book by bob, ordered by price
db.streamBy(['author.name', 'price'], 'bob').pipe(somewhere)
// every book by bob with a price of 10
db.streamBy(['author.name', 'price'], ['bob', 10]).pipe(somewhere)
// ordered by title
db.streamBy('title').pipe(somewhere)
// use sublevel hooks
db.index('custom').post(function(op){
// op.key is the array we returned from our `map` fn above
if (op.type=='del' && op.key[0]===0) {
console.log('no more titles starting with "f" in our db')
}
})
// this will (eventually) trigger the above post hook
db.del('a')
}
Install the plugin.
Create an index.
Likely to change, no docs yet.
Manually trigger a rebuild. A complete
event fires when it's done.
With npm do:
npm install level-map-index