How am I able to undo un-synced changes to the database?
A use case scenario
I want to give the user the possibility to undo a database operation (i.e. deletion) for at least a few seconds after he does one.
A possibility would be to hold on for the deletion from the database until the time to undo it passes, however I think it would be more streamlined to reflect in the code what I'll see in the UI, just to keep things 1:1.
So, I tried storing the object before deletion and then updating it (so that its _status
wouldn't be deleted anymore):
this.lastDeletedDoc = this.docs[this.lastDeletedDocIndex];
// remove from the db
this.documents.delete(docId)
.then(console.log.bind(console))
.catch(console.error.bind(console));
// ...
// user taps "UNDO"
this.documents.update(this.lastDeletedDoc)
.then(console.log.bind(console))
.catch(console.error.bind(console));
but I got the error Error: Record with id=65660f62-3eb1-47b7-8746-5d0b2ef44eeb not found
.
I also tried creating the object again with:
// user taps "UNDO"
this.documents.create(this.lastDeletedDoc, { useRecordId: true })
.then(console.log.bind(console))
.catch(console.error.bind(console));
but I get an Id already present
error.
I also skimmed trough the source code quickly, but couldn't find any undo
functions.
How do I generally undo changes to an un-synced kinto collection?
As such, you should be able to find the record back and set its
_status
to its old previous version, like you are doing.The problem lies in the fact that the
get
method takes anincludeDeleted
option, which allows you to retrieve deleted records, but theupdate
method doesn't pass it this option.The best way to fix this is probably to open a pull request on the Kinto.js repository, making the
update
method accept anincludeDeleted
option, that it would pass to theget
method.Due to a limited connection right now I cannot push the change, but it would look basically like this (+ a test which demonstrates this works properly):
Don't hesitate to submit a pull request with these changes, I believe that should solve your problem!