Proper collection structure for firestore

Viewed 69

So the app has users (Firebase authentication)

Each user has some settings, and then a collection of json "stuff".

My thought is to have each user be their own root collection, then have a settings doc inside of that, and the also the data collection. With the reasoning being if I have to delete someone and their data it's just deleting the root with everything inside?

Am I thinking about this the right way? I don't want to get 2 years in and need to refactor because it's unmanageable.

1 Answers

You might run into a dead-end if you decide to manage your indexes later, because they require specific names of collections and document fields, and your collections will be named after your users' UIDs.

I ran into similar trouble when I used dynamic document fields, so I had to wrap them into a "myDynamicFields" field, and use that in my indexes.

// firestore.indexes.json
{
  "indexes": [],
  "fieldOverrides": [
    {
      "collectionGroup": "myCollection",
      "fieldPath": "myDynamicFields",
      "indexes": []
    }
  ]
}

Just like I wrapped my dynamic fields in a "Map" field, you could wrap your settings in a userSettings collection, and have a document for each user. The same thing for your "data": a userData collection, and a document for each user. Then, to delete a user, you would have to delete two documents.

Related