Skip to content

seveibar/kysely-neon

Repository files navigation

kysely-neon

Kysely dialect for Neon serverless postgres.

Created with help from kysely-d1 and the neon serverless binding

Setup

Edge runtime (or cloudflare workers):

npm i kysely-neon kysely @neondatabase/serverless

Node.js:

npm i kysely-neon kysely @neondatabase/serverless ws

Usage

Edge runtime:

import { GeneratedAlways, Kysely } from "kysely"
import { NeonDialect } from "kysely-neon"

interface Database {
  person: PersonTable
}

interface PersonTable {
  id: GeneratedAlways<number>
  first_name: string
  gender: "male" | "female" | "other"
}

const db = new Kysely<Database>({
  dialect: new NeonDialect({
    connectionString: process.env.DATABASE_URL,
  }),
})

await db
  .insertInto("person")
  .values({ first_name: "Jennifer", gender: "female" })
  .returning("id")
  .executeTakeFirstOrThrow()

Node.js:

import { GeneratedAlways, Kysely } from "kysely"
import { NeonDialect } from "kysely-neon"
import ws from "ws"

interface Database {
  person: PersonTable
}

interface PersonTable {
  id: GeneratedAlways<number>
  first_name: string
  gender: "male" | "female" | "other"
}

const db = new Kysely<Database>({
  dialect: new NeonDialect({
    connectionString: process.env.DATABASE_URL,
    webSocketConstructor: ws,
  }),
})

await db
  .insertInto("person")
  .values({ first_name: "Jennifer", gender: "female" })
  .returning("id")
  .executeTakeFirstOrThrow()

Experimental HTTP Dialect:

@neondatabase/serverless has a new experimental neon function that allows making stateless HTTPS requests, that should have lower latencies, but no session or transaction support. To use it, import and use NeonHTTPDialect instead of NeonDialect.

import { GeneratedAlways, Kysely } from "kysely"
import { NeonHTTPDialect } from "kysely-neon"

interface Database {
  person: PersonTable
}

interface PersonTable {
  id: GeneratedAlways<number>
  first_name: string
  gender: "male" | "female" | "other"
}

const db = new Kysely<Database>({
  dialect: new NeonHTTPDialect({
    connectionString: process.env.DATABASE_URL,
  }),
})

await db
  .insertInto("person")
  .values({ first_name: "Jennifer", gender: "female" })
  .returning("id")
  .executeTakeFirstOrThrow()