adelin-b
adelin-b6mo ago

How to cascade delete an entity and all references to it in other entities

given a schema like this :
modules: defineTable({
name: v.string(),
documentIds: v.array(v.id("documents")),
})
documents: defineTable({
name: v.string(),
content: v.optional(v.string()),
})
modules: defineTable({
name: v.string(),
documentIds: v.array(v.id("documents")),
})
documents: defineTable({
name: v.string(),
content: v.optional(v.string()),
})
How do should I proceed to have the documentIds automatically removed from the modules when a document is deleted ? I could add a call to the db in the document:delete query so it update the associated modules but I wonder if there is something more robust like a cascading option somewhere like in postgres
5 Replies
Michal Srb
Michal Srb6mo ago
Right now your options are: 1. In the mutation where you delete the module, also delete the documents. 2. Use Ents: https://labs.convex.dev/convex-ents We might add a plugin for cascading deletes in the future.
Convex Ents - Convex Ents
Relations, default values, unique fields and more for Convex
adelin-b
adelin-bOP6mo ago
I just found convex ents indeed and was going throught the documentation it seem to do what I need ! I will start the setup process However it seems that its not very well used and I wonder about the maintain capabilities as its not convex team doing it. Recently started using convex and it feel a bit scary to rely on convex-helpers and now this package.
Matt Luo
Matt Luo6mo ago
I'm going through a migration to Ents now. I shared these concerns, but I looked through the Ents repo and it seems maintainable with respect to its importance to Convex's customer success.
Michal Srb
Michal Srb6mo ago
(also both Ents and helpers are built by the folks on the Convex team - it's mostly a packaging decision that allows us to move faster on the edge of the platform)
lee
lee4mo ago
Database Triggers
Triggers automatically run code whenever data in a table changes. A library in the convex-helpers npm package allows you to attach trigger functions t...

Did you find this page helpful?