Best practices for joining data in query
export const getProjectById = query({
args: { projectId: v.id("projects") },
handler: withUser(async ({ db, user }, { projectId }) => {
const project = await db.get(projectId);
if (!project) {
throw new Error("Project not found");
}
if (project.user !== user._id) {
throw new Error("Unauthorized");
}
const notes = await db
.query("notes")
.withIndex("by_project", (q) => q.eq("project", project._id))
.collect();
return {
...project,
notes: notes,
};
}),
});