Skip to content

Base for creating kappa-core materialized views over LevelDB.

Notifications You must be signed in to change notification settings

kappa-db/kappa-view

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kappa-view

Base for creating kappa-core views over LevelDB.

Materialized Views

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.

Example

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.

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]

API

var makeView = require('kappa-view')

var view = makeView(level, setupFunction)

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) and core (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. Once you've persisted whatever changes you'd like to db, call next() to signal the view is ready for the next batch of log entries.
  • api: {}: 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.

Install

With npm installed, run

$ npm install kappa-view

License

ISC

About

Base for creating kappa-core materialized views over LevelDB.

Resources

Stars

Watchers

Forks

Packages

No packages published