Database Triggers
https://stack.convex.dev/triggers this just doesnt work at all. no events trigger
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...
40 Replies
Hi, this is definitely unexpected. Did you make sure that you’re defining all your mutations using
customMutation
instead of the regular mutation
and internalMutation
functions?triggers.register("users", async (ctx, change) => {
console.log("user changed", change);
});
no logs on trigger so nothing is executing

This looks correct. But for triggers to work, you also need to use the correct syntax when defining mutations that will affect the
users
tableFor instance in https://stack.convex.dev/triggers the mutations are imported from
./functions
, where mutation
and internalMutation
are defined as:
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...
Triggers won’t run if you update the data from somewhere else (e.g. if you don’t use customMutation or edit data from the dashboard or DB imports)
i have custom mutations defined
Are you using this declaration of
mutation
where you write your custom functions?what do you mean? i have this
export const mutation = customMutation(rawMutation, customCtx(triggers.wrapDB));
export const internalMutation = customMutation(rawInternalMutation, customCtx(triggers.wrapDB));
For instance could you please share one example of how you’re definining particular mutations in your code? For instance the mutation you use to insert a row in playerInventory
here is mutation that is removing item from inventory
and its not triggering trigger
Do you have an example of a place where you’re calling this function?
This isn’t a Convex mutation in itself but can be called from mutations
so data in the table is changed
Thanks! In this example, where do you import
mutation
from?so there are multiple functions triggered from mutation till inventory is modified
mutation that is called by client?
For instance, in the file where you define
spin
, what’s the import
statement you use to import mutation
?Thanks! So here, if you want triggers to work, you need to change that
import
to import mutation
from the file where you defined custom mutations
The issue is that with the current import, your changes go straight to your database, without the triggers having any chance of detecting that there was a changeok understand
so its not possible trigger to work if data is changed in database directly?
Yes, triggers are implemented entirely as code that lives inside of your Convex functions, so you need to do this
ok understand thanks
And as I said above the triggers won’t be executed for changes that come from somewhere else (e.g. the dashboard)
You're welcome!
can i call await ctx.db.patch inside trigger register?
or need to call mutation
because its executed now but has no effect
but no data was changed
regenerationPauseTime of user is still defined in the database
@Nicolas
Yes, this should work
ok doesnt work for me hm
logs triggering but no data is changed in the database
If you go to the Convex logs in the dashboard, does the function call succeed? If there is an error the effects of the mutation will not be committed
no error

Hmmm, I’m wondering if that might be a bug in the triggers library where
undefined
is treated the same way as if the field didn't exist at all
If you try to set the value to something else than undefined, does the write go through?will test
Thanks!
still same.
i will now move away from trigger and trigger the functions directly in code
ok sorry this bug was not connected to this. db patch of this table was called within same mutation somewhere else so data was overriden
Great to hear you found the source of the issue!
Did not know such conflicts possible there
Is it possible somehow to trigger db patch in the code somewhere else and not to override entire row?
Because one db patch is affecting one colum and another db patch another column
But full document gets overriden
Are they happening in parallel promises? If so, they might be racing
I will check if those are parallel
One is executed by mutation code and another one by trigger
Triggers does some locking to prevent this. If you can get a minimal repro, that'd be awesome
w ian
the problem with overriding was on my side. i fixed it. thanks