Using Sync function for simple business logic?

67 views
Skip to first unread message

Marc Nicholas

unread,
Nov 17, 2014, 9:04:47 PM11/17/14
to mobile-c...@googlegroups.com
Hi,

Is it a valid (and possible) pattern to embed user-specific, simple business logic in the Couchbase Sync function? I guess that the notification portion of the Couch Chat app is somewhat going in this direction.

What I'd like to do is compare a value in an incoming document to a set of rules gleaned from a document stored in an existing document (working through them as an array) and then do something if the rules match. We're talking about very simple 'if value X is > Y" type rules here.

Possible? Valid? Any thoughts on how to go about this?

Cheers,

-m

Jens Alfke

unread,
Nov 18, 2014, 1:00:19 AM11/18/14
to mobile-c...@googlegroups.com

On Nov 17, 2014, at 6:04 PM, Marc Nicholas <ma...@wimoto.com> wrote:

What I'd like to do is compare a value in an incoming document to a set of rules gleaned from a document stored in an existing document (working through them as an array) and then do something if the rules match. We're talking about very simple 'if value X is > Y" type rules here.

The answer is no, if I understand you correctly. The sync function deliberately operates only on single documents. There is no way for it to look at another document. Any rules it applies have to be hardcoded into the sync function itself.

The main reason for this is that there is no notion of an atomic transaction in this sort of distributed system, so you can't enforce constraints that depend on multiple documents.

—Jens

Traun Leyden

unread,
Nov 18, 2014, 8:50:58 AM11/18/14
to mobile-c...@googlegroups.com
Have you looked into having a process that listens on the sync gateway changes feed?

On Nov 17, 2014, at 6:04 PM, Marc Nicholas <ma...@wimoto.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/e2a157a0-06b3-4de7-900b-78b78a41d647%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marc Nicholas

unread,
Nov 18, 2014, 9:44:38 AM11/18/14
to mobile-c...@googlegroups.com


On Tuesday, 18 November 2014 01:00:19 UTC-5, Jens Alfke wrote:

On Nov 17, 2014, at 6:04 PM, Marc Nicholas <ma...@wimoto.com> wrote:

What I'd like to do is compare a value in an incoming document to a set of rules gleaned from a document stored in an existing document (working through them as an array) and then do something if the rules match. We're talking about very simple 'if value X is > Y" type rules here.

The answer is no, if I understand you correctly. The sync function deliberately operates only on single documents. There is no way for it to look at another document. Any rules it applies have to be hardcoded into the sync function itself.

Thanks, Jens....it did start to seem that the Sync function is only for transitional documents, but I appreciate you clarifying that it's the case :)

-m 

 

Marc Nicholas

unread,
Nov 18, 2014, 9:47:45 AM11/18/14
to mobile-c...@googlegroups.com


On Tuesday, 18 November 2014 08:50:58 UTC-5, Traun Leyden wrote:
Have you looked into having a process that listens on the sync gateway changes feed?

Hi Traun,

Thanks for your response.

Funnily enough, I've trolled through the Couchbase Chat code since posting and will probably take an approach similar to the Notifications feature (which is something I need to also do).

How does one attach to the 'changes feeds'? Is this just a channel that scoops up everything

Cheers :)

-m 

 

Jens Alfke

unread,
Nov 18, 2014, 11:42:17 AM11/18/14
to mobile-c...@googlegroups.com

On Nov 18, 2014, at 6:47 AM, Marc Nicholas <ma...@wimoto.com> wrote:

How does one attach to the 'changes feeds'? Is this just a channel that scoops up everything

Check the SG REST API doc for "_changes". It describes how to configure which channel(s) to watch.

—Jens
Reply all
Reply to author
Forward
0 new messages