Add basic D1 support

This commit is contained in:
Your Name
2023-03-15 19:27:25 -04:00
parent 7acd546481
commit bdda2bcb8d
8 changed files with 9238 additions and 1 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
cryptopad.db cryptopad.db
node_modules
.wrangler

1
d1setup.sql Normal file
View File

@@ -0,0 +1 @@
CREATE TABLE data (k BLOB, v BLOB, PRIMARY KEY (`k`));

View File

@@ -0,0 +1,22 @@
export async function onRequest(context) {
const db = context.env.D1DB;
if (context.params.storage[0] == "delete") {
const deleteReq = context.params.storage[1];
await db.prepare("DELETE FROM data WHERE k=?").bind(deleteReq).run();
return new Response();
}
const key = context.params.storage[0];
console.log(context);
if (context.request.method == "POST") {
const formData = await context.request.formData();
const value = formData.get("value");
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());
return new Response();
}
const value = await db.prepare("SELECT v FROM data WHERE k=?").bind(key).first("v");
return new Response(JSON.stringify(value));
}

9191
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

14
package.json Normal file
View File

@@ -0,0 +1,14 @@
{
"name": "cryptopad",
"version": "0.0.0",
"devDependencies": {
"better-sqlite3": "^8.0.1",
"jest": "^29.5.0",
"wrangler": "2.12.3"
},
"private": true,
"scripts": {
"start": "wrangler pages dev static --d1=D1DB --persist",
"deploy": "wrangler pages publish static --project-name cryptopad"
}
}

View File

@@ -29,7 +29,7 @@ def mystatic(filepath):
@app.route('/') @app.route('/')
def index(): def index():
return bottle.static_file("cryptopad.html", root='static') return bottle.static_file("index.html", root='static')
if __name__ == "__main__": if __name__ == "__main__":
bottle.debug(True) bottle.debug(True)

7
wrangler.toml Normal file
View File

@@ -0,0 +1,7 @@
name = "cryptopad"
compatibility_date = "2023-03-15"
[[ d1_databases ]]
binding = "D1DB"
database_name = "cryptopad"
database_id = "17080b9e-80c5-4253-8e7c-451ece72ed42"