Combine queries into batches
All checks were successful
Test, Build, Deploy / Test, Build, and Deploy (push) Successful in 45s
Test, Build, Deploy / Create Sentry Release (push) Successful in 6s

This commit is contained in:
2026-03-14 05:18:50 -04:00
parent 994a7a7a58
commit 47df3dc55f
2 changed files with 34 additions and 45 deletions

View File

@@ -33,37 +33,31 @@ export async function loader({ context }: { context: RequestContext }) {
if (!currentUser) throw new Response(null, { status: 401 });
const d1Promises = [];
const batchStatements = [];
for (const itemType of ["appeals", "inactivity_notices", "reports"])
d1Promises.push(
for (const itemType of ["appeals", "inactivity_notices", "reports"]) {
batchStatements.push(
context.env.D1.prepare(
`SELECT *
FROM ${itemType}
WHERE json_extract(user, '$.id') = ?
ORDER BY created_at DESC;`,
)
.bind(currentUser.id)
.all(),
`SELECT * FROM ${itemType} WHERE json_extract(user, '$.id') = ? ORDER BY created_at DESC;`,
).bind(currentUser.id),
);
const settledPromises = await Promise.allSettled(d1Promises);
let etData: { [k: string]: any } | null = null;
if (currentUser.permissions & (1 << 3)) {
etData = await context.env.D1.prepare(
"SELECT name, points, roblox_id FROM et_members WHERE id = ?;",
)
.bind(currentUser.id)
.first();
}
return {
etData,
items: settledPromises.map((p) => {
if (p.status === "fulfilled") return p.value.results;
if (currentUser.permissions & (1 << 3))
batchStatements.push(
context.env.D1.prepare(
"SELECT name, points, roblox_id FROM et_members WHERE id = ? LIMIT 1;",
).bind(currentUser.id),
);
return null;
const batchResults = await context.env.D1.batch(batchStatements);
return {
etData: batchResults.at(3)?.results?.at(0) as { [k: string]: any } | null,
items: batchResults.map((r) => {
if (r.success) return r.results;
return [];
}) as any as ({ [k: string]: any }[] | null)[],
permissions: currentUser.permissions as number,
};