level-indexer
Generic indexer for leveldb. Only stores document keys for space efficiency.
npm install level-indexer
Usage
var indexer =// create a index (by country)var country = // index by countrycountrycountryvar stream = country// or using the shorthand syntaxvar stream = countrystream
The stored index is prefix with the index key names which means you can use the same levelup instance to store multiple indexes.
API
index = indexer(db, [prop1, prop2, ...], [options])
Creates a new index using the given properties. Options include
{// map find results to another valuedb}
index.add(doc, [key], [cb])
Add a document to the index. The document needs to have a key or provide one. Only the key will be stored in the index.
index.remove(doc, [key], [cb])
Remove a document from the index.
index.key(doc, [key])
Returns the used leveldb key. Useful if you want to batch multiple index updates together yourself
var batch = type:'put' key:index value:dockey ...
stream = index.find(options, [cb])
Search the index. Use options.{gt,gte,lt,lte}
to scope your search.
// find everyone in the age range 20-50 in denmarkvar index =...var stream = index
Optionally you can specify the ranges using arrays
var stream = index
Or if you do not care about ranges
var stream = index// equivalent tovar stream = index
The stream will contain the keys of the documents that where found in the index.
Use options.map
to map the to the document values.
Options also include the regular levelup db.createReadStream
options.
If you set cb
the stream will be buffered and passed as an array.
index.findOne(options, cb)
Only find the first match in the index and pass that to the callbck
License
MIT