Firestore: Version history of documents

后端 未结 2 1732
清歌不尽
清歌不尽 2020-12-16 03:13

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 co

2条回答
  •  悲&欢浪女
    2020-12-16 03:47

    I can think of it like this. Each offers document will have offerHistoryID as number.

    • You can have a separate root collection for versioned documents of offers(say offers_transactions).
    • Now write an update trigger cloud function on offers document which will have both after and before values of the document.
    • Before doing the doc update, you can write the before values into the offers_transactions along with timestamp and latest historyID.
    • Increment the offerHistoryID by 1 for that offer and update the doc with new values.

    Now you can query the root collection offers_transactions for historic transactions based on your filters. This way you can keep your root collection offers cleaner.

    Thoughts?

提交回复
热议问题