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,11 +1,20 @@
export async function onRequest(context) {
console.log(context);
if (context.params.dump !== context.env.DUMP_KEY) {
return new Response("Wrong key");
}
const db = context.env.D1DB;
const dump = await db.dump();
return new Response(dump, {
const db = context.env.cryptopad;
console.log(db)
let accum = []
let cursor = undefined;
do {
const listResult = await db.list({cursor});
cursor = listResult.cursor;
accum = accum.concat(listResult.keys.map(x => x.name));
} while (cursor !== undefined);
const results = await Promise.all(accum.map(async x => ({key: x, value: await db.get(x)})));
return new Response(JSON.stringify(results), {
status: 200,
headers: {
'Content-Type': 'application/octet-stream'

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));
}

View File

@@ -4,7 +4,7 @@
"devDependencies": {
"better-sqlite3": "^8.0.1",
"jest": "^29.5.0",
"wrangler": "2.12.3"
"wrangler": "3.66.0"
},
"private": true,
"scripts": {

View File

@@ -8,3 +8,7 @@ DUMP_KEY = "dump"
binding = "D1DB"
database_name = "cryptopad"
database_id = "17080b9e-80c5-4253-8e7c-451ece72ed42"
[[kv_namespaces]]
binding = "cryptopad"
id = "f3899275342e429987dc34d37100b5b5"