Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync Gateway "Channels" in PouchDB

Is there any support for Couchbase Sync Gateway's "Channels" in Pouch DB? I'd like to be able to have uses see a subset of the overall data and if they create new data to be able to share whom they share it with.

Is that possible with PouchDB? Or would I have to interact with the server directly or use couchbase lite for mobile devices?

like image 451
user3546902 Avatar asked Apr 17 '14 21:04

user3546902


4 Answers

Just a little update: This is now possible, PouchDB (since version V3.4.0) is now compatible with sync gateway.

See tutorial here: http://blog.couchbase.com/first-steps-with-pouchdb--sync-gateway-todomvc-todolite

like image 58
Thiago F. Alencar Avatar answered Oct 08 '22 01:10

Thiago F. Alencar


Here is the solution to make the pouch db client work with Couchbase Sync Gateway over user's channels:

var sync = function () {
    var opts = {
        live: true,
        retry: true,
        //-- from here
        filter: "sync_gateway/bychannel",   
        query_params: {
            "channels": channels
        }
        //-- to here
    };

    database.sync(syncServer, opts);
}

The key here is you just pass the filter & query_params as is, the Sync Gateway anyway has the ability to understand this filter.

like image 45
Maqbool Ahmed Avatar answered Oct 08 '22 00:10

Maqbool Ahmed


PouchDB is modeled after CouchDB, which doesn't have the concept of channels, so there are no plans to implement it in PouchDB.

However, one easy way to solve your problem is to sync PouchDB to a CouchDB, then sync that to Couchbase Sync Gateway. The reason you will need CouchDB as an intermediary is that there are a few issues with direct PouchDB <-> Couchbase Sync Gateway syncing, although hopefully they should be resolved soon (see e.g. this and this).

like image 3
nlawson Avatar answered Oct 08 '22 02:10

nlawson


PouchDB syncing through channels / filtered replication

Here's a concrete example to use channels.

var db = new PouchDB("yep");
db.sync(new PouchDB("http://localhost:4984/beer-sample/"), {
    live: true,
    retry: true,
    filter: "sync_gateway/bychannel",
    query_params: {
        channels: "channel-1,channel-2,channel-3,bar"
    }
})

filter: "sync_gateway/bychannel"

Passes the name of filter to apply to the source documents, currently the only supported filter is "sync_gateway/bychannel", this will replicate documents only from the set of named channels.1

query_params.channels

Instead of passing array we separate them by commas.2

Sync Function Example

And in the Sync Gateway your sync function might look like this (It was my intention to keep the sync function as stupid as possible so at one glance you can understand how we used the channels above in PouchDB):

function sync(doc, oldDoc) {
    if (doc.type == "beer") {
        channel("channel-1");
    } else if (doc.type == "soap") {
        channel("channel-2");
    } else if (doc.type == "sweets") {
        channel("channel-3");
    } else if (doc.type == "bar") {
        channel(doc.type);
    }
}

6 years too late though... But it's better late than never!

like image 2
Miko Chu Avatar answered Oct 08 '22 01:10

Miko Chu