backpack1098B
Convex Community9mo ago
9 replies
backpack1098

How to properly combine a Convex query with an action?

I'm trying to implement a query that needs to fetch some encrypted data and decrypt it using AWS KMS. However, I'm running into a TypeScript error where ctx.runAction is not available in the query context. Here's a simplified version of what I'm trying to do:

export const queryEncryptedData = query({
  args: { id: v.string() },
  handler: async (ctx, args) => {
    // 1. First fetch the encrypted data
    const data = await ctx.db
      .query("myTable")
      .withIndex("by_id", q => q.eq("id", args.id))
      .first()
    if (!data) return undefined

    // 2. Then try to decrypt the sensitive fields
    // This is where we get the error:
    // Property 'runAction' does not exist on type 'QueryCtx'
    const decryptedItems = await Promise.all(
      data.items.map(async (item) => ({
        ...item,
        value: await ctx.runAction(internal.kms.decryptData, {
          data: item.encryptedValue?.data,
          key: item.encryptedValue?.key,
          iv: item.encryptedValue?.iv,
        })
      }))
    )

    return {
      items: data.items.map(item => ({
        ...item,
        // Need to return the decrypted value here
      }))
    }
  }
})


is the only option to do useEffect in the front end component?
Was this page helpful?