Back

Database modeling

  • 0
  • General
elnur
26 Jul, 2024, 19:15

Hi, I think it would be very valuable to provide users with examples of database modeling for NoSQL like Appwrite.

It would be further appreciated to know whether data duplication is necessary to avoid listDocuments vs getDocument.

Eg:

Collection: Posts postId: string postContent: string images?: string [] (imageIds?)

Collection: Images imageId: string imageUrl: Url postId: string

So either we query all images for the post via listDocuments or we have an images key in each post that has the imageIds that the post has which would enable us to have getDocument but increases complexity by having data duplication/reciprocal design.

So my question is more of a request to have more info on what is recommended or what to consider when designing database schemas for appwrite.

Kindly

TL;DR
Using arrays is suitable if posts are rarely updated. For frequently updated posts with images, it's better to use key indexing on postId in the images collection. Avoid relationships due to issues like slow performance and data mismatch. Consider caching for performance optimization. Data duplication may be necessary to choose between listDocuments and getDocument. More guidance on database modeling for NoSQL like Appwrite with examples would be valuable.
elnur
26 Jul, 2024, 19:18

Is listDocuments as performant as getDocument? Then we don’t need to have the images key on each post. But if it’s a huge performance loss it’s a no brainer right?

What of many-to-many relationships? Googling will give more insight on what to do and Firebase database guides are almost applicable but they have enough differences that make those approaches not ideal for appwrite.

elnur
26 Jul, 2024, 19:18

Thanks for taking the time to read and I’m looking forward to hear your thoughts.

Joshi
26 Jul, 2024, 19:33

Collection: Posts postId / documentId postContent: string

Collection: Images imageId / documentId imageUrl: Url postId: string

Joshi
26 Jul, 2024, 19:33

I personally don't like using array in Appwrites current state, because you can't update a single item in the array. You gotta do a full update

Joshi
26 Jul, 2024, 19:33

And there can be a mismatch because of race conditions

Joshi
26 Jul, 2024, 19:34

I do not recommend using relationsships either lol

Joshi
26 Jul, 2024, 19:34

Not until they have improved it

Joshi
26 Jul, 2024, 19:35

In images collection you can put a key index on postId

Joshi
26 Jul, 2024, 19:36

getDocument is cached and listDocument isn't. with getDocument you always get max performance but if you set key index on postId you get the same performance as getDocument basically

elnur
26 Jul, 2024, 19:53

Interesting, thanks for your insight. I have had the same experience as you unfortunately. Relationships have treated me poorly, stuck attributes, slow performance, related documents not having their relationships show up with the expected keys.

Joshi
26 Jul, 2024, 19:56

Backup your data 3 times for good measure before you are going to use relationsship lmao

Joshi
26 Jul, 2024, 19:57

Are the posts are going to be updated often? Can the user only attach images to a post when he creates the post or can he update the posts with new images later on?

elnur
26 Jul, 2024, 19:58

Posts can be updated and probably will be often, they can attach images at creation and update or remove images after the fact.

Joshi
26 Jul, 2024, 19:59

Okay then I would advise to stick to my solution above

Joshi
26 Jul, 2024, 19:59

If the posts are rarely updated you can use arrays

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