Back

[SOLVED] Database Relationship with Storage

  • 0
  • Databases
  • Storage
Tomic R.
5 May, 2023, 20:12

Appwrite has recently launched Database Relationships, a feature many of use have been waiting for. But there is no good way of creating a relationship with an item in the storage. What is the recommended approach to achieve this currently?

TL;DR
The user was looking for a way to create a database relationship with storage items in Appwrite. They were using a list of file IDs in their database, but felt it was not safe and complicated to manage. One suggestion was to create a function triggered by the deletion of an article to also delete the related storage items. Another suggestion was to consider a hidden feature that allows referencing a file from the storage service in the database. The user was asking for the recommended approach to achieve this currently. Solution: - Create a function triggered by the deletion of an article to delete the related storage items. - Consider using a hidden feature that allows referencing
safwan
5 May, 2023, 20:18

Mind if i ask what your use case is?

Torsten Dittmann
5 May, 2023, 21:37

Funnily we actually had a hidden feature like 2 years go, which allows referencing a file from the storage service in the database but it never saw the light of day @Eldad

It might be worth a thought now to be able, to reference appwrite resource.

Tomic R.
6 May, 2023, 05:56

Okay, so it's actually a quite simple use case: I have multiple articles. Each article can have multiple article blocks. These consist of a subheading, a text, and a list of images.

My current database structure looks roughly like this: "Articles" with the attributes: ID (logically) Name (The name for the whole Article) Relationship to ArticleBlocks. One article can contain multiple ArticleBlocks, one ArticleBlock can belong to one Article:

"ArticleBlocks" with the attributes: ID Headline ArticleID (The Relationship) Content List<String> (Currently a list of ids referencing to the document ids in the storage)

The problem is, that I feel like this List<String> approach is not very safe. For example, when an Article gets deleted, all Article Blocks get deleted too (Because of the relationship). But I have to manually write a function that also deletes the storage items, because they take up a lot of space. When a user in the application removes an image, I not only have to delete the image in the storage, but also have to update the article block. Considering, that the user may go offline in the meantime or something else happens, I can never be sure, that everything is actually deleted.

Conclusion: I always have to execute 2 steps to fulfill one task, which is quite messy

I hope that I've explained this understandably πŸ˜…

safwan
12 May, 2023, 07:39

First off, I apologize for the late response. I lost track of this thread πŸ˜…

Let me see if I got this right:

  • An Article can have multiple ArticleBlocks
  • An ArticleBlock can belong to only one Article
  • When an Article is deleted, all of it's related ArticleBlocks also get deleted, but the assets used in the ArticleBlocks do not get deleted.

Okay so based on this, I can see why a link to bucket files will be much better. Considering Appwrite doesn't have that feature yet, here's what I would do.

First thing to note, I don't think storing a list of file IDs in the ArticleBlocks collection is unsafe. It's basically what relations do (if i'm not wrong). As for the deletion part, here's my idea:

  1. Create a function article-deleted. This function is triggered when an document in the Article collection is deleted - databases.[DATABSE-ID-HERE].collections.[COLLECTION-ID-HERE].delete would be the event to look for. Whenever a function is executed on an event trigger, it comes with a APPWRITE_FUNCTION_EVENT_DATA[1] variable in the payload. You can use the data in this variable to fetch the deleted document's data, which should contain the ArticleBlocks that got deleted as well. (Not a 100% sure about the last sentence as I haven't used Relations yet).
  2. Even if the APPWRITE_FUNCTION_EVENT_DATA doesn't contain the full data for the related ArticleBlocks, you should be able to get atleast some data that points to the specific ArticleBlocks, and use that.
  3. Once you have the corresponding documents of ArticleBlock, you can use the list of file IDs stored in each document, and call storage.deleteFile() to delete each file.

I don't think it's tooo complex, and can be done with some trail-and-error.

[1] https://appwrite.io/docs/functions#functionVariables

safwan
12 May, 2023, 07:39

Hope it's easy to understand lol

Tomic R.
13 May, 2023, 12:20

Thank you very much! I will do that!

safwan
17 May, 2023, 06:53

Hey @Tomic R. Checking in to see if your issue was solved?

Tomic R.
17 May, 2023, 07:16

yep, thank you very much!

safwan
17 May, 2023, 07:37

[SOLVED] Database Relationship with Storage

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