Created
September 2, 2023 16:39
-
-
Save mizchi/e57c3f817e3acf5ff4d9f76b5bb6a1d0 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
import { Miniflare } from "miniflare"; | |
const mf = new Miniflare({ | |
modules: true, | |
script: `export default { | |
async fetch(req, env, ctx) { | |
return new Response("ok"); | |
} | |
} | |
`, | |
d1Persist: true, | |
d1Databases: { | |
"DB": "db" | |
} | |
}); | |
// const response = await mf.dispatchFetch("http://localhost:8787/"); | |
// console.log(await response.text()); // Hello Miniflare! | |
const bindings = await mf.getBindings(); | |
const db = bindings.DB; | |
await db.exec("CREATE TABLE IF NOT EXISTS requests (url TEXT)"); | |
await db.exec("INSERT INTO requests (url) VALUES ('aaa')"); | |
await db.exec("INSERT INTO requests (url) VALUES ('bbb')"); | |
await db.exec("INSERT INTO requests (url) VALUES ('ccc')"); | |
const prepared = await db.prepare(`select * from "requests"`); | |
const res = await prepared.all(); | |
// const res = await db.exec(`select * from "requests"`).; | |
console.log(res); | |
await mf.dispose(); | |
/* results | |
$ node mf.mjs | |
{ | |
success: true, | |
meta: { | |
duration: 0.15012499690055847, | |
last_row_id: 0, | |
changes: 0, | |
served_by: 'miniflare.db', | |
internal_stats: null | |
}, | |
results: [ | |
{ url: 'aaa' }, | |
{ url: 'bbb' }, | |
{ url: 'ccc' }, | |
] | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment