0

如何撤消对数据库的未同步更改?

用例场景

我想让用户在他执行一个数据库操作后至少几秒钟内撤消数据库操作(即删除)的可能性。

一种可能性是保留从数据库中删除,直到撤消它的时间过去,但是我认为在代码中反映我将在 UI 中看到的内容会更加简化,只是为了保持 1:1 .

所以,我尝试在删除之前存储对象,然后更新它(这样_status它就不会被删除了):

 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));

但我得到了错误Error: Record with id=65660f62-3eb1-47b7-8746-5d0b2ef44eeb not found

我还尝试使用以下方法再次创建对象:

// user taps "UNDO"
this.documents.create(this.lastDeletedDoc, { useRecordId: true })
   .then(console.log.bind(console))
   .catch(console.error.bind(console));

但我得到一个Id already present错误。

我也快速浏览了源代码,但找不到任何undo 功能。

我通常如何撤消对未同步的 kinto 集合的更改?

4

2 回答 2

1

因此,您应该能够找回记录并将其设置_status为以前的旧版本,就像您正在做的那样。

问题在于该get方法带有一个includeDeleted选项,该选项允许您检索已删除的记录,但update方法没有将这个选项传递给它

解决此问题的最佳方法可能是在 Kinto.js 存储库上打开一个拉取请求,使该update方法接受一个includeDeleted选项,该选项将传递给该get方法。

由于现在连接有限,我无法推送更改,但它看起来基本上是这样的(+ 一个证明这可以正常工作的测试):

diff --git a/src/collection.js b/src/collection.js
index c0cce02..a0bf0e4 100644
--- a/src/collection.js
+++ b/src/collection.js
@@ -469,7 +469,7 @@ export default class Collection {
    * @param  {Object} options
    * @return {Promise}
    */
-  update(record, options={synced: false, patch: false}) {
+  update(record, options={synced: false, patch: false, includeDeleted:false}) {
     if (typeof(record) !== "object") {
       return Promise.reject(new Error("Record is not an object."));
     }
@@ -479,7 +479,7 @@ export default class Collection {
     if (!this.idSchema.validate(record.id)) {
       return Promise.reject(new Error(`Invalid Id: ${record.id}`));
     }
-    return this.get(record.id)
+    return this.get(record.id, {includeDeleted: options.includeDeleted})
       .then((res) => {
         const existing = res.data;
         const newStatus = options.synced ? "synced" : "updated";

不要犹豫,提交包含这些更改的拉取请求,我相信这应该可以解决您的问题!

于 2016-03-28T20:23:08.347 回答
1

我不确定将“未同步”与“用户可以撤消”结合起来是否是一个好的设计原则。如果您确定您只想撤消删除,那么以这种方式在同步延迟上搭载您的撤消功能是可行的,但是如果将来您想支持撤消更新怎么办?旧值已经丢失。

我认为您应该在您的应用程序中添加一个名为“撤消历史”的集合,您可以在其中存储具有撤消用户操作所需的所有数据的对象。如果您同步此收藏,则甚至可以删除手机上的某些内容,然后从笔记本电脑上撤消该操作!:)

于 2016-03-29T03:14:43.967 回答