A Kysely dialect for Cloudflare D1.
npm install kysely @gawdn/kysely-cloudflare-d1
- This does not support transactions as Cloudflare D1 does not support transactions in its public API.
- This was built for working with Cloudflare Page Functions but should work with Cloudflare Workers generally.
Declare a D1 binding to your database. This will also work with locally defined bindings.
An example of using it in a page function.
You may need to define a worker-configuration.d.ts
in your /functions
directory to set the binding in Typescript.
// /functions/worker-configuration.d.ts
interface Env {
DB: D1Database;
}
// /functions/index.ts (or any function)
import { D1Dialect } from "@gawdn/kysely-cloudflare-d1";
import { Kysely } from "kysely";
export const onRequest: PagesFunction<Env, string, any> = async ({
params,
env,
}): Promise<Response> => {
const db = new Kysely<any>({
dialect: new D1Dialect({
database: env.DB,
}),
});
// Use Kysely the same way you usually would.
await db.insertInto("users").values({ user_name: "Jam" }).execute();
const users = await db.selectFrom("users").selectAll().execute();
return new Response(JSON.stringify(users), { status: 200 });
});
An example of using it in a worker.
// /src/index.ts
import { D1Dialect } from "@gawdn/kysely-cloudflare-d1";
import { Kysely } from "kysely";
export interface Env {
DB: D1Database;
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
const db = new Kysely<any>({
dialect: new D1Dialect({
database: env.DB,
}),
});
// Use Kysely the same way you usually would.
await db.insertInto("users").values({ user_name: "Jam" }).execute();
const users = await db.selectFrom("users").selectAll().execute();
return new Response(JSON.stringify(users), { status: 200 });
},
};
Define a binding in your wrangler.toml
in your top-level directory.
[[ d1_databases ]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "database"
database_id = ""