No, it’s not possible for the sync function to modify documents (even the incoming document).
There are a few different approaches you might take for the example scenario you mentioned. I think the most common approach would be to use views to perform aggregation operations like count - either in Couchbase Lite or in Couchbase Server.
Use a count view every time I want to know how many books are associated with AuthorX
this requires couchbase to actually do the math on every read request which is an ongoing hit on performance (not a big deal for count, but if it’s a function with more complicated math, this could be significant)
but we know the count is accurate (there’s no chance of deleting BookY and forgetting to update AuthorX.Books)
“Prior to the 1.1 release, the only way to add custom logic in the form of plugins to Sync Gateway was to listen on the changes feed. While this approach still works, it isn’t the easiest to setup. With webhooks, you can simply provide a filter function and the URL to post the event to. Then, you handle the event with the back-end language of your choice (NodeJS, Java, Go, Ruby…).”
I think Webhooks is the way to go. By filtering “changes” you only spend CPU operation on the ones you care about.
What if I want to validate the document based on other documents stored in bucket and reject in sync function? I cant do in webhook or other thing because by then the original document will already be stored.
@avia_tm When doc is inserted, you can have the script on server side to listen to doc changes and update or act based on those changes using admin port.
so in the above you still have to go through the data port in SG.
and in your sync function just make it so that when the doc comes is just put it in the channel.