fix: improve sql queries

This commit is contained in:
2026-04-09 04:48:50 +04:00
parent 89f0856a04
commit 91ca996e74
10 changed files with 495 additions and 219 deletions
+14 -9
View File
@@ -62,17 +62,22 @@ async function replaceGroupsAndMonitors(
if (groups !== undefined) {
await sql`DELETE FROM status_page_groups WHERE status_page_id = ${pageId}`;
}
// Single bulk INSERT instead of one round-trip per group. The RETURNING set
// comes back in INSERT order, which equals the array order — that lets us
// map index → id without a follow-up SELECT. Mirrors the bulk insert pattern
// used by the monitors block right below.
const groupIds: string[] = [];
if (groups && groups.length > 0) {
for (let i = 0; i < groups.length; i++) {
const g = groups[i]!;
const [row] = await sql<{ id: string }[]>`
INSERT INTO status_page_groups (status_page_id, name, position)
VALUES (${pageId}, ${g.name}, ${g.position ?? i})
RETURNING id
`;
groupIds.push(row!.id);
}
const rows = groups.map((g, i) => ({
status_page_id: pageId,
name: g.name,
position: g.position ?? i,
}));
const inserted = await sql<{ id: string }[]>`
INSERT INTO status_page_groups ${sql(rows, "status_page_id", "name", "position")}
RETURNING id
`;
for (const r of inserted) groupIds.push(r.id);
}
if (monitorsList !== undefined) {