D1 Query
A simple, partly typesafe library for flexible queries against a Cloudflare D1 database
Goals
- Stay as true to SQL as possible, don't invent new lingo
- Automatic generation of types from your d1 database-schema
- Typesafe as far as possible without complicating the syntax too much
- Flexible, chain statements in any order, one big query or branching from a base-query
Warning
This project is in early days. The aim is to help out with simpler, basic SQL-queries. Not all methods have detailed types. Used in prod @ Shadr.net
Install
yarn add d1-query
npm install d1-query
Generate types (depends on bun and wrangler binaries)
bun ./node_modules/d1-query/typegen.ts
Usage
Simple query
import {Database} from "d1-query-types";
const d1 = new D1Query<Database["blog"]>();
const posts = await d1
.from("posts")
.select("id,title,text,created_at")
.where("public", "=", 1)
.orderBy("created_at DESC")
.all();
Flexible branching queries
const users = d1
.from("users")
.select("id,name,created_at")
.where("deleted", "=", 0)
.orderBy("created_at DESC");
const admins = await users.where("role", "=", "admin").all();
const moderators = await users.where("role", "=", "moderator").all();
const developers = await users.where("role", "=", "developer").all();
Class-pattern
Coming from Rails / Active Record this pattern can look familiar:
class PostTable extends D1Query<Database["blog"], "posts"> {
async latestPosts(limit = 10) {
return this.select("*, users.name as user_name")
.leftJoin("users")
.on("posts.user_id", "=", "users.id")
.orderBy("created_at DESC")
.limit(limit)
.all();
}
}
export const postTable = new PostTable({table: "posts"});
export async function loader({context}: LoaderFunctionArgs) {
const d1 = new D1Query<Database["blog"]>({db: context.cloudflare.env.DB});
const latestPosts = await d1.from("posts").orderBy("created_at DESC").limit(10).all();
return json({latestPosts});
}
Fallback to SQL
const list = await d1.sql<User>("SELECT id,name FROM users", parameters);
const row = await d1.sqlFirst<User>("SELECT * FROM users WHERE id = ?", parameters);