如何撤消更改为未同步集合



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

用例场景

我想让用户在完成数据库操作(即删除)后至少几秒钟内可以撤消数据库操作。

一种可能性是保留从数据库中删除的内容,直到撤消它的时间过去,但我认为它会更精简,以在代码中反映我将在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));

但是我得到了错误CCD_ 2。

我还尝试再次创建对象:

// 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集合的更改?

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

问题在于get方法采用了includeDeleted选项,该选项允许您检索已删除的记录,但update方法没有将此选项传递给它。

解决此问题的最佳方法可能是在Kinto.js存储库上打开一个pull请求,使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";

不要犹豫,提交一个带有这些更改的拉取请求,我相信这应该能解决你的问题!

我不确定将"未同步"与"用户可以撤消"相结合是否是一个好的设计原则。如果你确信你只想撤消删除,那么以这种方式将撤消功能附加到同步延迟上是可行的,但如果将来你想支持撤消更新呢?旧的价值已经丢失了。

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

最新更新