Merge pull request #238 from amal66/olp-pr/directory-fetch-storm

[Fix 01] Batch the directory modal's project fetch (N+1 getProject storm)
This commit is contained in:
Will Chen 2026-07-21 16:51:36 +08:00 committed by GitHub
commit eda088e143
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 17 deletions

View file

@ -152,9 +152,16 @@ async function attachChatCreatorLabels(
}
// GET /projects
// Pass ?include=documents to also receive each project's documents in the
// same response. The directory pickers (useDirectoryData) previously fanned
// out one GET /projects/:id per project to obtain those documents; with N
// projects that burst — auth check plus several DB queries per request —
// could overwhelm the Supabase gateway. Batching keeps it at one request
// and a fixed number of queries regardless of project count.
projectsRouter.get("/", requireAuth, async (req, res) => {
const userId = res.locals.userId as string;
const userEmail = res.locals.userEmail as string | undefined;
const includeDocuments = req.query.include === "documents";
const db = createServerSupabase();
const { data, error } = await db.rpc("get_projects_overview", {
@ -163,7 +170,45 @@ projectsRouter.get("/", requireAuth, async (req, res) => {
});
if (error) return void res.status(500).json({ detail: error.message });
res.json(data ?? []);
const projects = (data ?? []) as { id: string }[];
if (!includeDocuments || projects.length === 0) {
return void res.json(projects);
}
const { data: docs, error: docsError } = await db
.from("documents")
.select("*")
.in(
"project_id",
projects.map((p) => p.id),
)
.order("created_at", { ascending: true });
if (docsError)
return void res.status(500).json({ detail: docsError.message });
const docsTyped = (docs ?? []) as unknown as {
id: string;
project_id?: string | null;
user_id?: string | null;
current_version_id?: string | null;
}[];
await attachLatestVersionNumbers(db, docsTyped);
await attachActiveVersionPaths(db, docsTyped);
await attachDocumentOwnerLabels(db, docsTyped);
const docsByProject = new Map<string, typeof docsTyped>();
for (const doc of docsTyped) {
if (!doc.project_id) continue;
const bucket = docsByProject.get(doc.project_id);
if (bucket) bucket.push(doc);
else docsByProject.set(doc.project_id, [doc]);
}
res.json(
projects.map((p) => ({
...p,
documents: docsByProject.get(p.id) ?? [],
})),
);
});
// POST /projects