xpriori
xpriori•3mo ago

xpriori's Thread

I'm in a lookout for an example of query + filter function. I can't seem to nail it. I'm currently collecting all my users then run a filter by email on the client. 😂
6 Replies
convex-threads
convex-threads•3mo ago
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. If this is a support-related question, please create a post in #support-community. If not, carry on! Thanks!
Clever Tagline
Clever Tagline•3mo ago
What kind of filter do you want? You mentioned email, so are you just trying to find the user document with the matching email, or is it something else?
xpriori
xprioriOP•3mo ago
just like that, passing array of emails as args
erquhart
erquhart•3mo ago
You can move whatever filtering you’re doing on the client right into your query for starters
xpriori
xprioriOP•2mo ago
I currently have that setup. But somewhere in the official docs, the built-in filter is faster that js filter and that's really what i'm trying to accomplish.
erquhart
erquhart•2mo ago
If you have an array of email assesses, index your users table on the email field and loop over the array. Something like:
export const listUsers = query({
args: {
emails: v.array(v.string()),
},
handler: async (ctx, args) => {
return Promise.all(
args.emails.map(async (email) => {
return ctx.db
.query('users')
.withIndex('by_email', (q) => q.eq('email', email))
.unique()
}),
)
},
})
export const listUsers = query({
args: {
emails: v.array(v.string()),
},
handler: async (ctx, args) => {
return Promise.all(
args.emails.map(async (email) => {
return ctx.db
.query('users')
.withIndex('by_email', (q) => q.eq('email', email))
.unique()
}),
)
},
})
Filtering alone requires a full table scan. Here we use an index and the unique method, so each db call is scanning 0 or 1 records max, and we’re running the queries in parallel via Promise.all.

Did you find this page helpful?