npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gawdn/kysely-cloudflare-d1

v1.0.3

Published

Cloudflare D1 bindings for Kysely

Downloads

71

Readme

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