Home

Awesome

level-couch-sync

Replicate couchdb data into leveldb in real time with follow. Must be used with sublevel.

Usage

The following example illustrates the simplest use case. It will synchronize couchdb data into a leveldb located at /tmp/level-npm and store the data as (key, value) = (data.id, JSON.stringify(data.doc)), where data is JSON chunks received from the couch.

var levelCouchSync = require('level-couch-sync')
var levelup = require('levelup')
var sublevel = require('level-sublevel')

var db = sublevel(levelup('/tmp/level-npm'))
levelCouchSync('http://isaacs.iriscouch.com/registry', db, 'registry-sync')

If you provide a map/iterator function you can decide for yourself what kind of data your want to persist. An easy way to accomplish this, is to create more sublevels and shove data into them. This example shows how you can store basic package metadata in a sublevel named 'package':

var levelCouchSync = require('level-couch-sync')
var levelup = require('levelup')
var sublevel = require('level-sublevel')

var db = sublevel(levelup('/tmp/level-npm-advanced'))
var packageDb = db.sublevel('package')

levelCouchSync('http://isaacs.iriscouch.com/registry', db, 'registry-sync',
    function (data, emit) {
      var doc = data.doc
      emit(data.id, JSON.stringify({
        name        : doc.name,
        author      : doc.author,
        repository  : doc.repository
      }), packageDb)
    })

Each emit() call adds a (key, value, sublevel) triplet to a batch operation that is executed once the iterator returns, which means you can call emit() many times during each time the iterator is invoked.

levelCouchSync() returns an EventEmitter, which you can attach listeners to. The following example logs package versions and progress to stdout. See the events section for more details.

// ..
var sync = levelCouchSync(url, db, 'registry-sync')

sync.on('data', function (data) {
  console.log(data.id, data.doc.versions && Object.keys(data.doc.versions))
})
sync.on('progress', function (ratio) {
  console.log(Math.floor(ratio*10000)/100 + '%')
})

Run the samples in the example/ folder and try it out! It should work on all systems where levelup can be compiled. If you want to take a closer look at what the data looks like you can use lev, which is an awesome cli tool for viewing any leveldb. All you need is a path to it.

API

The API is very simple and only contain one function.

require('level-couch-sync')(sourceUrl, db, metaDb[, map])

This function returns an EventEmitter instance and has three mandatory arguments and one optional.

Events

level-couch-sync emits various events as the leveldb is syncronized with the couch:

progress bar

you can create a progress bar like used in npmd, just provide a name for the couchdb, and a function that returns a tagline describing the document that was updated.

sync.createProgressBar(name, function (data) {
  return toTagline(data)
})

by default, name is the url of the couchdb instance, and the tagline will be doc._id+'@'+doc._rev

License

MIT