From 45aed454f5945842dd6192900bc2744556855df2 Mon Sep 17 00:00:00 2001 From: James Martinez Date: Thu, 5 Feb 2026 09:37:14 -0600 Subject: [PATCH 1/6] Add support for Bun --- .github/workflows/ci.yaml | 22 +++++++++++++ packages/docs/docs/cli.mdx | 35 ++++++++++++++++++++ packages/docs/docs/configuration.mdx | 4 +++ packages/docs/docs/core-concepts.mdx | 4 +++ packages/docs/docs/dashboard.mdx | 4 +++ packages/docs/docs/migration-v6.mdx | 12 +++++++ packages/docs/docs/namespaces.mdx | 9 ++++++ packages/docs/docs/postgres.mdx | 4 +++ packages/docs/docs/production.mdx | 4 +++ packages/docs/docs/quickstart.mdx | 18 ++++++++++- packages/docs/docs/retries.mdx | 4 +++ packages/docs/docs/sqlite.mdx | 3 +- packages/docs/docs/workers.mdx | 24 ++++++++++++++ packages/docs/index.mdx | 4 +++ packages/openworkflow/README.md | 11 ++++--- packages/openworkflow/sqlite/backend.ts | 8 ++--- packages/openworkflow/sqlite/sqlite.test.ts | 17 ++++++++++ packages/openworkflow/sqlite/sqlite.ts | 36 +++++++++++++++++++-- vitest.config.ts | 6 ++++ 19 files changed, 214 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7364b981..6923828e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,3 +33,25 @@ jobs: - uses: codecov/codecov-action@v5 with: token: ${{ secrets.CODECOV_TOKEN }} + bun-tests: + runs-on: ubuntu-latest + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: + - uses: actions/checkout@v6 + - uses: actions/setup-node@v6 + with: + node-version-file: package.json + - run: npm ci + - uses: oven-sh/setup-bun@v2 + - run: bun run test diff --git a/packages/docs/docs/cli.mdx b/packages/docs/docs/cli.mdx index 6f79e912..e61ea9b9 100644 --- a/packages/docs/docs/cli.mdx +++ b/packages/docs/docs/cli.mdx @@ -17,6 +17,10 @@ npm install -D @openworkflow/cli pnpm add -D @openworkflow/cli ``` +```bash bun +bun add -D @openworkflow/cli +``` + ## Commands @@ -34,6 +38,10 @@ npx @openworkflow/cli init pnpx @openworkflow/cli init ``` +```bash bun +bunx @openworkflow/cli init +``` + This interactive command: @@ -59,6 +67,10 @@ npx @openworkflow/cli worker start pnpx @openworkflow/cli worker start ``` +```bash bun +bunx @openworkflow/cli worker start +``` + Options: @@ -77,6 +89,11 @@ npx @openworkflow/cli worker start --concurrency 4 pnpx @openworkflow/cli worker start --concurrency 4 ``` +```bash bun +# Run with 4 concurrent workflows +bunx @openworkflow/cli worker start --concurrency 4 +``` + The worker: @@ -100,6 +117,10 @@ npx @openworkflow/cli dashboard pnpx @openworkflow/cli dashboard ``` +```bash bun +bunx @openworkflow/cli dashboard +``` + This launches the OpenWorkflow dashboard on `http://localhost:3000`. See @@ -118,6 +139,10 @@ npx @openworkflow/cli doctor pnpx @openworkflow/cli doctor ``` +```bash bun +bunx @openworkflow/cli doctor +``` + This command checks: @@ -164,6 +189,10 @@ npx @openworkflow/cli --version pnpx @openworkflow/cli --version ``` +```bash bun +bunx @openworkflow/cli --version +``` + ### `--help` @@ -183,6 +212,12 @@ pnpx @openworkflow/cli worker --help pnpx @openworkflow/cli worker start --help ``` +```bash bun +bunx @openworkflow/cli --help +bunx @openworkflow/cli worker --help +bunx @openworkflow/cli worker start --help +``` + ## Environment Variables diff --git a/packages/docs/docs/configuration.mdx b/packages/docs/docs/configuration.mdx index ca42ad26..77cc8cb1 100644 --- a/packages/docs/docs/configuration.mdx +++ b/packages/docs/docs/configuration.mdx @@ -17,6 +17,10 @@ npm i openworkflow postgres pnpm add openworkflow postgres ``` +```bash bun +bun add openworkflow postgres +``` + ## Config File diff --git a/packages/docs/docs/core-concepts.mdx b/packages/docs/docs/core-concepts.mdx index 0cfa9677..d5aa7ff3 100644 --- a/packages/docs/docs/core-concepts.mdx +++ b/packages/docs/docs/core-concepts.mdx @@ -71,6 +71,10 @@ npx @openworkflow/cli worker start pnpx @openworkflow/cli worker start ``` +```bash bun +bunx @openworkflow/cli worker start +``` + ## How it Works diff --git a/packages/docs/docs/dashboard.mdx b/packages/docs/docs/dashboard.mdx index 5334d993..6f815252 100644 --- a/packages/docs/docs/dashboard.mdx +++ b/packages/docs/docs/dashboard.mdx @@ -17,6 +17,10 @@ npx @openworkflow/cli dashboard pnpx @openworkflow/cli dashboard ``` +```bash bun +bunx @openworkflow/cli dashboard +``` + The dashboard starts on `http://localhost:3000`. diff --git a/packages/docs/docs/migration-v6.mdx b/packages/docs/docs/migration-v6.mdx index 0836eead..1f0db5e6 100644 --- a/packages/docs/docs/migration-v6.mdx +++ b/packages/docs/docs/migration-v6.mdx @@ -36,6 +36,10 @@ npm uninstall @openworkflow/backend-postgres @openworkflow/backend-sqlite pnpm remove @openworkflow/backend-postgres @openworkflow/backend-sqlite ``` +```bash bun +bun remove @openworkflow/backend-postgres @openworkflow/backend-sqlite +``` + If you're using PostgreSQL, install the `postgres` driver directly: @@ -49,6 +53,10 @@ npm install postgres pnpm add postgres ``` +```bash bun +bun add postgres +``` + Your `package.json` should now look like: @@ -79,6 +87,10 @@ npx @openworkflow/cli init pnpx @openworkflow/cli init ``` +```bash bun +bunx @openworkflow/cli init +``` + The CLI now generates code with the new import paths automatically. diff --git a/packages/docs/docs/namespaces.mdx b/packages/docs/docs/namespaces.mdx index 9c4f0ea5..4a2a8016 100644 --- a/packages/docs/docs/namespaces.mdx +++ b/packages/docs/docs/namespaces.mdx @@ -79,6 +79,15 @@ NAMESPACE=staging pnpm run worker NAMESPACE=production pnpm run worker ``` +```bash bun +# Development +NAMESPACE=development bun run worker +# Staging +NAMESPACE=staging bun run worker +# Production +NAMESPACE=production bun run worker +``` + ### Multi-Tenancy diff --git a/packages/docs/docs/postgres.mdx b/packages/docs/docs/postgres.mdx index e464353f..570d955b 100644 --- a/packages/docs/docs/postgres.mdx +++ b/packages/docs/docs/postgres.mdx @@ -20,6 +20,10 @@ npm i openworkflow postgres pnpm add openworkflow postgres ``` +```bash bun +bun add openworkflow postgres +``` + ## Setup diff --git a/packages/docs/docs/production.mdx b/packages/docs/docs/production.mdx index 38deb6cf..ccabbfbd 100644 --- a/packages/docs/docs/production.mdx +++ b/packages/docs/docs/production.mdx @@ -45,6 +45,10 @@ npx @openworkflow/cli dashboard pnpx @openworkflow/cli dashboard ``` +```bash bun +bunx @openworkflow/cli dashboard +``` + - Review step attempts and errors for failed workflows diff --git a/packages/docs/docs/quickstart.mdx b/packages/docs/docs/quickstart.mdx index 1a06b7d1..4fdfa8d3 100644 --- a/packages/docs/docs/quickstart.mdx +++ b/packages/docs/docs/quickstart.mdx @@ -5,7 +5,7 @@ description: Get up and running with OpenWorkflow ## Prerequisites -- Node.js +- Node.js or Bun - PostgreSQL (and/or SQLite) ## 1. Install @@ -21,6 +21,10 @@ npx @openworkflow/cli init pnpx @openworkflow/cli init ``` +```bash bun +bunx @openworkflow/cli init +``` + The CLI will help you set up OpenWorkflow, create your `openworkflow.config.ts`, @@ -37,6 +41,10 @@ npx @openworkflow/cli worker start pnpx @openworkflow/cli worker start ``` +```bash bun +bunx @openworkflow/cli worker start +``` + This starts the worker using `openworkflow.config.{ts,js}` and auto-loads @@ -59,6 +67,10 @@ pnpx tsx openworkflow/hello-world.run.ts node openworkflow/hello-world.run.js ``` +```bash bun +bun openworkflow/hello-world.run.ts +``` + This script runs the `helloWorld` workflow and waits for it to complete. @@ -81,6 +93,10 @@ npx @openworkflow/cli dashboard pnpx @openworkflow/cli dashboard ``` +```bash bun +bunx @openworkflow/cli dashboard +``` + The dashboard provides a UI for monitoring workflow runs, viewing step details, diff --git a/packages/docs/docs/retries.mdx b/packages/docs/docs/retries.mdx index 96b1ec65..4cf98c1e 100644 --- a/packages/docs/docs/retries.mdx +++ b/packages/docs/docs/retries.mdx @@ -164,4 +164,8 @@ npx @openworkflow/cli dashboard pnpx @openworkflow/cli dashboard ``` +```bash bun +bunx @openworkflow/cli dashboard +``` + diff --git a/packages/docs/docs/sqlite.mdx b/packages/docs/docs/sqlite.mdx index f81ed5e1..7e6a3d4c 100644 --- a/packages/docs/docs/sqlite.mdx +++ b/packages/docs/docs/sqlite.mdx @@ -6,7 +6,8 @@ description: Lightweight SQLite storage for development and testing The SQLite backend is perfect for local development, testing, and single-server deployments. It requires no external database server. -Requires Node.js 22.5+ for `node:sqlite`. +Uses `bun:sqlite` for Bun and `node:sqlite` for Node.js (requires Node.js +22.5+). ## Setup diff --git a/packages/docs/docs/workers.mdx b/packages/docs/docs/workers.mdx index 59358751..22cdd4ae 100644 --- a/packages/docs/docs/workers.mdx +++ b/packages/docs/docs/workers.mdx @@ -20,6 +20,10 @@ npx @openworkflow/cli worker start pnpx @openworkflow/cli worker start ``` +```bash bun +bunx @openworkflow/cli worker start +``` + This command: @@ -68,6 +72,10 @@ npx @openworkflow/cli worker start --concurrency 10 pnpx @openworkflow/cli worker start --concurrency 10 ``` +```bash bun +bunx @openworkflow/cli worker start --concurrency 10 +``` + @@ -107,6 +115,13 @@ pnpx @openworkflow/cli worker start --concurrency 10 pnpx @openworkflow/cli worker start --concurrency 10 ``` +```bash bun +# Terminal 1 +bunx @openworkflow/cli worker start --concurrency 10 +# Terminal 2 +bunx @openworkflow/cli worker start --concurrency 10 +``` + Workers coordinate through the database: @@ -145,6 +160,15 @@ pnpx @openworkflow/cli worker start # [info] Worker stopped ``` +```bash bun +# The worker handles Ctrl+C gracefully +bunx @openworkflow/cli worker start +^C +# [info] Shutting down worker... +# [info] Waiting for active workflows to complete... +# [info] Worker stopped +``` + ## Workflow Discovery diff --git a/packages/docs/index.mdx b/packages/docs/index.mdx index 62f526d6..57e30e68 100644 --- a/packages/docs/index.mdx +++ b/packages/docs/index.mdx @@ -14,6 +14,10 @@ npx @openworkflow/cli init pnpx @openworkflow/cli init ``` +```bash bun +bunx @openworkflow/cli init +``` + ![OpenWorkflow Dashboard](/assets/dashboard.png) diff --git a/packages/openworkflow/README.md b/packages/openworkflow/README.md index c28447df..dc782e63 100644 --- a/packages/openworkflow/README.md +++ b/packages/openworkflow/README.md @@ -41,12 +41,15 @@ export const sendWelcomeEmail = defineWorkflow( ## Quick Start -**Prerequisites:** Node.js & PostgreSQL (or SQLite) - -### Install - ```bash +# npm npx @openworkflow/cli init + +# pnpm +pnpx @openworkflow/cli init + +# bun +bunx @openworkflow/cli init ``` The CLI will guide you through setup and generate everything you need to get diff --git a/packages/openworkflow/sqlite/backend.ts b/packages/openworkflow/sqlite/backend.ts index 3c913c3f..ba875cb2 100644 --- a/packages/openworkflow/sqlite/backend.ts +++ b/packages/openworkflow/sqlite/backend.ts @@ -532,9 +532,7 @@ export class BackendSqlite implements Backend { }); } - const rows = rawRows.map((row) => - rowToWorkflowRun(row as unknown as WorkflowRunRow), - ); + const rows = rawRows.map((row) => rowToWorkflowRun(row as WorkflowRunRow)); return Promise.resolve( this.processPaginationResults(rows, limit, !!after, !!before), @@ -601,9 +599,7 @@ export class BackendSqlite implements Backend { }); } - const rows = rawRows.map((row) => - rowToStepAttempt(row as unknown as StepAttemptRow), - ); + const rows = rawRows.map((row) => rowToStepAttempt(row as StepAttemptRow)); return Promise.resolve( this.processPaginationResults(rows, limit, !!after, !!before), diff --git a/packages/openworkflow/sqlite/sqlite.test.ts b/packages/openworkflow/sqlite/sqlite.test.ts index 1a6ac82f..cb5b1d2a 100644 --- a/packages/openworkflow/sqlite/sqlite.test.ts +++ b/packages/openworkflow/sqlite/sqlite.test.ts @@ -50,6 +50,23 @@ describe("sqlite", () => { } }); + test("newDatabase does not depend on global require", () => { + const globals = globalThis as { require?: unknown }; + const originalRequire = globals.require; + + try { + globals.require = undefined; + const tempDb = newDatabase(":memory:"); + tempDb.close(); + } finally { + if (originalRequire === undefined) { + delete globals.require; + } else { + globals.require = originalRequire; + } + } + }); + describe("migrations()", () => { test("returns migration SQL statements with correct table names", () => { const migs = migrations(); diff --git a/packages/openworkflow/sqlite/sqlite.ts b/packages/openworkflow/sqlite/sqlite.ts index 332ea26e..709ecfcb 100644 --- a/packages/openworkflow/sqlite/sqlite.ts +++ b/packages/openworkflow/sqlite/sqlite.ts @@ -1,15 +1,45 @@ import { randomUUID } from "node:crypto"; -import { DatabaseSync } from "node:sqlite"; +import { createRequire } from "node:module"; -export type Database = DatabaseSync; +const require = createRequire(import.meta.url); + +/** + * Common database interface that both Node and Bun SQLite drivers satisfy. + */ +export interface Database { + exec(sql: string): void; + prepare(sql: string): { + run(...params: unknown[]): { changes: number }; + get(...params: unknown[]): unknown; + all(...params: unknown[]): unknown[]; + }; + close(): void; +} /** * newDatabase creates a new SQLite database connection. + * Uses node:sqlite in Node.js or bun:sqlite in Bun. * @param path - Database file path (or ":memory:") for testing * @returns SQLite database connection */ export function newDatabase(path: string): Database { - const db = new DatabaseSync(path); + let db: Database; + + const isBun = (globalThis as { Bun?: unknown }).Bun !== undefined; + + if (isBun) { + /* v8 ignore start -- Bun tests are run separately */ + const { Database: BunDatabase } = require("bun:sqlite") as { + Database: new (path: string) => Database; + }; + db = new BunDatabase(path); + /* v8 ignore stop */ + } else { + const { DatabaseSync } = require("node:sqlite") as { + DatabaseSync: new (path: string) => Database; + }; + db = new DatabaseSync(path); + } // Only enable WAL mode for file-based databases if (path !== ":memory:") { db.exec("PRAGMA journal_mode = WAL;"); diff --git a/vitest.config.ts b/vitest.config.ts index 40d525e2..10e3516b 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -21,5 +21,11 @@ export default defineConfig({ lines: 90, }, }, + // fix ESM resolution issues when running tests with Bun + server: { + deps: { + inline: ["arktype", "valibot", "yup", "zod"], + }, + }, }, }); From 567707e8deafc24f6aa0386b17c8364a7b298cfe Mon Sep 17 00:00:00 2001 From: James Martinez Date: Thu, 5 Feb 2026 09:47:07 -0600 Subject: [PATCH 2/6] Add `bun run ci:bun` command for Bun CI --- .github/workflows/ci.yaml | 9 +++------ package.json | 1 + 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6923828e..051616bb 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,7 +33,7 @@ jobs: - uses: codecov/codecov-action@v5 with: token: ${{ secrets.CODECOV_TOKEN }} - bun-tests: + ci-bun: runs-on: ubuntu-latest services: postgres: @@ -49,9 +49,6 @@ jobs: --health-retries 5 steps: - uses: actions/checkout@v6 - - uses: actions/setup-node@v6 - with: - node-version-file: package.json - - run: npm ci - uses: oven-sh/setup-bun@v2 - - run: bun run test + - run: bun ci + - run: bun run ci:bun diff --git a/package.json b/package.json index da1d19a9..bfeff43e 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "scripts": { "build": "turbo build", "ci": "npm run format && npm run build && npm run lint && npm run typecheck && npm run test:coverage", + "ci:bun": "bun run build && bun run test", "db:migrate": "tsx ./packages/openworkflow/postgres/scripts/db-migrate.ts", "db:reset": "tsx ./packages/openworkflow/postgres/scripts/db-reset.ts", "format": "prettier --check . --ignore-path .gitignore --ignore-path .prettierignore", From 57b566840ffbb433feb6cb3aeaf36ef965644a64 Mon Sep 17 00:00:00 2001 From: James Martinez Date: Thu, 5 Feb 2026 09:59:28 -0600 Subject: [PATCH 3/6] global require --- packages/openworkflow/sqlite/sqlite.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/openworkflow/sqlite/sqlite.ts b/packages/openworkflow/sqlite/sqlite.ts index 709ecfcb..210195ca 100644 --- a/packages/openworkflow/sqlite/sqlite.ts +++ b/packages/openworkflow/sqlite/sqlite.ts @@ -1,7 +1,4 @@ import { randomUUID } from "node:crypto"; -import { createRequire } from "node:module"; - -const require = createRequire(import.meta.url); /** * Common database interface that both Node and Bun SQLite drivers satisfy. @@ -29,16 +26,18 @@ export function newDatabase(path: string): Database { if (isBun) { /* v8 ignore start -- Bun tests are run separately */ + // eslint-disable-next-line @typescript-eslint/no-require-imports const { Database: BunDatabase } = require("bun:sqlite") as { Database: new (path: string) => Database; }; db = new BunDatabase(path); /* v8 ignore stop */ } else { - const { DatabaseSync } = require("node:sqlite") as { + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { DatabaseSync: NodeDatabase } = require("node:sqlite") as { DatabaseSync: new (path: string) => Database; }; - db = new DatabaseSync(path); + db = new NodeDatabase(path); } // Only enable WAL mode for file-based databases if (path !== ":memory:") { From 4822f103f31cf31698934604670799b34771f4eb Mon Sep 17 00:00:00 2001 From: James Martinez Date: Thu, 5 Feb 2026 10:03:58 -0600 Subject: [PATCH 4/6] Fix bun detection --- packages/openworkflow/sqlite/sqlite.test.ts | 17 ----------------- packages/openworkflow/sqlite/sqlite.ts | 3 ++- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/packages/openworkflow/sqlite/sqlite.test.ts b/packages/openworkflow/sqlite/sqlite.test.ts index cb5b1d2a..1a6ac82f 100644 --- a/packages/openworkflow/sqlite/sqlite.test.ts +++ b/packages/openworkflow/sqlite/sqlite.test.ts @@ -50,23 +50,6 @@ describe("sqlite", () => { } }); - test("newDatabase does not depend on global require", () => { - const globals = globalThis as { require?: unknown }; - const originalRequire = globals.require; - - try { - globals.require = undefined; - const tempDb = newDatabase(":memory:"); - tempDb.close(); - } finally { - if (originalRequire === undefined) { - delete globals.require; - } else { - globals.require = originalRequire; - } - } - }); - describe("migrations()", () => { test("returns migration SQL statements with correct table names", () => { const migs = migrations(); diff --git a/packages/openworkflow/sqlite/sqlite.ts b/packages/openworkflow/sqlite/sqlite.ts index 210195ca..1e12ee1f 100644 --- a/packages/openworkflow/sqlite/sqlite.ts +++ b/packages/openworkflow/sqlite/sqlite.ts @@ -22,7 +22,8 @@ export interface Database { export function newDatabase(path: string): Database { let db: Database; - const isBun = (globalThis as { Bun?: unknown }).Bun !== undefined; + // https://bun.com/docs/guides/util/detect-bun + const isBun = !!process.versions["bun"]; if (isBun) { /* v8 ignore start -- Bun tests are run separately */ From 9a76b77c69cff4958a9fc0eed10c5f11cbc8d9fd Mon Sep 17 00:00:00 2001 From: James Martinez Date: Thu, 5 Feb 2026 10:13:48 -0600 Subject: [PATCH 5/6] Run Bun tests in Bun env --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index bfeff43e..1c6712e3 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "scripts": { "build": "turbo build", "ci": "npm run format && npm run build && npm run lint && npm run typecheck && npm run test:coverage", - "ci:bun": "bun run build && bun run test", + "ci:bun": "bun run build && bun run test:bun", "db:migrate": "tsx ./packages/openworkflow/postgres/scripts/db-migrate.ts", "db:reset": "tsx ./packages/openworkflow/postgres/scripts/db-reset.ts", "format": "prettier --check . --ignore-path .gitignore --ignore-path .prettierignore", @@ -22,6 +22,7 @@ "pghero": "sh ./packages/openworkflow/postgres/scripts/pghero.sh", "squawk": "tsx ./packages/openworkflow/postgres/scripts/squawk.ts", "test": "vitest run", + "test:bun": "bun run --bun vitest run", "test:coverage": "vitest run --coverage", "test:coverage:browse": "open ./coverage/index.html", "test:watch": "vitest", From 6da48bf6d5c63faa788ce37208ab3e35bfcb312a Mon Sep 17 00:00:00 2001 From: James Martinez Date: Thu, 5 Feb 2026 10:22:04 -0600 Subject: [PATCH 6/6] Bring back createRequire --- packages/openworkflow/sqlite/sqlite.test.ts | 19 +++++++++++++++++++ packages/openworkflow/sqlite/sqlite.ts | 7 +++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/openworkflow/sqlite/sqlite.test.ts b/packages/openworkflow/sqlite/sqlite.test.ts index 1a6ac82f..e08cfa88 100644 --- a/packages/openworkflow/sqlite/sqlite.test.ts +++ b/packages/openworkflow/sqlite/sqlite.test.ts @@ -50,6 +50,25 @@ describe("sqlite", () => { } }); + // a bit of a hacky test, but needed since vitest still loads requires even + // though the package is type:module + test("newDatabase does not depend on global require", () => { + const globals = globalThis as { require?: unknown }; + const originalRequire = globals.require; + + try { + globals.require = undefined; + const tempDb = newDatabase(":memory:"); + tempDb.close(); + } finally { + if (originalRequire === undefined) { + delete globals.require; + } else { + globals.require = originalRequire; + } + } + }); + describe("migrations()", () => { test("returns migration SQL statements with correct table names", () => { const migs = migrations(); diff --git a/packages/openworkflow/sqlite/sqlite.ts b/packages/openworkflow/sqlite/sqlite.ts index 1e12ee1f..bf3fd163 100644 --- a/packages/openworkflow/sqlite/sqlite.ts +++ b/packages/openworkflow/sqlite/sqlite.ts @@ -1,4 +1,5 @@ import { randomUUID } from "node:crypto"; +import { createRequire } from "node:module"; /** * Common database interface that both Node and Bun SQLite drivers satisfy. @@ -20,6 +21,10 @@ export interface Database { * @returns SQLite database connection */ export function newDatabase(path: string): Database { + // needed for Node ESM, also supported in Bun + // https://bun.com/reference/node/module/default/createRequire + const require = createRequire(import.meta.url); + let db: Database; // https://bun.com/docs/guides/util/detect-bun @@ -27,14 +32,12 @@ export function newDatabase(path: string): Database { if (isBun) { /* v8 ignore start -- Bun tests are run separately */ - // eslint-disable-next-line @typescript-eslint/no-require-imports const { Database: BunDatabase } = require("bun:sqlite") as { Database: new (path: string) => Database; }; db = new BunDatabase(path); /* v8 ignore stop */ } else { - // eslint-disable-next-line @typescript-eslint/no-require-imports const { DatabaseSync: NodeDatabase } = require("node:sqlite") as { DatabaseSync: new (path: string) => Database; };