changes-index
create indexes from a leveldb changes feed
This package provides a way to create a materialized view on top of an append-only log.
To update an index, just change the index code and delete the indexed data.
example
Create a change feed and set up an index. Here we'll create an index for keys
that start with the prefix 'user!'
on the name
and hackerspace
properties.
var level = ;var sublevel = ;var changes = ;var changesdown = ;var chi = ;var argv = processargv;var up = ;var feed = ;var db = ;var indexes =;indexes;
now we can create users:
if argv_0 === 'create'var id = ;var name = argv_1 space = argv_2;var value = name: name hackerspace: space ;;{indexes;{ }{ }}
or clear (and implicitly regenerate) an existing index:
else if argv_0 === 'clear'indexesclearargv_1 {if err console;};
With these indexes we can list users by name
and space
:
else if argv_0 === 'by-name'indexes;else if argv_0 === 'by-space'indexes;
methods
var chi =
var indexes = chi(opts)
You must provide:
opts.ixdb
- levelup database to use for indexingopts.chdb
- wrapped changesdown levelup database to lookup primary recordsopts.feed
- changes-feed handle wired up to the chdb
indexes.add(fn)
Create an index from a function fn(row, cb)
that will be called for each
put and delete. Your function fn
must call cb(err, ix)
with ix
, an object
mapping index names to values. The values from ix
will be sorted according to
the algorithm from bytewise.
indexes.createReadStream(name, opts)
Create a readable object-mode stream of the primary documents inserted into
chdb
based on the index given by name
.
The stream will produce row
objects with:
row.key
- the key name put into changedownrow.value
- the value put into changedownrow.index
- the index value generated by the index functionrow.exists
- whether the key existed prior to this operationrow.prev
- the previous set of indexes ornull
if the key was createdrow.change
- the monotonically increasing change sequence from changes-feed
This read stream can be bounded by all the usual levelup options:
opts.lt
opts.lte
opts.gt
opts.gte
opts.limit
opts.reverse
plus:
opts.eq
which is the same as setting opts.gte
and opts.lte
to the same value.
This isn't common in ordinary levelup but is very common when dealing with
indexes that map to other keys.
indexes.clear(name, cb)
Delete the index for name
, calling cb(err)
when finished.
versioning
The internals of this module may change between patch releases, which may affect how data is stored on disk.
When you upgrade this package over existing data, you should delete the indexes first.
license
MIT