8000 GitHub - NoxHarmonium/kysely-d1: D1 dialect for Kysely
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

NoxHarmonium/kysely-d1

 
 

Repository files navigation

@noxharmonium/kysely-d1

Temporary fork of https://github.com/aidenwallis/kysely-d1 that is published on NPM under "@noxharmonium/kysely-d1" until the changes in aidenwallis#25 are published.

I really needed the ESM changes to get vitest to work so I published this to make my life easier. I'll delete it as soon as that PR is merged so please don't depend on this. I probably won't pull in any other updates.

npm i @noxharmonium/kysely-d1

This project was largely adapted from kysely-planetscale.

Usage

Pass your D1 binding into the dialect in order to configure the Kysely client. Follow these docs for instructions on how to do so.

import { Kysely } from 'kysely';
import { D1Dialect } from '@noxharmonium/kysely-d1';

export interface Env {
  DB: D1Database;
}

interface KvTable {
  key: string;
  value: string;
}

interface Database {
  kv: KvTable;
}

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const { searchParams } = new URL(request.url);
    const key = searchParams.get('key');
    if (!key) {
      return new Response('No key defined.', { status: 400 });
    }

    // Create Kysely instance with kysely-d1
    const db = new Kysely<Database>({ dialect: new D1Dialect({ database: env.DB }) });

    // Read row from D1 table
    const result = await db.selectFrom('kv').selectAll().where('key', '=', key).executeTakeFirst();
    if (!result) {
      return new Response('No value found', { status: 404 });
    }

    return new Response(result.value);
  },
};

There is a working example also included, which implements a K/V style store using D1.

About

D1 dialect for Kysely

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%
0