make views over kappa-core with leveldb
npm install kappa-view> Base for creating kappa-core views over LevelDB.
A materialized view does two things:
1. A map function, that maps a list of log entries to modifications to a
view. In this case, a LevelDB database.
2. An api object, which are functions and variables that the view exposes in
order to retrieve data from the LevelDB that map writes to.
This module handles view lifecycle logic for you: normally a kappa view needs
to manage storing and fetching the state of the indexer ("up to what log
sequence numbers have I processed?"), as well as purging the view's LevelDB
database when the version of the view gets bumped.
This is a view for a very simple single-value key-value store, that maps log
entries like { key: 'foo', value: 'bar' } to an API that allows get(key)
queries.
``js
var kappa = require('kappa-core')
var makeView = require('kappa-view')
var ram = require('random-access-memory')
var memdb = require('memdb')
var core = kappa(ram, { valueEncoding: 'json' })
var lvl = memdb()
var view = makeView(lvl, function (db) {
return {
map: function (entries, next) {
var batch = entries.map(function (entry) {
return {
type: 'put',
key: entry.value.key,
value: entry.value.value
}
})
db.batch(batch, next)
},
api: {
get: function (core, key, cb) {
core.ready(function () {
db.get(key, cb)
})
}
}
}
})
core.use('kv', view)
core.writer(function (err, log) {
log.append({key: 'foo', value: 'bar'})
log.append({key: 'bax', value: 'baz'})
core.api.kv.get('foo', console.log)
core.api.kv.get('bax', console.log)
core.api.kv.get('nix', console.log)
})
`
outputs
``
null 'bar'
null 'baz'
NotFoundError: Key not found in database [nix]
`js`
var makeView = require('kappa-view')
Create a new view, backed by LevelDB.
Expects a LevelUP or LevelDOWN instance level.
setupFunction is a function that is given parameters db (LevelDB instance)core
and (kappa-core instance). It is called exactly once. A kappa view must
be returned, which is an object with the keys
- map: function (entries, next): receives an array of log entries. Oncedb
you've persisted whatever changes you'd like to , call next() to signalapi: {}
the view is ready for the next batch of log entries.
- : an object that defines API functions that the view exposes. These
can have whatever names you want, be sync or async, and return whatever you'd
like.
With npm installed, run
```
$ npm install kappa-view
ISC