Back

Relationship Doubt

  • 0
  • Databases
  • General
  • Web
k_vasen
3 Jan, 2024, 17:53

Consider user cart document with relationship with one-to-many products. Obviously Each product should also contain counts based on user choices. My question is how can we integrate count into specific products. Note:- Product should not be updated. Only Cart Document should be updated.

One solution that came to my mind is storing count as array of string {id: productId: count:2}. i.e.

Is there any other better way? With/without relationship?

TL;DR
The user is having doubts about the best way to structure their database for a cart system. They want to keep all the products in one document but also have counts based on user choices. One solution suggested is to have an array of strings for count, but the user wants to know if there's a better way. Solution: One option is to have a cart collection where each item in the cart is a document with a reference to the user and a relationship with a single product, along with a quantity attribute. This way, the cart document can be updated without changing the product document.
ideclon
3 Jan, 2024, 18:23

I would probably have a cart Collection. Each item in the cart would be a Document, with a reference to the user and a Relationship with a single product, then a qty Attribute

k_vasen
4 Jan, 2024, 04:09

This will require many documents for single user cart. That's why I am tried to keep all products in on doc

ideclon
4 Jan, 2024, 04:42

Well, if you want only one Document for the cart, the only option I see is to have an Array Attribute with the product IDs and quantites (as you suggested). You could also have another Attribute with a many-to-many Relationship with all the products, to avoid having to make a second request out to get the products each time.

ideclon
4 Jan, 2024, 04:43

Although I don't see any inherent issue with having many Documents for each cart

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more