Search code examples
firebaseangularfire2google-cloud-firestore

How to structure versioned data in Firebase Firestore Collection?


I need to version entries to a FireStore collection, and I'm not sure the best way to structure the data so I can load back a list of the latest versions only.

Say we have a collection of Items. Each Item will have it's ID, title, content and a revision number. Each revision will change the content, and bump the rev number.

What is the best way to store the revisions?

  • Each revision as a brand new Item in the collection
    • it would have to have some link back to the parent ID
  • Or an Item contains its own collection, Revisions, each with all the data

How would I get a list of Items that shows ONLY the latest revision?

For option 2 I suppose you could order by dateCreated and limitToFirst(1) ?

But I would have to load the main collection, and then loop each entry and load its latest item - not the end of the word, but feels like there is a better way.


Solution

  • In the end I went with the sub collection of Revisions. The main doc kept a reference to the latest / current revision.

    We simplified the requirements, so we only needed top level data in the lists, not data from the revisions.