Switch to CF KV instead of D1, more appropriate for the purpose

This commit is contained in:
Your Name
2024-07-24 01:47:20 -04:00
parent 491a1a3a4f
commit 808e848561
4 changed files with 23 additions and 10 deletions

View File

@@ -1,8 +1,8 @@
export async function onRequest(context) {
const db = context.env.D1DB;
const db = context.env.cryptopad;
if (context.params.storage[0] == "delete") {
const deleteReq = context.params.storage[1];
await db.prepare("DELETE FROM data WHERE k=?").bind(deleteReq).run();
await db.delete(deleteReq);
return new Response();
}
const key = context.params.storage[0];
@@ -13,10 +13,10 @@ export async function onRequest(context) {
console.log("Inserting", key, value);
// this doesn't use numbered parameters because miniflare behaves differently because better-sqlite3 doesn't handled numbered params well
// CF really needs named support and better-sqlite3 really needs to fix this
console.log(await db.prepare("INSERT INTO data (k, v) VALUES (?,?) ON CONFLICT(k) DO UPDATE SET v=? WHERE k=?").bind(key, value, value, key).run());
await db.put(key, value);
return new Response();
}
const value = await db.prepare("SELECT v FROM data WHERE k=?").bind(key).first("v");
const value = await db.get(key);
return new Response(JSON.stringify(value));
}