level-commit

track the change history of a leveldb instance

level-commit

Track the change history of a leveldb instance.

var level = require('level-test')()
var concat = require('concat-stream')
var db = level('db1', {valueEncoding: 'json'})
var committer = require('level-commit')(db)
 
committer.commit({a: 1}, {user: 'sam'}, function(err){
  if (err) return
  // success! 
})
 
// later 
committer.history(function (errhistory) {
  if (err) return
  // commits are tracked in the history 
  // history = [ 
  //   { 
  //     patch: {a: 1}, 
  //     user: 'sam', 
  //     ts: '1914-05-16T05:44:36.155Z' // when the commit was added 
  //   } 
  // ] 
})
 
db.readStream({start: 'a', end: 'a'}).pipe(concat(function(body){
  // commits alter the database contents 
  // body = [ 
  //   {key: 'a', value: 1} 
  // ] 
}))
 

db is a levelup instance.

  • opts.depth = 0 the depth of key that should be used as a prefix for each property. This is passed to level-objectify
  • opts.separator = '\xff' the string to use to separate sections of the prefix.
  • opts.timestamp = 'ts' the key to store the timestamp on.
  • opts.prefix = 'commit' + opts.separator the string to prefix the commit document's key with. To store commits in a sublevel, prefix with '\xffpath\xff\xffto\xff\xffsublevel\xffcommit'

The callback function will be passed any error and an object that is the result of combining the patches of all commits in order with .applyPatch() from patcher.

patch
The patch to commit. Create one with .computePatch() from patcher.

meta
Key-value pairs to include with the commit. Use any valid object key except patch and opts.timestamp, which will be overwritten by the patch provided in the first argument and the provided setting respectively.

fn(err)
A callback function for when the commit is complete. All writes are completed in a single batch operation in leveldb. A failure here means the commit and all side-effects failed.

The callback function will receive any error and the commit history for this instance in the order it was committed (oldest first).

Pass a function that will receive the meta information about a commit and the batch of operations that are about to occur as well as a callback to invoke with any error and the batch of operations to trigger.

// this batch function splits documents into sublevels 
var committer = require('level-commit')(db, {depth: 1})
 
// note: you must augment your db with `level-sublevel` for this to work! 
committer.batch(function(metabatchcb){
  admin = db.sublevel(meta.ship).sublevel('pegasus.admin')
  open = db.sublevel(meta.ship).sublevel('pegasus.public')
 
  batch.forEach(function (op) {
    op.prefix = op.key.slice(0,1) === 'u' ? admin : open
  })
 
  cb(null, batch)
})
 
data = {
  user: {
    lee: {name: 'Lee Adama'}
  }
  gear: {
    viper1: {serial: 15513}
  }
}
 
committer.commit(data, {user: 'sam', ship: 'battlestar'}, function (err) {
  if (err) return
 
  // db.sublevel('battlestar').sublevel('pegasis.admin') now contains: 
  // [{key: 'user\xfflee', value: {name: 'Lee Adama'}}] 
 
  // db.sublevel('battlestar').sublevel('pegasis.public') now contains: 
  // [{key: 'gear\xffviper1', value: {serial: 15513}}] 
})

MIT