• Stars
    star
    153
  • Rank 243,368 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

D1 dialect for Kysely

kysely-d1

ci npm

Kysely adapter for Cloudflare D1.

npm i 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 '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.

More Repositories

1

cloudflare-pages-badges

Render custom badges for Cloudflare Pages projects
TypeScript
13
star
2

customapi

AidenWallis' customapi resource
JavaScript
11
star
3

twitch-chat-widget

Personal twitch chat widget
TypeScript
10
star
4

nowplaying

HTML
10
star
5

nft-twitter-block

No more hexagons on Twitter
JavaScript
9
star
6

go-utils

A set of common Go utils I use throughout my projects.
Go
9
star
7

go-twitch-client

A low level, simple wrapper around the Twitch API written in Go.
Go
9
star
8

personal-twitch-alerts

The CSS behind my custom Twitch sub alerts - for StreamElements, inspired by MSN notifications
HTML
7
star
9

fivem-projects

A collection of FiveM projects I've built.
Go
6
star
10

twitch-chat-widget-2

My personal Twitch chat widget written in Web Components.
TypeScript
6
star
11

hachubby-birthday

A little birthday card for Twitch.tv/HAchubby
JavaScript
5
star
12

modclient2

Another attempt of writing a Twitch chat mod client in pure JS.
TypeScript
4
star
13

go-ratelimiting

Ratelimiting libraries for Go
Go
4
star
14

thonkbot

Thonkbot is a fun bot for running queries on chat messages.
Go
4
star
15

spotifyv2

Spotify now playing widget.
TypeScript
4
star
16

go-write

Simple, clean Golang HTTP response writer.
Go
4
star
17

combonator

TypeScript
4
star
18

fivem_metagame_machine

Experimenting with NUI by making the metagame machine
HTML
3
star
19

modclient

Simple and half working temporary mod client for Twitch, till chatterino 2 was fixed.
JavaScript
3
star
20

soundcloud-nowplaying

SoundCloud now playing widget
TypeScript
2
star
21

random-shit

I throw random shit I make in here.
HTML
2
star
22

aiden_fivem_auth

A lightweight and simple sessions service for FiveM.
Go
2
star
23

customapi2

A new, better solution to customapis.
Go
2
star
24

komodohype.best

HTML
2
star
25

shitcamp-commands

Chatbot customapis for Shitcamp.live
TypeScript
1
star
26

mote

HTML
1
star
27

combomachine

JavaScript
1
star
28

twitch-mod-resources

A general guide of resources compiled by the Twitch moderation community to help streamers get started with moderation in their chats.
1
star
29

pumpers

Go
1
star