MegaHydronics
MegaHydronics
CCConvex Community
Created by MegaHydronics on 12/19/2024 in #support-community
Accessing database in an action
I'm hoping to better understand why ActionCtx does not expose db. This forces me to write much longer code like the first below, instead of the second.
export const set = internalAction({
args: { key: v.string() },
handler: async (ctx, { key }) => {
const exists = await ctx.runQuery(internal.db.user._exists, { key })
if (exists) return

const response = await fetch(...)
const args = await response.json()
ctx.runMutation(internal.db.user._insert, args)
},
})

export const _insert = internalMutation({
args: v.object(userSchema),
handler: async (ctx, args) => {
await ctx.db.insert('user', args)
},
})

export const _exists = internalQuery({
args: { key: v.string() },
handler: async (ctx, { key }) => {
const user = await ctx.db.query('user').withIndex('key', q => q.eq('key', key)).unique()
return user !== null
},
})
export const set = internalAction({
args: { key: v.string() },
handler: async (ctx, { key }) => {
const exists = await ctx.runQuery(internal.db.user._exists, { key })
if (exists) return

const response = await fetch(...)
const args = await response.json()
ctx.runMutation(internal.db.user._insert, args)
},
})

export const _insert = internalMutation({
args: v.object(userSchema),
handler: async (ctx, args) => {
await ctx.db.insert('user', args)
},
})

export const _exists = internalQuery({
args: { key: v.string() },
handler: async (ctx, { key }) => {
const user = await ctx.db.query('user').withIndex('key', q => q.eq('key', key)).unique()
return user !== null
},
})
export const set = internalAction({
args: { key: v.string() },
handler: async (ctx, { key }) => {
const user = await ctx.db.query('user').withIndex('key', q => q.eq('key', key)).unique()
if (user !== null) return

const response = await fetch(...)
const args = await response.json()
await ctx.db.insert('user', args)
},
})
export const set = internalAction({
args: { key: v.string() },
handler: async (ctx, { key }) => {
const user = await ctx.db.query('user').withIndex('key', q => q.eq('key', key)).unique()
if (user !== null) return

const response = await fetch(...)
const args = await response.json()
await ctx.db.insert('user', args)
},
})
3 replies
CCConvex Community
Created by MegaHydronics on 12/17/2024 in #support-community
Accessing client IP address in a query/mutation
Good day. Any help would be appreciated, as I am fairly new to Convex. Is there a way to expose HTTP headers, such as for obtaining the IP address in the example below? Or is an httpAction required for this use case?
const registerClient = mutation({
args: { userAgent: v.string() },
handler: async (ctx, { userAgent }) => {
// get ip address from 'x-forwarded-for' header
await ctx.db.insert('client', { userAgent, ip })
}
})
const registerClient = mutation({
args: { userAgent: v.string() },
handler: async (ctx, { userAgent }) => {
// get ip address from 'x-forwarded-for' header
await ctx.db.insert('client', { userAgent, ip })
}
})
7 replies