#v
7 messages · Page 1 of 1 (latest)
Hi @maiden geyser. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
For “I have an array of document IDs, how do I fetch all of them?”, the recommended pattern is to load them in parallel with an async map / getAll helper.
Conceptually:
// ids: v.array(v.id("someTable"))
const docs = await Promise.all(
ids.map(id => ctx.db.get("someTable", id))
);
The Convex docs and helpers show the same idea as:
const userPromises = [];
for (const userId of userIds) {
userPromises.push(db.get(userId));
}
const users = await Promise.all(userPromises);
and then wrapped as a getAll helper that takes an array of IDs and returns all documents in parallel.[One-to-many helpers][Array of docs]
This is the best practice when you already have specific document IDs and just need to load those documents.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@maiden geyser Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@left marlin i mean in internalQuery
Let me check the knowledge base for you again. One moment! :books:
In an internalQuery you use the same pattern: parallel db.get calls over the array of IDs.
From the docs and Stack article, the recommended approach is:
import { internalQuery } from "./_generated/server";
import { v } from "convex/values";
export const getManyById = internalQuery({
args: { ids: v.array(v.id("someTable")) },
handler: async (ctx, args) => {
const docs = await Promise.all(
args.ids.map(id => ctx.db.get("someTable", id))
);
return docs;
},
});