Waddler <> Cloudflare D1

This guide assumes familiarity with:

According to the official website, D1 is Cloudflare’s first queryable relational database.

Waddler fully supports the Cloudflare D1 database and Cloudflare Workers environment. We embrace SQL dialects and dialect specific drivers and syntax and mirror SQLite-like all, run query methods syntax.

To setup project for your Cloudflare D1 please refer to official docs.

Step 1 - Install packages

npm
yarn
pnpm
bun
npm i waddler

Step 2 - Initialize the driver and make a query

You would need to have either a wrangler.json or a wrangler.toml file for D1 database and will look something like this:

wrangler.json
wrangler.toml
{
    "name": "YOUR_PROJECT_NAME",
    "main": "src/index.ts",
    "compatibility_date": "2024-09-26",
    "compatibility_flags": [
        "nodejs_compat"
    ],
    "d1_databases": [
        {
            "binding": "BINDING_NAME",
            "database_name": "YOUR_DB_NAME",
            "database_id": "YOUR_DB_ID",
            "migrations_dir": "waddler/migrations"
        }
    ]
}

Make your first D1 query:

import { waddler } from 'waddler/d1';

export interface Env {
  <BINDING_NAME>: D1Database;
}

export default {
  async fetch(request: Request, env: Env) {
    const sql = waddler(env.<BINDING_NAME>);
    const result = await sql`select 1;`.all();
    return Response.json(result);
  },
};