Back

[SOLVED] Querying data from a collection that depends on other collections!

  • 0
  • Databases
ZiaChoudhary
5 Sep, 2023, 07:26

I am having a database with collections as : Countries, Categories, SubCategories, Posts My posts collection has attributes as: countryID, categoryID, subCategoryID, title, Image, content Now if I have to query a list of posts it returns data like {[{'countryID': 'countryUniqueID','categoryID' : 'categoryUniqueID', 'subCategoryID': 'subCategoryUniqueID', 'title': 'Post Title', 'content': 'post content ......' },...]} Here I got id's for country, category and sub category. Instead of getting id's I want to get names for them. NOTE: I don't wanna use relationships as they are experimental and I am working on a realtime production ready application. What are the other alternatives for getting desired results.

TL;DR
The user is asking for alternatives to using relationships in Appwrite to query data from collections that depend on other collections. They want to get names instead of IDs in the response. The recommended solution is to manually handle the relationships by fetching the related documents after fetching from the primary collection and converting the response to a hash map. This allows for quick lookup of related documents. Relationships in Appwrite are still experimental and don't support queries. Other options mentioned include using client-side functions or making new requests for each ID.
ZiaChoudhary
5 Sep, 2023, 13:13

is there anyone who knows about this?

ideclon
5 Sep, 2023, 14:50

The only way to get the names in the response direct from Appwrite would be to either store the names in the collections, or with Relationships

ideclon
5 Sep, 2023, 14:50

Your only other option is to take the response you’re currently getting and make new requests for each of those IDs

ideclon
5 Sep, 2023, 14:51

If you don’t want to do that on the client side, you could put it into a Function

ZiaChoudhary
5 Sep, 2023, 15:06

I think using direct names is not a good practice. Relationships are not mature yet. And the third one using functions is okey but is not it a bad practice to query for every id means i have to loop the list of post documents and for every post again i run queries to get data from other collections is not it a bad one

ideclon
5 Sep, 2023, 17:20

Of course, none of the options are ideal - but there isn't any other way currently, I'm afraid. This is exactly what Relationships was created for, but if you don't want to use them yet, you don't really have many other options

ZiaChoudhary
5 Sep, 2023, 17:35

The reason for not choosing relationships is that they don't support queries and also they are still experimental.

Please if someone knows about when we can expect the relationships to be fully stable. Because they are the backbone of any live app.

ZiaChoudhary
5 Sep, 2023, 17:36

Anyone from core team, please throw some light on this issue. Because its a biggest nightmare for our team.

Drake
6 Sep, 2023, 18:09

i can't say when exactly. probably not for a (minor) release or 2. it all depends on whether we find more bugs.

Drake
6 Sep, 2023, 18:11

for now, i would recommend manually handling the relationships yourself by fetching the related documents after fetching from the primary collection.

after fetching the related collections, i highly recommend convering the response to a hash where the key is the ID and the value is the document. This will allow you to quickly look up the related document when you're iterating over your primary collection.

for example:

  1. hash map: https://github.com/stnguyen90/places/blob/9c55f7097f79a12eeb7eda8fa450e4d961b07cc4/src/features/place-dialog/CommentsTab.tsx#L60-L63
  2. using hash map: https://github.com/stnguyen90/places/blob/9c55f7097f79a12eeb7eda8fa450e4d961b07cc4/src/features/place-dialog/CommentsTab.tsx#L133
ZiaChoudhary
7 Sep, 2023, 01:49

Thank you @Steven. One more thing, Should we create server side functions or achieve it simply through client side. Which one is best optimized for appwrite.

ZiaChoudhary
7 Sep, 2023, 01:53

Kindly add it to closest roadmap. And please also share the link if any for upcoming updares/changes in appwrite. So that we can directly track and vote for features.

Drake
7 Sep, 2023, 02:11

I would just do it client side

Drake
7 Sep, 2023, 02:12

We don't have a public roadmap yet...we're still figuring that out

ZiaChoudhary
7 Sep, 2023, 03:32

[SOLVED] Querying data from a collection that depends on other collections!

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