Snake
Snake

Reputation: 14648

Firestore structure model

Coming from SQL background and watching tutorials, I am trying to do a model in Firestore to understand how things work. I basically wants model a situation where user has multiple lists and every list has his friends ( to display names of friends). Does the below make sense?

Users
    "[email protected]"
         -Name: John Smith

    "[email protected]"
         - Name: Celina West

    "[email protected]"
         - Name: Dan Nelson

Lists
     "[email protected]"
           List_Titles
                  "List 1"
                       - <AutoGenId>: Celina West
                       - <AutoGenId>: Dan Nelson

anything with "-" is a field, anything with bracket it Document and anything without prefixes is collection.

One issue I find here, is that lets say a user updates his/her name. Then I have to go not to only Users Collection but through every subcollection List to look for that person and update name. I thought about using email ID instead of name but then that goes against the "structure the nosql db as you view it" way. Plus then everytime I have to hit the Users tables in a seperate call for every Id to query the name.

Is my assumption correct? Thanks

Upvotes: 0

Views: 607

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Snake, there is no perfect database structure. You need to model your database so you can query very easily later, when you need to do CRUD operations. In one of my tutorials, I have explained step by step how can we structure a Firestore database which holds users, lists and products.

Please see the below database structure that can help achieve what you want.

{
  "users": {
    "[email protected]": {
      "tokenId": "eGVzwv7Y...",
      "userEmail": "[email protected]",
      "userName": "First User"
    },
    "appseconduser@gmail,com": {
      "tokenId": "cc8Uhriu...",
      "userEmail": "[email protected]",
      "userName": "Second User"
    }
  },
  "shoppingLists": {
    "[email protected]": {
      "userShoppingLists": {
       "3Oe37QdcHXSohL2dnNlX": {
        "createdBy": "First User",
        "date": "February 3, 2018 at 2:56:31 PM UTC+2",
        "shoppingListId": "3Oe37QdcHXSohL2dnNlX",
        "shoppingListName": "Pharmacy"
    },
    "  WovuleVbTZdql68gXk84": {
        "createdBy": "First User",
        "date": "February 3, 2018 at 2:56:20 PM UTC+2",
        "shoppingListId": "WovuleVbTZdql68gXk84",
        "shoppingListName": "Grocery"
       }
      }
    }
  },
  "products": {
    "WovuleVbTZdql68gXk84": {
      "shoppingListProducts": {
       "8vinaHJyjG4JqFH33YE7": {
        "productId": "8vinaHJyjG4JqFH33YE7",
        "productName": "Milk"
    },
      "JALygtedMHWQcdEoSnPM": {
       "productId": "JALygtedMHWQcdEoSnPM",
       "productName": "Eggs"
    },
      "WFkJMWZSnhJU9iwGeoOi": {
        "productId": "WFkJMWZSnhJU9iwGeoOi",
        "productName": "Bacon"
       }
      }
    }
  }
}

Using a database structure that looks like this, you'll be abte to create, read, update and delete records very easily.

Upvotes: 2

Related Questions