Is it possible to post a doc that has a ttl via the sync gateway rest apis?


#1

I want to generate a temp mobile number and code doc that expires in 5 minutes. I’ll text the user the code, and they will need to enter it and send another request to verify their number. Is it possible to set ttl / expiry times on docs via sync gateway rest apis? I know you can do it for sessions, but the docs aren’t clear if you can do it for standard docs. Cheers.


#2

Hi @derekdon,

I don’t think it’s possible to set a TTL property on documents in the context of CBMobile.
You could create a view on SG that emits the documents by their created_at property.
And query that view with the appropriate start_key and end_key parameter in the query string to periodically remove the documents no longer needed.

James


#3

Hey @jamiltz,

Cool thanks for letting me know. Is that the recommended approach for such cases when you only need a short lived document? Or should I just use the node sdks bucket.upsert with expiry via an express api which my app also has access to…? I would rather do all db actions via sync gateway if possible, but perhaps in this case (temp doc) it doesn’t matter.

Derek.


#4

Yes I’d recommend doing all writes through SG and using the view on SG to clean up old docs.

You could also setup a webhook and post the document to an app server to delete the document after a certain amount of time. Instead of periodically calling a map/reduce view on SG, you’d rely on another system to know when to delete a document.

This approach is less setup on the SG side as you just need to add the webhook in the config file (no views required) but it would be more work on the app server side.

@adamf Is there another way of having a TTL-like behaviour with SG?

James