KV to D1 migration (this is totally gonna break something)

This commit is contained in:
2024-05-12 01:25:46 -04:00
parent a2b3391bda
commit e00b7e8c55
24 changed files with 1835 additions and 669 deletions

View File

@ -8,42 +8,12 @@ export async function onRequestGet(context: RequestContext) {
results: { id: string }[];
success: boolean;
} = await context.env.D1.prepare(
"SELECT id FROM reports WHERE user = ? ORDER BY created_at LIMIT 50;",
"SELECT created_at, id, open, target_usernames FROM reports WHERE json_extract(user, '$.id') = ? ORDER BY created_at LIMIT 50;",
)
.bind(context.data.current_user.id)
.all();
if (!success) return jsonError("Failed to retrieve reports", 500);
const ids: string[] = [];
results.map((v) => ids.push(v.id));
const kvDataPromises = [];
for (const id of ids)
kvDataPromises.push(context.env.DATA.get(`report_${id}`, { type: "json" }));
let settledKvPromises;
try {
settledKvPromises = (await Promise.all(
kvDataPromises,
)) as ReportCardProps[];
} catch (e) {
console.log(e);
return jsonError("Failed to resolve reports", 500);
}
return jsonResponse(
JSON.stringify(
settledKvPromises.map((r) => {
return {
created_at: r.created_at,
id: r.id,
open: r.open,
target_usernames: r.target_usernames,
};
}),
),
);
return jsonResponse(JSON.stringify(results));
}