Combine queries into batches
This commit is contained in:
@@ -35,29 +35,24 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
month = 12;
|
||||
}
|
||||
|
||||
const eventMemberQuery = await context.env.D1.prepare(
|
||||
"SELECT id, name, roblox_id FROM et_members;",
|
||||
).all();
|
||||
const inactivityQuery: D1Result<Record<string, any>> =
|
||||
await context.env.D1.prepare(
|
||||
"SELECT decisions, json_extract(user, '$.id') AS uid FROM inactivity_notices WHERE (end BETWEEN DATE('now', 'start of month', '-1 month') AND DATE('now', 'start of month', '-1 day')) OR (start BETWEEN DATE('now', 'start of month', '-1 month') AND DATE('now', 'start of month', '-1 day'));",
|
||||
).all();
|
||||
const batchStatements: D1Result<Record<string, any>>[] =
|
||||
await context.env.D1.batch([
|
||||
context.env.D1.prepare("SELECT id, name, roblox_id FROM et_members;"),
|
||||
context.env.D1.prepare(
|
||||
"SELECT decisions, json_extract(user, '$.id') AS uid FROM inactivity_notices WHERE (end BETWEEN DATE('now', 'start of month', '-1 month') AND DATE('now', 'start of month', '-1 day')) OR (start BETWEEN DATE('now', 'start of month', '-1 month') AND DATE('now', 'start of month', '-1 day'));",
|
||||
),
|
||||
context.env.D1.prepare(
|
||||
"SELECT approved, answered_at, created_by, performed_at, reached_minimum_player_count, type FROM events WHERE month = ? AND year = ?;",
|
||||
).bind(month, year),
|
||||
]);
|
||||
|
||||
const eventsQuery = await context.env.D1.prepare(
|
||||
"SELECT approved, answered_at, created_by, performed_at, reached_minimum_player_count, type FROM events WHERE month = ? AND year = ?;",
|
||||
)
|
||||
.bind(month, year)
|
||||
.all();
|
||||
const memberMap = Object.fromEntries(
|
||||
eventMemberQuery.results.map((entry) => {
|
||||
return [
|
||||
entry.id,
|
||||
{ name: entry.name, points: 0, roblox_id: entry.roblox_id },
|
||||
];
|
||||
batchStatements[0].results.map((e) => {
|
||||
return [e.id, { name: e.name, points: 0, roblox_id: e.roblox_id }];
|
||||
}),
|
||||
);
|
||||
|
||||
for (const event of eventsQuery.results as {
|
||||
for (const event of batchStatements[2].results as {
|
||||
approved: number;
|
||||
answered_at: number;
|
||||
created_by: string;
|
||||
@@ -77,10 +72,10 @@ export async function loader({ context }: { context: RequestContext }) {
|
||||
for (const member of Object.keys(memberMap))
|
||||
if (
|
||||
(memberMap[member].points < 50 ||
|
||||
eventsQuery.results.filter(
|
||||
batchStatements[2].results.filter(
|
||||
(e) => e.type === "gamenight" && e.created_by === member,
|
||||
).length === 0) &&
|
||||
!inactivityQuery.results.find(
|
||||
!batchStatements[1].results.find(
|
||||
(i) => i.uid === member && JSON.parse(i.decisions).et,
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user