@pondoknusa/database-d1
v7.0.0
Published
Cloudflare D1 database driver for Pondoknusa
Readme
@pondoknusa/database-d1
Cloudflare D1 driver for Pondoknusa — serverless SQLite over a Workers binding or the REST API.
A Pondoknusa app runs on SQLite (node:sqlite) by default, so a fresh project pulls in no database
SDK at all. This package adds Cloudflare D1 on top: it registers a d1 driver with
@pondoknusa/database's DatabaseManager, reachable through a Workers binding or Cloudflare's
REST API. D1 speaks the SQLite dialect (SqliteGrammar) and needs no third-party SDK.
@pondoknusa/database is a peer dependency — the driver calls DatabaseManager.extend(), which
mutates a module-static registry, so the app must supply exactly one shared copy of the host package.
Part of the Pondoknusa framework — a TypeScript-native full-stack framework built on standard Web APIs. Requires Node.js ≥ 26.
Install
npm install @pondoknusa/database-d1 @pondoknusa/databaseNo third-party database SDK is required — the driver reaches D1 over the Workers binding or the Cloudflare REST API.
Quick start
Register the service provider before the database provider boots in src/main.ts:
import { Application, DatabaseServiceProvider } from '@pondoknusa/core';
import { D1DatabaseServiceProvider } from '@pondoknusa/database-d1';
const app = new Application(import.meta.dirname);
app.register(D1DatabaseServiceProvider);
app.register(DatabaseServiceProvider);
await app.boot();Configuration
// config/database.ts
import type { D1ConnectionConfig } from '@pondoknusa/database-d1';
import { env } from '@pondoknusa/config';
export default {
default: env('DB_CONNECTION', 'd1'),
connections: {
d1: {
driver: 'd1',
accountId: env('CLOUDFLARE_ACCOUNT_ID'),
databaseId: env('D1_DATABASE_ID'),
apiToken: env('CLOUDFLARE_API_TOKEN'),
} satisfies D1ConnectionConfig,
},
} as const;Inside a Worker, point the driver at the D1 binding instead:
d1: {
driver: 'd1',
binding: env.DB, // the Workers D1 binding
} satisfies D1ConnectionConfig,Read-after-write consistency
The driver classifies every statement — SELECT and PRAGMA as reads, everything else as writes —
issuing reads with statement.all() and writes with statement.run(), and waits for each write's
meta.changes / meta.last_row_id before resolving, so a completed write is visible to the queries
that follow. Interactive BEGIN/COMMIT is not available on every D1 backend, so prefer short,
single-statement writes when unsure.
Queries go through the same ORM and query builder as every other connection:
import { DB } from '@pondoknusa/core';
import { User } from './models/user.js';
const rows = await DB.connection('d1').query('SELECT * FROM users WHERE active = ?', [1]);
const user = await User.query().where('email', '[email protected]').first();Related packages
@pondoknusa/database— the ORM, query builder, and migrations this driver plugs into.@pondoknusa/database-pg,@pondoknusa/database-mysql,@pondoknusa/database-mssql,@pondoknusa/database-oracle— the other first-party drivers.
Documentation
MIT © Pondoknusa Contributors
