Skip to content

gawdn/kysely-cloudflare-d1

Repository files navigation

kysely-cloudflare-d1

A Kysely dialect for Cloudflare D1.

Installation

npm install kysely @gawdn/kysely-cloudflare-d1

Limitations

  • 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.

Usage

Declare a D1 binding to your database. This will also work with locally defined bindings.

Page function

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 });
});

Worker

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 });
  },
};

Local usage

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 = ""

About

A Kysely dialect for Cloudflare D1

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published