Changing data structure after app has been published - Firestore

Viewed 170

I have just published an app that uses Firestore as a backend. I want to change how the data is structured; for example if some documents are stored in subcollections like 'PostsCollection/userId/SubcolletionPosts/postIdDocument' I want to move all this last postIdDocument inside the first collection 'PostsCollection'.

Obviously doing so would prevent users of the previous app version from writing and reading the right collection and all data would be lost.

Since I don't know how to approach this issue, I want to ask you what is the best approach that also big companies use when changing the data structure of their projects.

1 Answers

So the approach I have used is document versioning. There is an explanation here.

You basically version your documents so when you app reads them, it knows how to update those documents to get them to the desired version. So in your case, you would have no version, and need to get to version 1, which means read the sub-collections to the top collection and remove the sub collection before working with the document.

Yes it is more work, but allows an iterative approach to document changes. And sometimes, a script is written to update to the desired state and new code is deployed . Which usually happens when someone wants it done yesterday. Which with many documents can have it's own issues.

Related