Firestore: Version history of documents

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.


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?