@variablesoftware/mock-d1
Version:
๐๏ธ๐๏ธ๐ง Mock D1 Database implementation for testing Cloudflare Workers
49 lines (48 loc) โข 2.03 kB
TypeScript
/**
* mockD1Database.ts v3 ๐ง (updated for named bind support)
*
* A lightweight, test-optimized mock of Cloudflare's D1Database interface.
* Designed for Workers unit testing without requiring a live D1 instance.
*
* โ
Supported SQL commands (simplified parsing):
* - CREATE TABLE [IF NOT EXISTS]
* - DROP TABLE (stubbed via `delete db[table]` expected externally)
* - INSERT INTO ... VALUES (...)
* - SELECT * FROM ... WHERE ... (supports AND/OR + named binds)
* - DELETE FROM ... WHERE ... (with accurate changes count)
*
* ๐งฉ Internal mechanics:
* - Tables stored in `Map<string, { rows: D1Row[] }>`
* - Uses basic regex parsing โ not SQL-standards compliant
* - No type coercion โ binds and data are raw JS primitives
* - Validation of :bind args occurs during execution, not .bind()
* - Logs behavior when LOG includes "D1" at level 3 or above
*
* ๐งช Test-only helpers:
* - `inject(table, rows[])`: manually preload test data
* - `dump()`: returns full mock DB snapshot
* - `batch()`: stubbed to return empty D1Result[]
*
* ๐งฑ Design notes:
* - Safe for isolated tests (no persistence between runs)
* - Reflects Cloudflare D1 runtime quirks (bind usage, defer errors)
* - Implements prepare().bind().run()/all()/first()/raw() interface
*
* ๐ง Future support ideas:
* - UPDATE ... SET ... WHERE ...
* - LIKE, BETWEEN, NOT, nested conditions
* - JOIN or mock link handling
* - ORDER BY, LIMIT
*/
import { D1Database } from "./types/MockD1Database.js";
/**
* Creates a new mock D1 database instance.
*
* @returns An object implementing the mock D1Database interface, including:
* - prepare(sql): prepares a statement for execution
* - batch(statements): executes multiple statements in parallel
* - dump(): returns a snapshot of the current database state
* - inject(table, rows): preloads data into a table
* - withSession(): returns a session-scoped interface
*/
export declare function mockD1Database(): D1Database;