David Alonso
David Alonso2w ago

dbRules functions not being triggered

I have the following:
export const dbRules: Rules<AuthQueryCtx | AuthMutationCtx, DataModel> = {
firestoreEdits: {
insert: async (ctx, edit) => {
const role = await _getExistingRoleFromUserAndOrgWorkspaceIds(ctx, {
userId: ctx.user._id,
workspaceId: ctx.workspace._id,
});
console.log("role", role);

if (role?.role === "admin") {
return true;
}
return false;
},
...
export const dbRules: Rules<AuthQueryCtx | AuthMutationCtx, DataModel> = {
firestoreEdits: {
insert: async (ctx, edit) => {
const role = await _getExistingRoleFromUserAndOrgWorkspaceIds(ctx, {
userId: ctx.user._id,
workspaceId: ctx.workspace._id,
});
console.log("role", role);

if (role?.role === "admin") {
return true;
}
return false;
},
...
And this:
export const zAuthenticatedMutation = zCustomMutation(
mutationWithTriggers, // The base function we're extending
{
args: {
// This type makes sure we can't just forget to pass the clerkOrgId
// clerkOrgId: v.union(v.string(), v.null()),
clerkOrgId: v.string(),
userTokenIdentifier: v.optional(v.string()),
},
input: async (ctx, args) => {
const { user, workspace } = await enhanceClientAuthContext(
ctx,
args.clerkOrgId,
args.userTokenIdentifier
);

// RLS wrapper
const safeDB: DatabaseWriter = wrapDatabaseWriter(
{ user, workspace, ...ctx },
ctx.db,
dbRules
);

return { ctx: { user, workspace }, db: safeDB, args: { ...args } };
},
}
);
export const zAuthenticatedMutation = zCustomMutation(
mutationWithTriggers, // The base function we're extending
{
args: {
// This type makes sure we can't just forget to pass the clerkOrgId
// clerkOrgId: v.union(v.string(), v.null()),
clerkOrgId: v.string(),
userTokenIdentifier: v.optional(v.string()),
},
input: async (ctx, args) => {
const { user, workspace } = await enhanceClientAuthContext(
ctx,
args.clerkOrgId,
args.userTokenIdentifier
);

// RLS wrapper
const safeDB: DatabaseWriter = wrapDatabaseWriter(
{ user, workspace, ...ctx },
ctx.db,
dbRules
);

return { ctx: { user, workspace }, db: safeDB, args: { ...args } };
},
}
);
I have a mutation that adds edits:
export const addEdit = zAuthenticatedMutation({
args: {
edit: zAddEditArgs,
},
handler: async (ctx, args) => {
...
export const addEdit = zAuthenticatedMutation({
args: {
edit: zAddEditArgs,
},
handler: async (ctx, args) => {
...
But when i trigger it and an edit is created I don't see any logs put inside the insert function in the rules, and it doesn't seem to be used.. How is this possible?
4 Replies
Convex Bot
Convex Bot2w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
erquhart
erquhart2w ago
Can you confirm other similar rules are working as expected? Also, obligatory, are you running convex dev?
lee
lee2w ago
I'm not the most familiar with zCustomMutation but this looks like a typo: return { ctx: { user, workspace }, db: safeDb, args: {...args}} Should it be this? return { ctx: { user, workspace, db: safeDb }, args: {...args}}
David Alonso
David AlonsoOP2w ago
Savior! I overlooked this! Thank you Lee!