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?
Mind if i ask what your use case is?
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.
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 π
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:
- 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 aAPPWRITE_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). - 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. - 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.
Hope it's easy to understand lol
Thank you very much! I will do that!
Hey @Tomic R. Checking in to see if your issue was solved?
yep, thank you very much!
[SOLVED] Database Relationship with Storage
Recommended threads
- Type Mismatch in AppwriteException
There is a discrepancy in the TypeScript type definitions for AppwriteException. The response property is defined as a string in the type definitions, but in pr...
- What Query's are valid for GetDocument?
Documentation shows that Queries are valid here, but doesn't explain which queries are valid. At first I presumed this to be a bug, but before creating a githu...
- Appwrite exception: user_unauthorized, t...
After refreshing the app it is working perfectly