Registry / database / d1-orm

d1-orm

JSON →
library0.9.2jsnpmunverified

A simple, strictly typed ORM for Cloudflare's D1 SQLite database. Version 0.9.2 provides a TypeScript-first API with inferred types, supporting CRUD operations, migrations, and query building. It differentiates from raw D1 usage by offering model-based schema definition, automatic type inference via Infer, and common ORM patterns like first/all/insert/update/delete. Designed for Cloudflare Workers, it requires @cloudflare/workers-types v3.16.0+ and is actively maintained with a focus on type safety.

npm install d1-orm
INSTALL
IMPORT
SIG · D1-ORM
D
d1-orm
databasejavascriptv0.9.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

D1Orm
import { D1Orm } from 'd1-orm'
const D1Orm = require('d1-orm')
Package is ESM-only; CommonJS require will fail.
DataTypes
import { DataTypes } from 'd1-orm'
import { DataTypes } from 'd1-orm/DataTypes'
DataTypes is a named export from main entry, not a submodule.
Model
import { Model } from 'd1-orm'
import Model from 'd1-orm'
Model is a named export, not a default export.
Infer
import type { Infer } from 'd1-orm'
import { Infer } from 'd1-orm'
Infer is a type-only export; should be imported using 'import type'.

Defines a User model with automatic type inference and retrieves the first record matching a condition.

import { D1Orm, DataTypes, Model } from 'd1-orm'; import type { Infer } from 'd1-orm'; interface Env { DB: D1Database; } export default { async fetch(request: Request, env: Env) { const orm = new D1Orm(env.DB); const users = new Model( { D1Orm: orm, tableName: 'users', primaryKeys: 'id', autoIncrement: 'id', uniqueKeys: [['email']], }, { id: { type: DataTypes.INTEGER, notNull: true }, name: { type: DataTypes.STRING, notNull: true, defaultValue: 'John Doe' }, email: { type: DataTypes.STRING }, } ); type User = Infer<typeof users>; const user = await users.First({ where: { id: 1 } }); return new Response(JSON.stringify(user)); }, };
Debug
Known issues
breakingD1Orm constructor signature changed: now takes D1Database instance directly (no wrapper object).
fix
Update constructor call: new D1Orm(env.DB) instead of new D1Orm({ D1: env.DB }).
affects: <0.8.0
deprecatedThe `createTable` method has been deprecated; use `migrate` instead.
fix
Replace createTable calls with migrate method on Model.
affects: >=0.9.0
gotchaInfer type only works correctly when Model is created with const assertions or as a const variable.
fix
Ensure Model is declared using 'const' and not 'let' to preserve literal types for inference.
affects: all
breakingModel constructor second parameter schema now enforces object literal with DataTypes keys; string literals no longer accepted.
fix
Use DataTypes constants (e.g., DataTypes.STRING) instead of 'STRING' string.
affects: >=0.9.0
deprecatedThe `find` method is deprecated; use `First` or `All` with where clauses.
fix
Replace find with First({ where: ... }) for single result or All({ where: ... }) for multiple.
affects: >=0.9.0
Errors
Common errors & fixes
TypeError: D1Orm is not a constructor
Using CommonJS require instead of ESM import.
fix
Replace 'const D1Orm = require("d1-orm")' with 'import { D1Orm } from "d1-orm"' in an ESM context.
Property 'First' does not exist on type 'Model<...>'
Using older version (<0.9.0) where method was named 'find' instead of 'First'.
fix
Update to d1-orm@0.9 or later, or use 'find' in older versions.
Type 'string' is not assignable to type 'DataTypes'
Passing plain string like 'INTEGER' instead of DataTypes.INTEGER.
fix
Use DataTypes constants: DataTypes.INTEGER instead of 'INTEGER'.
Cannot find name 'D1Database'
Missing @cloudflare/workers-types or not included in tsconfig.
fix
Install @cloudflare/workers-types and add 'types': ['@cloudflare/workers-types'] to tsconfig.json.
Upgrade
Version history
0.9.2latest on npm
Audit
Dependencies
@cloudflare/workers-typesoptionalProvides TypeScript types for D1Database and other Cloudflare Workers APIs
Agent activity
12 hits · last 30 days
node
8
OpenAI (training)
2
Resources
packaged1-orm
d1-orm — npm install d1-orm · libregistry