/d1-orm

A simple, strictly typed ORM, to assist you in using Cloudflare's D1 product

Primary LanguageTypeScriptApache License 2.0Apache-2.0

D1-Orm

✨ A simple, strictly typed ORM, to assist you in using Cloudflare's D1 product

API reference can be found at https://d1-orm.pages.dev/modules

Docs can be found at https://d1-orm.pages.dev/guides

Installation

This package can be found on NPM

$ npm install d1-orm

Usage

This package is recommended to be used with @cloudflare/workers-types 3.16.0+.

import { D1Orm, DataTypes, Model } from "d1-orm";

export interface Env {
	// from @cloudflare/workers-types
	DB: D1Database;
}

type User = {
	id: number;
	name: string;
	email: string | undefined;
};

export default {
	async fetch(request: Request, env: Env) {
		const orm = new D1Orm(env.DB);
		const users = new Model<User>(
			{
				D1Orm: orm,
				tableName: "users",
			},
			{
				id: {
					type: DataTypes.INTEGER,
					primaryKey: true,
					autoIncrement: true,
					notNull: true,
				},
				name: {
					type: DataTypes.STRING,
					notNull: true,
					defaultValue: "John Doe",
				},
				email: {
					type: DataTypes.STRING,
					unique: true,
				},
			}
		);
	},
};

For more information, refer to the docs.