Hello. I have a question. How would you connect a local Cloudflare D1 DB through a script? This is

Hello. I have a question. How would you connect a local Cloudflare D1 DB through a script?

This is exec.ts, I will want to use this script for seeding against my local environment.

import { Log, LogLevel, Miniflare } from "miniflare";

const nullScript =
  "export default {fetch: () => new Response(null, {status:404})};";
const mf = new Miniflare({
  modules: true,
  script: nullScript,
  d1Databases: { DB: "xxx-xxx" },
  log: new Log(LogLevel.INFO),
});

const env = await mf.getBindings();
const { results } = await (env.DB as D1Database)
  .prepare(`PRAGMA table_list;`)
  .all();
console.log(results);
await mf.dispose();


Then, I'm executing this with npx tsx exec.ts

The app’s D1 has a “users” table that I created through the wrangler d1 execute command. The Miniflare in the exec.ts doesn't acknowledge the “users” table.

I have only one D1 Database, and can confirm that the .wrangler/state/v3/d1/miniflare-D1DatabaseObject has only one set of files.

Your help is appreciated!
Was this page helpful?