I have multiple users, from different companies, using my android application. I’m currently switching to Couchbase for my storage needs.
I have some questions about the synchronization. The documents that should sync to the user must be dependent on which company it belongs to. For example, I don’t want a user belonging to company A to sync data belonging to company B.
Is it possible to add a sync function where the function matches the unique company ID on the user, and only syncs documents with the same company ID on them? See the following example:
"sync": `
function(doc, oldDoc, user) {
if(doc.companyId == user.companyId)
{
// Do the synchronization....
}
}
`
If this doesn’t work, what is the recommended solution for me?
so when the cmp document updated(created),you can assign ["user1","user2"] to companyId-1 channel using access(user, channel) function in sync function;
This means every Company document must include a list of all users? I thought I could specify what document to sync by including a companyId and then checking the companyId on the user for a match. Could you clarify this?
you also can add Company list into user documents(or other documents), when this document updated, you can call access() function to assign the Company channel to user. at least you should add mapping relationships into some documents, and then you can call access() function with CompanyId and userId as parameters of access function.
Either way, what you’re saying is that I must include a list of the referencing object in the document? (i.e. the list of the companies in the user document, the list of users in the company document, or other in this fashion). It’s a bit odd if the user document must include companies it doesn’t belong to (although, I know this was just an example).
I am just trying to interpret the answer I was given.
Another scenario I’ve been thinking of is to divide the companies with the help of natural keys (e.g. “company1::product…”). Is it possible to regulate access in this way? I haven’t found much documentation on this.
Edit:
So we’ve played around with this and thought we could do something like this:
…
=> channel(“company.” + doc.companyId);
We would then do the rest of the accesscontrol by requireRole(…). This scenario would suit our needs.
Can the channels be dynamic? I.e. do we need to physically create each channel in the SG config file, by adding each company as a channel manually? It would be preferable with a dynamic solution, since new companies could potentially be added each week.
Yes. You can use any key you want that suits your application needs. In your sync function, you can parse the key to extract the company Id and use it appropriately just as you would use any property value.
OK. I think that’s what Atom was suggesting when he posted “then route documents to this companyId channel.”
When you use the channel method to route a document to a channel, you are implicitly “creating” the channel. This is during run time. If you add a new company, depending on your data model, you are likely to have company specific documents. When these documents gets processed by the sync function, it will be assigned to the companyId channel. The first time a document is assigned to the company channel , it will automatically bring the channel to existence.
Note that on the client side, you will have to set your replicator to filter by appropriate channel.