Detecting a specific change event based on a difference between the latest version of a document and the previous one

32 views
Skip to first unread message

Sebastien ARBOGAST

unread,
Aug 18, 2014, 1:00:34 PM8/18/14
to mobile-c...@googlegroups.com
Hi,

In my database I have a parking session document that looks like this:

{
  "latitude": 50.856134,
  "licensePlate": "TSA-214",
  "longitude": 4.356331,
  "plannedEndDate": "2014-08-16T18:00:34.000Z",
  "registeredDevices": [
    "I7zyZjpICMYSNK6huVgTBulMsicI8++mv2kk8K5c7BA="
  ],
  "startDate": "2014-08-15T16:40:23.329Z",
  "type": "parkingSession"
}

Where registeredDevices contains a like of APNS device tokens to which I want to send push notifications when certain changes happen to this document.

For example, when an endDate field is added to this document, it means the parking session is over and I need to notify all the devices that registered to this document.

How can I do that when I track the changes feed on the sync gateway using something like Ektorp for example? Is there a proper way to get the previous revision of a document so that I can compare it with the current one and see that an endDate field has been added?

Jens Alfke

unread,
Aug 19, 2014, 1:31:09 PM8/19/14
to mobile-c...@googlegroups.com

On Aug 18, 2014, at 10:00 AM, Sebastien ARBOGAST <sebastien...@gmail.com> wrote:

How can I do that when I track the changes feed on the sync gateway using something like Ektorp for example? Is there a proper way to get the previous revision of a document so that I can compare it with the current one and see that an endDate field has been added?

First you need to get the revision history by issuing "GET /db/docid?revs=true". The response will include a "_revs" property that's a list of revision IDs; the second one will be the parent revision. Then "GET /db/docid?rev=parent" where parent is the parent revision ID.

In the future we may add a callback to the sync function that lets it trigger external web-hooks; then you could code your sync function to call a hook when the endDate field was added.

—Jens

Sebastien ARBOGAST

unread,
Aug 19, 2014, 1:35:55 PM8/19/14
to mobile-c...@googlegroups.com
And it that OK to use those revisions in a business feature like that. I though I read somewhere that document revisions were designed for sync gateway to handle conflicts and they should not be used for functional purposes like that?

Also, where can I find documentation on how to track document changes on Sync Gateway using Java, and possibly hide those REST requests behind library methods?

---
Sébastien Arbogast
http://sebastien-arbogast.com


--
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/313DADC7-6563-40E2-97B0-A6A573D7DB48%40couchbase.com.

For more options, visit https://groups.google.com/d/optout.

Jens Alfke

unread,
Aug 19, 2014, 2:05:26 PM8/19/14
to mobile-c...@googlegroups.com
On Aug 19, 2014, at 10:35 AM, Sebastien ARBOGAST <sebastien...@gmail.com> wrote:

And it that OK to use those revisions in a business feature like that. I though I read somewhere that document revisions were designed for sync gateway to handle conflicts and they should not be used for functional purposes like that?

No, they're also used for purposes like letting apps resolve conflicts. Revisions do get deleted during compaction, but it's safe to get the previous revision shortly after a change is made.

Also, where can I find documentation on how to track document changes on Sync Gateway using Java, and possibly hide those REST requests behind library methods?

Look for a CouchDB client library for Java. Ektorp is the one that I know of.

—Jens
Reply all
Reply to author
Forward
0 new messages