Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created September 2, 2023 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/e57c3f817e3acf5ff4d9f76b5bb6a1d0 to your computer and use it in GitHub Desktop.
Save mizchi/e57c3f817e3acf5ff4d9f76b5bb6a1d0 to your computer and use it in GitHub Desktop.
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