Firebase-实现可撤销的.update()的正确方法? [英] Firebase - right way to implement an undoable .update()?

查看:129
本文介绍了Firebase-实现可撤销的.update()的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习Firebase,并注意到任何操作都只是替换数据,这与我在沙发上看到的数据有些不同。



了解他们的文档,为了实现回滚/撤消 .update()操作的目的,我将使用 .push()方法。想法是保留两个版本的数据,以便我可以查询先前的对象。要启动,将需要一种FILO方法,这样我就不会存储超过2个项目。



在首次写入之前的数据库:

  myData 
+ someObject
-keyOne:valueOne
-keyTwo:valueTwo

第一次写入后的数据库 ref.child(someObject).update({keyOne:valueOneUpdated})

  myData 
+ someObject
-keyOne:valueOneUpdated
-keyTwo:valueTwo

上面的结果不是理想的结果,因为我丢失了该项目的先前版本。



第一次写入后的数据库 ref.child(keyOne).push({keyOne:valueOneUpdated})

  myData 
+ someObject
+ keyOne
+ 1k34jh23kj4h:valueOneUpdated
+ 237we8sjkdf8:valueOne
-keyTwo:valueTwo

好!
因为没有 pop()方法可以删除此类操作的最后一项,所以我现在不得不以某种方式找出哪个是第一个



当然必须有一些优雅而直接的方法来使回滚成为可能,不是吗?我错过了什么?



谢谢。

解决方案

如果目标是

  myData 
someObject
keyOne
currentValue:valueOneUpdated
PriorValue:valueOne
键两个
currentValue:valueTwo

如果PriorityValue节点存在,则允许撤消,并将其值移动到currentValue,然后删除先前的值。



您可以通过添加多个撤消来对此进行扩展通过在Firebase中利用数组

  myData 
someObject
keyOne
currentValue:valueOneUpdated
PriorValues:
0:优先值_3 //紧接valueOneUpdated的值
1:优先值_2
:优先值_1

(从概念上讲)最近的priorValue位于索引0,因此请阅读它,将其弹出堆栈,然后将该值写入currentValue并移动其他值。



实际上没有函数可以在Firebase中移动数组中的值,因此只需通过代码读取数组,删除第0个元素并写入数组即可



我一般都不是Array在Firebase中的忠实拥护者,因此还有其他更可扩展的选择,但数组可能适合此特定用例。 / p>

I'm just learning Firebase and noticed that any operation simply replaces the data, which is a bit different than what I've seen on couchDB.

In my understanding of their documentation, for purposes of making a rollback/undo of an .update() operation possible, I would use the .push() method. The idea would be to keep 2 versions of the data, so that I can query for the previous object. TO boot, there would need to be a FILO kind of approach so I'm not storing more than 2 items.

DATABASE BEFORE FIRST WRITE:

myData
+ someObject
  - keyOne : valueOne
  - keyTwo : valueTwo

DATABASE AFTER FIRST WRITE ref.child(someObject).update({keyOne:valueOneUpdated}):

myData
+ someObject
  - keyOne : valueOneUpdated
  - keyTwo : valueTwo

The above is NOT the desired result, since I lost the previous version of the item.

DATABASE AFTER FIRST WRITE ref.child(keyOne).push({keyOne:valueOneUpdated}):

myData
+ someObject
  + keyOne
    + 1k34jh23kj4h : valueOneUpdated
    + 237we8sjkdf8 : valueOne
  - keyTwo : valueTwo

Yuck! As there is no pop() method to remove the last item of such an operation, I'm now faced with having to somehow figure out which is the first and which is the last item.

Surely there must be some elegant and straightforward way to make rollback possible, no? What am I missing?

Thanks.

解决方案

If the goal is to have a single level of undo, it's pretty straightforward.

myData
  someObject
    keyOne
      currentValue: valueOneUpdated
      priorValue: valueOne
    keyTwo
      currentValue: valueTwo

If the priorValue node exists then allow the undo, and move it's value to the currentValue and then remove prior value.

You could expand on this with adding multiple undos by leveraging an array in Firebase

myData
  someObject
    keyOne
      currentValue: valueOneUpdated
      priorValues: 
           0: prior_value_3 //the value just prior to valueOneUpdated
           1: prior_value_2
           2: prior_value_1         

(conceptually) The most recent priorValue is at index 0 so read it, 'pop' it off the stack and write that value to currentValue and 'move' the other value's up.

There isn't actually a function to 'move' values in an array in Firebase around so just read the array in via code, remove the 0th element and write the array back out.

I am not a huge fan of Array's in Firebase in general so there are other, more expandable options, but an array may fit this specific use case.

这篇关于Firebase-实现可撤销的.update()的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆