Back

Updating a list or relationships in a document

  • 0
  • Cloud
Samuel Kings
22 Jul, 2024, 10:41

Hmm. So how do you query this? Say you want to get a list of chats a user has

TL;DR
Developers discussing building a chat app; considering storing messages as JSON strings or using relationships, but encountering limitations with updating documents each time a new message is added. Suggestions include using separate collections for each chat, hashing participant IDs for unique chat IDs, and using prefixes for different message types within a JSON string. Permissions and document models are also discussed. Solution: Consider using separate collections for each chat to avoid updating the whole document each time a new message is added.
Joshi
22 Jul, 2024, 10:42

Haven't considered that. Yeah then you can save it

Samuel Kings
22 Jul, 2024, 10:43

In the last chat app I built with Realm, each chat object had a "participantsIDs" attribute. That way I could easily get a user's chats by querying the attribute using contains

Samuel Kings
22 Jul, 2024, 10:43

But all in all, thanks for your help. I learnt a thing or two

Samuel Kings
22 Jul, 2024, 10:44

I'd also like to know if there's a limit on the documents a collection could possibly hold

Joshi
22 Jul, 2024, 10:44

There is not really a limit

Samuel Kings
22 Jul, 2024, 10:45

Alright.

Samuel Kings
22 Jul, 2024, 10:45

Thank you

Joshi
22 Jul, 2024, 10:45

And even if there would be one it wake take a long time for you to reach it lmao

Joshi
22 Jul, 2024, 10:46

I literally inserted over 50m documents to a collection to test its effiency

Joshi
22 Jul, 2024, 10:46

It all still works fine

Samuel Kings
22 Jul, 2024, 10:46

Oh wow.

Samuel Kings
22 Jul, 2024, 10:46

Alright thanks for the info.

Samuel Kings
22 Jul, 2024, 10:47

I was wondering if to keep all the messages in a single collection or create a separate collection for each chat as you earlier mentioned

Joshi
22 Jul, 2024, 10:47

I would recommend separate collection for each chat

Joshi
22 Jul, 2024, 10:48

Otherwise you would have to include a chatId to the message model as well

Samuel Kings
22 Jul, 2024, 10:49

Yes, this.

Joshi
22 Jul, 2024, 10:49

In the first approach you only store the chatId once somewhere

Samuel Kings
22 Jul, 2024, 10:49

I'm just tryna avoid having to write more cloud functions 😅

Joshi
22 Jul, 2024, 10:50

but if each message contains chatId that can add up quite quickly

Samuel Kings
22 Jul, 2024, 10:50

True this.

Samuel Kings
22 Jul, 2024, 10:50

I'll do this anyways. Seems a lot cleaner

Joshi
22 Jul, 2024, 10:51

Yeah and I think permissions are somewhat easier to maintain I think

Samuel Kings
22 Jul, 2024, 10:54

Yeah. Thank you 🙏🏽

Joshi
22 Jul, 2024, 10:58

For image attachments you could create a function: Everytime a user uploads an image to a bucket where the chatId act as bucketId, a function gets triggered an creates a document in the chat collection with

message: <fileId> type: image senderId: <userId of sender>

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