Firestore:文档的版本历史记录 [英] Firestore: Version history of documents

查看:123
本文介绍了Firestore:文档的版本历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种构建Firestore数据库的正确方法,以处理单个集合中文档的多个版本历史记录。



例如:我有一个名为要约的集合,其中有多个文档,它们对应于多个要约。对于这些文档中的每一个,我都希望拥有历史记录的更改,例如Google文档中的更改。



由于文档仅支持直接添加字段或嵌套另一个集合,因此我想到的结构是:

 集合:提供
-文档:offer1,(offer2,offer3,...)
-填充了最新版本的提供内容的字段
-嵌套名为history
的集合-每个版本(v1,v2,v3)的嵌套文档,这些文档又具有用于指定该版本中每个字段状态的字段。

这似乎有点过于复杂,因为我拥有最新的状态,而不是嵌套的历史记录。在平面结构中,数组中的最新项是最新状态,还是类似的状态,这可能是某种原因吗?



此外,单击按钮即可生成历史记录状态,因此我不需要保存在历史记录中的所有可能更改,只需保存用户时的快照即可。



我想将Firebase用作我的数据库,因为我还需要其他一些东西,因此我暂时不考虑其他解决方案。



谢谢!



编辑:根据亚历克斯的回答,这是我的另一种看法。

  Firestore-root 
|
---提供(集合)
|
--- offerID(文档)
| (填充字段)
| |
| --- history(collection)//最后编辑的时间戳记
| |
| --- historyId
| --- historyId
|
--- offerID(文档)
(字段中填充了最新更改)
--- history(collection)//最后编辑的时间戳记
|
--- historyId
--- historyId

这样我可以查询整个提供集合,并获得一系列报价以及最新状态,因为它与集合本身处于同一级别。然后,如果我需要历史记录状态中的特定内容,则可以查询特定商品的历史记录收集并获取其历史记录状态。这有意义吗?



我不确定是否要进行非规范化,因为这似乎解决了我的问题并避免了复杂性。



再一次,要求是:
-能够获取最新状态(工作状态)的所有报价
-能够加载特定历史记录状态(有效)



每当我使用新状态更新历史记录集合时,我都会使用相同的最新状态直接覆盖offerID集合中的字段。



我错过了什么吗?

解决方案

在我看来,您上面的架构可能有效,但是您需要进行一些额外的数据库调用,因为Firestore查询是浅。这意味着Firestore查询只能从查询所针对的集合中获取项目。 Firestore不支持跨不同馆藏的查询。因此,无法通过单个查询获得一个文档以及该文档的集合下托管的相应历史版本。



可能的数据库结构我能想到的,将是使用这样的单个集合:

  Firestore-root 
|
--- offerId(集合)
|
--- offerHistoryId(文档)
| |
| --- //优惠详情
|
--- offerHistoryId(文档)
|
--- //要约详细信息

如果要显示某广告的所有历史版本报价,则需要一个查询。因此,您只需要在 offerId 集合上附加一个侦听器,即可一次性获得所有商品对象(文档)。



但是,如果您只想获取商品的最新版本,则应在每个商品对象下添加 timestamp 属性,并根据降序查询数据库。最后,只需调用 limit(1)就可以了!



编辑:



根据您的评论:


我需要获取一个列表所有报价及其最新数据


在这种情况下,您需要创建一个名为 offers 的新收藏集将保存您提供的所有最新版本。您的新集合应如下所示:

  Firestore-root 
|
---提供(集合)
|
--- offerHistoryId(文档)
| |
| --- date://最后编辑的时间戳记
| |
| --- //优惠详情
|
--- offerHistoryId(文档)
|
---日期://最后编辑的时间戳记
|
--- //优惠详情

这种做法称为非规范化,这是Firebase的常见做法。如果您不熟悉NoQSL数据库,建议您观看以下视频使用Firebase数据库进行正常化是正常的以获得更好的理解。它适用于Firebase实时数据库,但同样的规则也适用于Cloud Firestore。



此外,在复制数据时,需要记住一件事。用与添加数据相同的方式,您需要对其进行维护。换句话说,如果您想更新/删除项目,则需要在它存在的每个地方进行。



在您的特定情况下,当您想要创建一个报价,您需要将其添加到两个位置,一次在您的 offerId 集合中,一次在您的 offers 集合中。创建要约的新历史记录版本后,只需执行一项其他操作。和以前一样,在 offerId 集合中添加 offerHistoryId 文档,在相同的对象中添加您的报价集合,但是在这种情况下,您需要从报价集合中删除旧版本的报价。 / p>

I'm looking for a proper way to structure Firestore database to handle multiple version histories of documents inside a single collection.

For example: I have a collection named offers which have multiple documents which correspond to multiple offers. For each of these documents, I'd like to have history of changes, something like changes on Google Docs.

Since documents support only adding fields directly or nesting another collection, here's a structure I had in mind:

collections: offers
 - documents: offer1, (offer2, offer3, ...)
     - fields populated with latest version of the offer content
     - nested collection named history
         - nested documents for each version (v1, v2, v3), which in turn have fields specifing state of each field in that version. 

This seems a bit overly complicated since I have latest state and than nested collection for history. Can this be somehow in flat structure where latest item in array is the latest state, or something similar.

Also, history state is generated on a button click, so I don't need every possible change saved in a history, just snapshots when user saves it.

I'd like to use Firebase as my DB for this, as I need it some other things, so I'm not looking into different solutions for now.

Thanks!

EDIT: According to the Alex's answer, here's my another take on this.

Firestore-root
   |
   --- offers (collection)
        |
        --- offerID (document)
        |   (with fields populated )
        |        |
        |        --- history (collection) //last edited timestamp
        |            |
        |            --- historyId
        |            --- historyId
        |
         --- offerID (document)
            (with fields populated with latest changes)
                 |
                 --- history (collection) //last edited timestamp
                     |
                     --- historyId
                     --- historyId

This way I can query whole offers collection and get array of offers together with latest status since it's on the same level as the collection itself. Then if I need specific content from history state, I can query history collection of specific offer and get it's history states. Does this make sense?

I'm not sure about denormalization as this seems like it solves my problem and avoids complication.

Once more, requirements are: - being able to fetch all offers with latest state (works) - being able to load specific history state (works)

Just every time I update history collection with new state, I overwrite the fields directly in offerID collection with the same, latest, state.

Am I missing something?

解决方案

In my opinion, your above schema might work but you'll need to do some extra database calls, since Firestore queries are shallow. This means that Firestore queries can only get items from the collection that the query is run against. Firestore doesn't support queries across different collections. So there is no way in which you can get one document and the corresponding history versions that are hosted beneath a collection of that document in a single query.

A possible database structure that I can think of, would be to use a single collection like this:

Firestore-root
   |
   --- offerId (collection)
        |
        --- offerHistoryId (document)
        |        |
        |        --- //Offer details
        |
        --- offerHistoryId (document)
                 |
                 --- //Offer details

If you want to diplay all history versions of an offer, a single query is required. So you just need to attach a listener on offerId collection and get all offer objects (documents) in a single go.

However, if you only want to get the last version of an offer, then you should add under each offer object a timestamp property and query the database according to it descending. At the end just make a limit(1) call and that's it!

Edit:

According to your comment:

I need to get a list of all offers with their latest data

In this case you need to create a new collection named offers which will hold all the latest versions of your offers. Your new collection should look like this:

Firestore-root
   |
   --- offers (collection)
        |
        --- offerHistoryId (document)
        |        |
        |        --- date: //last edited timestamp
        |        |
        |        --- //Offer details
        |
        --- offerHistoryId (document)
                 |
                 --- date: //last edited timestamp
                 |
                 --- //Offer details

This practice is called denormalization and is a common practice when it comes to Firebase. If you are new to NoQSL databases, I recommend you see this video, Denormalization is normal with the Firebase Database for a better understanding. It is for Firebase realtime database but same rules apply to Cloud Firestore.

Also, when you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.

In your particular case, when you want to create an offer you need to add it in two places, once in your offerId collection and once in your offers collection. Once a new history version of an offer is created, there is only one more operation that you need to do. As before, add the offerHistoryId document in your offerId collection, add the same object in your offers collection, but in this case you need to remove the older version of the offer from the offers collection.

这篇关于Firestore:文档的版本历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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