Whats the best practice/approach for saving and fetching convex images?
- Should I save storageId inside i.e product's table i.e
thumbnail: v.id("_stareage")
or should I have a separate table i.e productThumbnails
?
- If I have multiple images (previous question also applies) i.e images: v.array(v.id("_stareage"))
when I delete I need to make sure I delete in both places (storage and in database) right?
- Should I prefetch images in convex server side when I call i.e getProducts? i.e
Or should I fetch images client side where I have only storageId id?5 Replies
you should store it in storage that is what I remember, and from what I remember it will be only stored in the storage and in the database it will store only the id
If you have maximum of 5 images, its fine with array, if you have many images create another table and join.
You should prefetch image url on the server, this will generate url for you that will work for 1 hour.
When you want to delete the reference, you first delete it from the storage:
await ctx.storage.delete(args.storageId);
and then you delete it the reference...
"If you have maximum of 5 images, its fine with array"
What's the reasoning?
"You should prefetch image url on the server, this will generate url for you that will work for 1 hour" - but this will take more time to respond right? because I need to fetch products and then all images for those products with urls
Trading off deeply nested documents vs. relationships
While it's useful that Convex supports nested objects and arrays, you should keep documents relatively small in size. In practice, we recommend limiting Arrays to no more than 5-10 elements and avoiding deeply nested Objects.
Instead, leverage separate tables, documents, and references to structure your data. This will lead to better maintainability and performance as your project grows.
Document IDs | Convex Developer Hub
Create complex, relational data models using IDs.