Created
October 25, 2024 12:51
-
-
Save mizchi/53d3316bda093b654b820bde177c47cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
[[durable_objects.bindings]] | |
name = "MyDurableObject" | |
class_name = "MyDurableObject" | |
[[migrations]] | |
tag = "v1" # Should be unique for each entry | |
new_sqlite_classes = ["MyDurableObject"] # Array of new classes | |
*/ | |
import { DurableObject } from "cloudflare:workers"; | |
export class MyDurableObject extends DurableObject { | |
sql: SqlStorage; | |
constructor(ctx: DurableObjectState, env: Env) { | |
super(ctx, env); | |
this.sql = ctx.storage.sql; | |
this.sql.exec(`CREATE TABLE IF NOT EXISTS artist( | |
artistid INTEGER PRIMARY KEY, | |
artistname TEXT | |
);`); | |
// INSERT INTO artist (artistid, artistname) VALUES | |
// (123, 'Alice'), | |
// (456, 'Bob'), | |
// (789, 'Charlie');`); | |
} | |
async query(queryString: string) { | |
const cur = this.sql.exec(queryString); | |
const ret = []; | |
for (const i of cur) { | |
ret.push(i); | |
} | |
return ret; | |
// return this.sql.exec(queryString).toArray(); | |
} | |
} | |
export default { | |
async fetch(request: Request, env: Env, ctx: any) { | |
// const d = await MyDurableObject. | |
console.log(env.MyDurableObject); | |
const id = env.MyDurableObject.idFromName("123"); | |
const do1 = env.MyDurableObject.get(id); | |
const ret = await do1.query("SELECT * FROM artist;"); | |
return Response.json(ret); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment