Does convex support database triggers?
I’m looking for guidance on implementing database triggers. I attempted to manage created, updated, and deleted documents by tracking in each mutation and scheduling actions to handle these changes. However, I soon realized that this approach becomes challenging to manage at scale, especially with more mutations across multiple tables. Is there a centralized method to handle database triggers more efficiently?
4 Replies
If you search this discord there have been some discussions on this. In terms of making this work today, I do everything I used to do with postgres triggers right inside the mutation, so it's a part of the same transaction. You can make this scalable by limiting writes to a set of shared helper functions (as opposed to calling
ctx.db.insert/patch/delete
right inside of your various convex functions).
For example, all of my tables have exactly one insert
, patch
and optionally upsert
helper function per table, so making sure something happens on any one of those events is trivial and scalable.Thanks for your suggestion.
I was concerned about loosing type safety with the helper functions but that doesn't seem to be case. So far seems to be working.
Yeah the type safety in convex is really well done, flows nicely
for anyone finding this later, there's now another option (although the helper function option is often better, as described in "Best practices") https://stack.convex.dev/triggers
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...