Skip to content

Error when passing RLS policies to triggers wrapper #893

Description

@Niall-Kenny

Example code:

export const employeeMutation = customMutation(rawMutation, {
  args: { ...employeeArgs },
  input: async (ctx, args) => {
    if (!args.token) {
      throw new ConvexError({
        type: "invalid employee user",
        description: "No token provided",
      })
    }
    const auth = await getCurrentUserHandler(ctx, { token: args.token })
    const employee = auth?.employee

    if (!employee) {
      throw new ConvexError({
        type: "invalid employee user",
        description: "userId",
      })
    }

    return {
      ctx: {
        ...triggers.wrapDB({
          ...ctx,
          db: wrapDatabaseWriter(ctx, ctx.db, employeeRlsRules(ctx, employee), {
            defaultPolicy: "deny",
          }),
        }),
        employee,
      },
      args,
    }
  },
})

Any time I call e.g. ctx.db.patch or ctx.db.get inside a function wrapped by employeeMutation, I get the following error :

TypeError: Cannot read properties of undefined (reading 'get') 
Object.get node_modules/convex-helpers/server/rowLevelSecurity.js:245:28

Its important than RLS policies are adhered to inside the trigger, which is why I haven't wrapped a custom mutation in a trigger wrapper, then wrapped this in am RLS wrapper.

As recommended in the trigger docs, the following works, BUT does not adhere to RLS policies, which is the issue:

const mutationWithTriggers = customMutation(
  rawMutation,
  customCtx(triggers.wrapDB),
)
export const employeeMutation = customMutation(mutationWithTriggers, {
...
      })

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions