0.0.1 • Published 8 years ago

claudia-pouchdb-replication-stream v0.0.1

Weekly downloads
3
License
ISC
Repository
github
Last release
8 years ago

Claudia Pouchdb Replication Stream

This module provides an express endpoint for streaming bulk couchdb changes to pouchdb. It makes use of the great PouchDB Replication Stream module.

Install

Install with npm:

npm i -S claudia-pouchdb-replication-stream

Usage

Basic usage enables streaming of one db to the client:

var ApiBuilder = require('claudia-api-builder');
var repStream = require('claudia-pouchdb-replication-stream');
const api = new ApiBuilder();
module.exports = api;


api.get('/api/couchdb', repStream('http://user:pass@localhost:5984/db'));

Database Name in Request

If you have per-user databases, or you want to get changes from different databases based on the request, this will work:

api.get('/api/couchdb/:db', repStream({
  url     : 'http://user:pass@localhost:5984/',
  dbReq   : true
}));

Filtered Replication

In order to do filtered replication, there are two options. If the parameters are known beforehand, then this method can be used:

api.get('/api/couchdb', repStream({
  url           : 'http://user:pass@localhost:5984/',
  replication   : {
    filter        : 'myFilterName',
    query_params  : {
      prop1       : 'myFilterParameter1'
    }
  }
});

However, if the filter options are also dynamic/based on the request, then the request data must be parsed first:

api.get('/api/couchdb/:filterFunc/:filterBy', function(req){
  var filterFunc = req.pathParams.filterFunc;
  var filterBy = req.pathParams.filterBy;
  repStream({
    url           : 'http://user:pass@localhost:5984/',
    replication   : {
      filter        : filterFunc,
      query_params  : {
        docName     : filterBy
      }
    }
  })(req, res, next);
});

Replication Options

Allowed replication options:

batch_size
batches_limit
filter
doc_ids
query_params
since
view

PouchDB Replication Options CouchDB Replication Options

Error Handling

The default behavior is to send an error response with a 500 error code and the error message. To overwrite this, pass a method to the error option:

repStream({
  url     : 'http://user:pass@localhost:5984/',
  error   : function(err){
    // do what you will with `err` here
    console.log(err);
    res.send(err);
  }
});