Back

What's the best way to automatically update data in a collection based on update in other collection

  • 1
  • Databases
  • Functions
  • Web
  • Realtime
rakesh
9 May, 2024, 16:07

My project is similar to an e-commerce app. I have a like functionality. every user can see their liked products in their wishlist and every product has the count of how many users have liked it. For the wishlist, I have a users collection where there's a document for every user with the id same as the user id and it has only one attribute which is an array that contains ids of all the products a user has liked. For the Likes count, the products collection has a likes attribute which is an array that contains ids of all the users that has liked the product.

Earlier i was updating both collections whenever a user adds or removes a product from wishlist (i.e. likes or unlikes) but soon i realised that updating product data manually isn't a good idea since it'll have a concurrency problem. So i used appwrite relationships. now whenever i update data in users collection, it automatically updates the products data coz it's a two way many to many collection. But now i have a new problem. now the Arrays don't just have the ids but they have the whole objects. eg. the array in products collection returns all the attributes of the user with associated id and same thing with the users array. as the users and products grow, the size increased and now the requests are very slow. like it takes almost 10 seconds to load the products data which was almost instantaneous earlier.

Is there a way to only add user ids of the users who liked the products instead of adding their whole document? or even a way to just increase or decrease a likes count based on updates in the users collection? please help. Sorry for the long message, I thought I should make the situation clear.

TL;DR
Developers want to automatically update a collection based on changes in another collection in an e-commerce app. Initially manually updating caused concurrency issues, so they switched to using appwrite relationships. However, now the collections contain whole objects causing slow loading times. They are looking for a way to only store user ids instead of the entire document. A suggestion: Instead of storing the whole user object, store just the user id in the likes array. To update likes count, you can listen for changes in the users collection and increment/decrement the likes count in the products collection accordingly.
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