From 808e84856172ce9fe89f401d4a9ad1abaf47526b Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 24 Jul 2024 01:47:20 -0400 Subject: [PATCH] Switch to CF KV instead of D1, more appropriate for the purpose --- functions/dump/[dump].js | 17 +++++++++++++---- functions/storage/[[storage]].js | 8 ++++---- package.json | 2 +- wrangler.toml | 6 +++++- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/functions/dump/[dump].js b/functions/dump/[dump].js index 851cd2f..40edf9b 100644 --- a/functions/dump/[dump].js +++ b/functions/dump/[dump].js @@ -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' diff --git a/functions/storage/[[storage]].js b/functions/storage/[[storage]].js index 5474d68..16c80aa 100644 --- a/functions/storage/[[storage]].js +++ b/functions/storage/[[storage]].js @@ -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)); } diff --git a/package.json b/package.json index ee3c8cb..357f215 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/wrangler.toml b/wrangler.toml index 02cf37b..104c0ec 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -7,4 +7,8 @@ DUMP_KEY = "dump" [[ d1_databases ]] binding = "D1DB" database_name = "cryptopad" -database_id = "17080b9e-80c5-4253-8e7c-451ece72ed42" \ No newline at end of file +database_id = "17080b9e-80c5-4253-8e7c-451ece72ed42" + +[[kv_namespaces]] +binding = "cryptopad" +id = "f3899275342e429987dc34d37100b5b5"