Add basic D1 support
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
cryptopad.db
|
cryptopad.db
|
||||||
|
node_modules
|
||||||
|
.wrangler
|
||||||
|
|||||||
1
d1setup.sql
Normal file
1
d1setup.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
CREATE TABLE data (k BLOB, v BLOB, PRIMARY KEY (`k`));
|
||||||
22
functions/storage/[[storage]].js
Normal file
22
functions/storage/[[storage]].js
Normal 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
9191
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
package.json
Normal file
14
package.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
7
wrangler.toml
Normal 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"
|
||||||
Reference in New Issue
Block a user