level-auto-index
Automatic secondary indexing for leveldb and subleveldown.
npm install level-auto-index

Usage
var level = var AutoIndex = var sub = var keyReducer = AutoIndexkeyReducer var db = var posts = var idx = title: length: tag: // add a title indexpostsbyTitle = // add a length index// append the post.id for unique indexes with possibly overlapping valuespostsbyLength = // Create multiple index keys on an indexpostsbyTag = posts
API
AutoIndex(db, idb, reduce, opts)
Automatically index a db
level into the idb
level using a reduce
function that creates the index key. The db
and idb
levels should be in isolated key namespaces, either by being two different levels or mafintosh/subleveldown
partitions. The db
hook is mutated by hypermodules/level-hookdown
to set up the prehooks used for indexing. Only db
keys are stored as values to save space and reduce data redundancy.
Secondary returns an AutoIndex
level that helps prune old index values, and automatically looks up source documents from db
as you access keys on the AutoIndex
level.
The reduce
functions get the value
of the put
or batch
operations. Make sure that this value
has everything you need to create your index keys.
{ var idxKey = valuefoo + '!' + valuebar return idxKey}
Available opts:
multi: false // Reducer returns an array of keys to associate with the primary key
Multi-key index's are for when you you want to write multiple index entries into an index. This is useful for 'tag' fields, where a document may have n
tags per document, and you would like to index documents by 'tag'. When creating a multi-key index, your reducer must return an array of keys to index by.
AutoIndex#get(key, opts, cb)
Get the value that has been indexed with key
.
AutoIndex#create{Key,Value,Read}Stream(opts)
Create a readable stream that has indexes as keys and indexed data as values.
AutoIndex#manifest
A level manifest that you can pass to multilevel.
AutoIndex.keyReducer(string)
A shortcut reducer for simplistic key indexing. You might need more than this.
{ { return valuereducerString } return keyRdc}
For a higher level api for creating secondary indexes see hypermodules/level-idx.
AutoIndex.keyReducer(string)
A shortcut reducer for simplistic multi-key indexing. You might need more than this.
{ return { if !document || !documentmultiFieldName || !Array return return documentmultiFieldName }
AutoIndex#db
The level instance that we are indexing.
AutoIndex#idb
The level instance that we are using for the index.
See Also
This module is a variant of
but aimed at decoupling the index storage fromt the indexd db and also being compatable with subleveldown. It came out of the work trying to make level-secondary
compatable with subleveldown and level-sublevel. That work lives here: github.com/bcomnes/level-secondary/commit/9b2f914e53.