Time-stamping documents in sync gateway

Is it possible to automatically add a time-stamp field to all documents at the point they are saved/updated on the server?

I notice in the documents saved in the sync gateway there’s a field added called time_saved, but I don’t think there’s a way to access this field from the client, correct?

Is there a mechanism (similar to creating views perhaps), where I can inject a javascript function into my server or sync gateway that mutates every document as they come in and adds this field?

Hi, you can’t modify any documents in the sync function. You have to do that on the client side.

To elaborate on what Laurent said: Documents are only modified through creating revisions. Replication doesn’t create document revisions, it only copies them from one database instance to another. Since the sync function runs during replication, it can’t modify the revision that’s being added by the client.

The time_saved property in the _sync metadata in SG isn’t part of the revision, it’s just used internally by SG. It might be feasible to expose this through an SG REST API, if there were compelling use cases for it. Could you describe what you want this for?

(Remember that the time the revision was pushed to SG is not the same as the time the revision was created on the client. If the document is created while the client is offline, or if the client isn’t replicating continuously, there could be an arbitrarily long delay before the doc is pushed. What often makes the most sense is to have the client app add the timestamp when it saves the document.)

Thanks for the explanation. Understood.

The time_saved field is actually useful to me, despite the fact its not the client time. I want both.

I’ve written a web hook that automatically adds a server time-stamp to every document (that needs it) in order to solve my problem.

OK, but be aware that the timestamped revisions will get synced back to all the clients, so this increases your overall bandwidth (it doubles it for those docs that get timestamped.)