Skip to content

Latest commit

History

History
44 lines (40 loc) 路 1.09 KB

README.md

File metadata and controls

44 lines (40 loc) 路 1.09 KB

@miniflare/d1

Workers D1 module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers. See 馃摝 D1 for more details.

Example

import { BetaDatabase } from "@miniflare/d1";
import { MemoryStorage } from "@miniflare/storage-memory";
const db = new BetaDatabase(new MemoryStorage());

// BetaDatabase only supports .fetch(), once D1 is out of beta the full API will be available here:
await db.fetch("/execute", {
  method: "POST",
  body: JSON.stringify({
    sql: `CREATE TABLE my_table (cid INTEGER PRIMARY KEY, name TEXT NOT NULL);`,
  }),
});
const response = await db.fetch("/query", {
  method: "POST",
  body: JSON.stringify({
    sql: `SELECT * FROM sqlite_schema`,
  }),
});
console.log(await response.json());
/*
{
  "success": true,
  "result": [
    [
      {
        "type": "table",
        "name": "my_table",
        "tbl_name": "my_table",
        "rootpage": 2,
        "sql": "CREATE TABLE my_table (cid INTEGER PRIMARY KEY, name TEXT NOT NULL)"
      }
    ]
  ]
}
*/