I’m setting up Sync Gateway and running into two major issues with my sync function, which I’ll paste here for reference:
function (doc, oldDoc) {
if (doc._deleted) {
return; //temporary.
}
if (!doc.type)
throw({forbidden: 'All documents must have a type'});
if (doc.type === 'organization') {
// requireAccess(doc._id);
if (doc.members) access(doc.members, doc._id);
channel(doc._id);
}
if (doc.type === 'list' || doc.type === 'item') {
// requireAccess(doc.organization);
channel(doc.organization);
}
}
The first and most pressing is that users don’t appear to be assigned channels correctly. I have an Organization document that has a members property that contains SG usernames. The sync function I wrote grants access to a channel define by the organization’s ID.
if (doc.type === 'organization') {
if (doc.members) access(doc.members, doc._id);
channel(doc._id);
}
When I view the organization document from within the SG Admin UI, I can see that it grants access to user X, but when I view that user I don’t see the organization’s channel listed.
What’s most frustrating is that this happens inconsistently. In my test database, I have 3 organizations that user X is a member of, but the user only has access to 1 channel.
Issue #2 is that I have a user who has access to channel “*”, which I assumed meant that they had access to every channel. But when I add requireAccess calls to my sync function, that user can no longer see any data.
So I haven't retested Issue #2, but I was able to resolve Issue #1 by hitting POST /_resync on the admin port.
It's not very obvious that that is a required step after changing the sync function in a way that alters permissions. For example, it's not mentioned in the Sync Function API documentation at all (http://developer.couchbase.com/mobile/develop/guides/sync-gateway/sync-function-api-guide/index.html). I notice now that when I change the sync function and restart the server that there's a little blurb outputted into the error log. I'm assuming you don't automatically resync on launch because of the performance implications?
On a related note, when one changes the sync function using the Admin UI, is it correct to assume that the new sync function is used while the server is running and that the server reverts back to the sync function given in config.json upon restart? Is it necessary to hit POST /_resync after changing the sync function in the Admin UI?
On Jan 1, 2015, at 7:59 PM, vf <vladimir...@gmail.com> wrote:It's not very obvious that that is a required step after changing the sync function in a way that alters permissions. For example, it's not mentioned in the Sync Function API documentation at all (http://developer.couchbase.com/mobile/develop/guides/sync-gateway/sync-function-api-guide/index.html).
I notice now that when I change the sync function and restart the server that there's a little blurb outputted into the error log. I'm assuming you don't automatically resync on launch because of the performance implications?
On a related note, when one changes the sync function using the Admin UI, is it correct to assume that the new sync function is used while the server is running and that the server reverts back to the sync function given in config.json upon restart?