Sync Gateway "Channels" in PouchDB
Asked Answered
S

4

6

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?

Sexlimited answered 17/4, 2014 at 21:5 Comment(0)
C
4

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

Calebcaledonia answered 27/4, 2015 at 11:14 Comment(0)
B
4

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.

Berhley answered 18/8, 2016 at 10:36 Comment(6)
what If a channel on a user change?Brock
@Jedi: It will have the same behaviour as Couchbase Lite wherein the local database will start getting the data assigned for new channels but then the catch is you will still have the previously synced documents which is not desired. So, to keep it clean & tidy what we do is clean the local database & resync which will get only new channels data.Berhley
@MaqboolAhmed,@Brock here channels value of key "channels" in query_params is a string or array of strings?Consecration
@K Pal: Array of stringsBerhley
@KPal per the couchbase lite documentation, channels is a comma-separated list.Rebuke
@StCleezy: You are right. However, the array of strings should also work as it gets converted to a comma separated list in the query string. By no means discouraging a string here, I personally would prefer an array as it's a better approach to represent multiple strings.Berhley
M
3

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).

Maddeu answered 17/4, 2014 at 21:59 Comment(0)
O
2

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!

Overstretch answered 24/7, 2020 at 4:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.