Back

Update Doc If Newer

  • 0
  • Databases
  • Functions
alb
22 Mar, 2023, 12:38

I'm trying to keep docs synced with a local database for an offline capable app. It would greatly simplify things if I could do myDB.updateDocument(myDoc) and the doc is only updated if it is newer than the one currently in the collection.

I've tried a couple of things and my best guess is that I need to write a cloud function that takes a doc as a parameter, gets the doc (with the same id) from the collection, and compares a date attribute.

Is this a sensible way of doing it? Any better ways or best practices I should know about? Any help would be greatly appreciated.

TL;DR
The user is seeking advice on how to update a document in a collection only if it is newer than the existing one. They propose writing a cloud function to compare a date attribute. The response suggests that adding a timestamp locally may be a better option and discusses the benefits of using a cloud function for future flexibility. No solution is given.
Binyamin
22 Mar, 2023, 12:41

The way you've suggested seems like the best way to my opinion to tackle this can of action.

The benefit of using it through a cloud function is that in the future you can easily add logical Middleware in the process

alb
22 Mar, 2023, 12:44

Thanks for your quick reply. It's good to know I'm on the right track.

ireneus
22 Mar, 2023, 13:04

adding the timestamp locally should be a better option?

Binyamin
22 Mar, 2023, 13:05

Sure you have them both in the updatedat field

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more