I am starting a project and after reading a lot I have come to the conclusion that I should use Sync Gateway REST API calls from mobile AND server (backend) apps, instead of bucket shadowing. However, Sync Gateway is obviously pretty annoying when it comes to doing querying, especially dynamic querying. My idea is to write a little module for backend applications that will use Sync Gateway for writes and the NodeJS SDK for reads. This way I have the ability to use N1QL, views, etc. for gets/queries (instead of doing awkward things with Sync Gateway) and I can be sure that my data is synced properly when writing.
Is my thinking flawed here? Can anyone foresee any issues with this approach?
There is nothing stopping you using the NodeJS SDK to connect directly to the sync gateway bucket. However, the JSON contents of the bucket are not publicly documented and are likely to change in future releases.
This forum thread from June 2014 gives tacit acknowledgement that this approach is feasible, at your own risk: https://groups.google.com/forum/#!topic/mobile-couchbase/OeTk1fzYJvE
I think the main pattern for interacting with Sync Gateway is for server code to subscribe to channels, and occasionally mutate documents based on the changes it sees. If an occasional map reduce query comes up, in my opinion it's fine to run those against the gateway bucket, and avoid the overhead of bucket shadowing completely. As long as you understand that the Sync Gateway data layout could change in the future. We don't consider the JSON we store in Couchbase Server to be a public API.
Right now you'd want to wrap your map functions like this:
function(doc, meta) { if (doc._sync && !doc._deleted) { emit(doc.foo, null); } }
The approach is to define your views with a filter to exclude sync gateway documents. However, the warning above still applies: sync gateway might change how it represents documents in future releases.
I also found another SO answer which details an N1QL query on a sync gateway bucket: https://stackoverflow.com/a/37036363