Syncing multiple channels dynamically

For our accounts we have parent/child relationships (as well as “shared” data). We would like to have the server to sync the child’s data along with its parent’s data. How would we set this up so the appropriate data would be on the mobile device?

The account relation information is in a relational database, fwiw. Just didn’t know what mechanism to employ.

(Complete newb to Couchbase, just looking to prototype, any pointers appreciated)

There’s nothing in our design that explicitly manages relationships between documents. Generally you create a relationship by adding a JSON property whose value is a string with the other document’s ID, but that’s an informal convention, and there are other ways to do it (like encoding the hierarchy into the docIDs, e.g. via a path naming convention.) It’s up to you.

You’d just need to make sure either that the parent and child documents were assigned to the same channels, or that any user that has access to the channel for a child doc also has access to the channel(s) for its parent(s).

What mechanism do you use to denote that the child account gets it’s channel, along with its parent channel documents?

You have complete control of this using the Sync Gateway sync function. You can probably structure your data the way it’s easiest for you to consume and then derive the channels in the sync function.

For example, you might have a child have a parent id property, at which point you could just check in the sync function for the parent property and assign a channels accordingly. Or maybe you have the child/parent relationship encoded in some way. Maybe your document id encodes the relationship (so you have a property like id = parent.child.uniquevalue). Just split it apart and assign the channels as you like.

In the end you would have two ‘channel(string)’ calls in the sync function, one for the parent channel and one for the child channel.

I appreciate the advice so far.

In my case, its not the documents that have a parent, its the accounts… For example child accounts A, B, C, might have a parent account D. They should be able to see D’s documents as well (but not modify), and obviously the accounts may change over time. For example, E w/o parent, might later become child of D as well.

I think from what is being said, that the information to sync/distribute the documents, must be contained in the document itself? So then I’d be manipulating the documents properties to “adjust their channels” it sounds like.

That’s right. The sync function needs to be a “pure” function, meaning you don’t reference anything external, just the documents passed in as arguments. (Technically, to be more precise, the guarantee must be that the sync function always returns the same output for the same input.) So the decision to assign a doc to a channel has to be based on document contents.

From the documentation:

If a user’s access to a channel is revoked or Couchbase Lite stops syncing with a channel, documents that have already been synced are not removed from the user’s device.

Does this also apply if the list of channels change for a document? For example, I have document marked for channel a & b, but remove later remove b from the list of channels, will it be appropriately removed from synced devices?

No, this only applies when a user loses access to a channel.

If a document is removed from a channel, a removal notification is sent to clients who no longer have access to that document.

Yes, that’s right, with some extra details. If a document is removed from all channels a client has access to, then a special “removed” tombstone gets sent. Of course this happens the first time the client syncs after the removal.

The trick is that, for example, if a document is assigned to channels A and B, and a client has access to both A and B, then removing the document from channel B alone will not remove the document from the client.

It sounds like best practice is to do something like make each user have their own channel (say by user’s uuid), and then per document update the channels property to get the document to the right folks (in my case from the “parent” to its children — just update the team documents with each of the children’s uuids). For us, this would need to scale to up to a few thousand children per customer.

This wouldn’t seem practical for more widely distributed documents, where I’d imagine the thing to do is to have a channel for the document type, and then revoke/grant as needed… Where we could run into the sync issue. Is there a workaround to have say the device detect and “refresh” this situation?

BTW, I’m amazed with the turnaround on asking question here.

One middle ground (between channel-per-user and document type channels) for wide distribution docs would be to have channels-per-role, and have documents move in and out of the role channels. That way users with that role will get the removal notification, without the overhead of moving a document in and out of 1000s of user channels.

There isn’t a built-in workaround for document removal on user channel loss. In some scenarios it’s possible to build your own client-side logic, usually relying on using a user-visible document type for channel access management, and the client application being able to identify which local documents should be purged for that channel.