skip to Main Content

I have a collection named "products". In JSON format, it looks like below.

 {
   "title":"Rice Combo",
   "currencySymbol":"Rs",
   "price":2900.0,
   "pid":"1d507a2d-09ae-4c75-a102-e9607eedcf9b",
   "dOption":"Non Veg",
   "category":"Mains",
   "categoryID":"103e8154-813f-4631-9605-6db9d3336039",
   "addOnsList":[
      
   ],
   "unitCharts":[
      
   ],
   "combos":[
      {
         "comboItems":[
            {
               "imageName":"scaled_1000000004.jpg",
               "cost":300.0,
               "dOption":"Non Veg",
               "description":"chicken wings",
               "currencySymbol":"Rs",
               "isActive":true,
               "title":"6 Chicken Wings",
               "combo_id":"4c797685-9642-4bdc-9270-ca61c38298ea",
               "dateUpdated":"2023-11-09 04:48",
               "imgUrl":"https://firebasestorage.googleapis.com/v0/b/sky-suite-services.appspot.com/o/combo%2Fscaled_1000000004.jpg?alt=media&token=db27d0be-0072-4890-ab20-1456a6fefbee",
               "uid":"8sKiWSfP12Omqgl8zHUUYaAjnvy1",
               "dateCreated":"2023-11-09 04:40",
               "price":600.0,
               "currency":"LKR"
            },
            {
               "imageName":"scaled_1000000003.jpg",
               "cost":180.0,
               "dOption":"Vegan",
               "description":"bowl of white rice",
               "currencySymbol":"Rs",
               "isActive":true,
               "title":"Rice Bowl",
               "combo_id":"63994746-fdc5-4931-a64d-b224eb6b0e0e",
               "dateUpdated":"2023-11-09 04:55",
               "imgUrl":"https://firebasestorage.googleapis.com/v0/b/sky-suite-services.appspot.com/o/combo%2Fscaled_1000000003.jpg?alt=media&token=a1b6bce0-7101-41bd-abed-cb53a7da4ed1",
               "uid":"8sKiWSfP12Omqgl8zHUUYaAjnvy1",
               "dateCreated":"2023-11-09 04:55",
               "price":300.0,
               "currency":"LKR"
            }
         ],
         "description":"Select Side Dish",
         "id":"a9b56aa0-b463-44e6-9387-2d39b79a5214"
      }
   ]
}

The object inside the "comboItems" comes from a different document in "combos" collection. When a product is created a combo is added inside the "comboItems" which takes data from the "combos" collection. It looks like below.

{
               "imageName":"scaled_1000000004.jpg",
               "cost":300.0,
               "dOption":"Non Veg",
               "description":"chicken wings",
               "currencySymbol":"Rs",
               "isActive":true,
               "title":"6 Chicken Wings",
               "combo_id":"4c797685-9642-4bdc-9270-ca61c38298ea",
               "dateUpdated":"2023-11-09 04:48",
               "imgUrl":"https://firebasestorage.googleapis.com/v0/b/sky-suite-services.appspot.com/o/combo%2Fscaled_1000000004.jpg?alt=media&token=db27d0be-0072-4890-ab20-1456a6fefbee",
               "uid":"8sKiWSfP12Omqgl8zHUUYaAjnvy1",
               "dateCreated":"2023-11-09 04:40",
               "price":600.0,
               "currency":"LKR"
            },

Now my concern is when data inside a document in "combos" collection is changed if it is used to create a product earlier, "comboItems" data in that document must be changed. How can I fix this? I use Flutter for frontend development.

2

Answers


  1. You can update everything by using firebase query like:
    FirebaseFirestore.instance.collection("data").doc().update{
    "item_name":"update data",
    }

    Login or Signup to reply.
  2. You can use the Cloud Firestore trigger https://firebase.google.com/docs/functions/firestore-events?gen=2nd with Cloud function so on combos collection update, you will put a code to update all the products that use the combo.

    It will look like this:

    exports.updatecombo = onDocumentUpdated("combos/{comboId}", (event) => {
    
    // perform your operations ...
    

    });

    But this is not the right thing to do in your case because it will perform many operations and the Firebase bill could be high.

    The right thing to do is to put the document ID in combo items, in your front-end code you can get the value of all items using queries, with that you will not need to update anything when a combo has been updated.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search