Home > Software engineering >  With these queries, it is possible to achieve this with Firestore without needing Cloud Functions?
With these queries, it is possible to achieve this with Firestore without needing Cloud Functions?

Time:02-04

I'm building an app using Firebase Firestore ReactJS where there will only be 1 user to add orders and manage the inventory.

Manage Inventory

  • CRUD Product
  • CRUD size & quantity (there could be multiple sizes)
  • CRUD color & quantity (there could be multiple colors)

It will also subtract the quantities for the specific product's size and color once an order was placed.

Ordering

  • Choose Product
  • Enter Quantity
  • Choose its sizes and color

For example: There was an order for notebooks and cups in one order. Let's assume that there was already a name, address, and phone number entered. The chosen order will be:

1st item:

  • Notebook

  • 3 pieces

  • 1st piece: Size: small, color: red

  • 2nd piece: Size: medium, color: green

  • 3rd piece: Size: Large, color: blue

2nd item:

  • Cup
  • 2 pieces
  • 1st piece: Size: small, color: red
  • 2nd piece: Size: Large, color: blue

I have this data structure in mind. However, I could not grasp how to structure if there are multiple pieces of the same products and each would have different sizes and colors. In the order collection, for the fields product, sizes, and color, I was thinking of using an array or map. I'm not sure about this yet. Any help would be most appreciated.

https://i.stack.imgur.com/Wzdbo.png

I tried structuring it in Firestore just for an example and I am not sure with the part of the order collection for the field products:

enter image description here

And this is what I came up with the Inventory collection: enter image description here

Are these queries possible in Firestore without needing Cloud Functions? Like those products with their sizes and colors that were placed will also be subtracted in the inventory?

CodePudding user response:

Yes, you can write Firestore queries in your React JS code instead of creating a separate Cloud Functions for it. You can treat it like any other NoSQL database and work with it.

You can also add various data types like an array as you have mentioned in your question as shown in the datatypes section of add data.

You can use set() to add data into the Firestore, and use update() whenever you want to update a field without overwriting the entire document. You can use update() to update the stocks present in the inventory.

You can learn more about adding data into Firestore and updating the data with the help of this Firebase documentation.

  •  Tags:  
  • Related