mirror of
https://github.com/theoludwig/kysely-typegen.git
synced 2026-10-16 19:16:47 +02:00
docs: add Bun.SQL support
This commit is contained in:
10 files changed
+448
-202
No files matched your search
@@ -50,7 +50,7 @@ Create your Kysely database instance and export a `databaseTypegen` for the scri
|
||||
|
||||
#### PostgreSQL
|
||||
|
||||
`KyselyTypegenPostgresDialect` works with any Kysely PostgreSQL dialect, including the built-in [`PostgresDialect`](https://kysely.dev/docs/dialects/postgres) (using [`pg`](https://npmx.dev/package/pg)) and [`PostgresJSDialect`](https://npmx.dev/package/kysely-postgres-js) (using [`postgres`](https://npmx.dev/package/postgres)). The example below uses `kysely-postgres-js`.
|
||||
`KyselyTypegenPostgresDialect` works with any Kysely PostgreSQL dialect, including the built-in [`PostgresDialect`](https://kysely.dev/docs/dialects/postgres) (using [`pg`](https://npmx.dev/package/pg)) and [`PostgresJSDialect`](https://npmx.dev/package/kysely-postgres-js) (using [`postgres`](https://npmx.dev/package/postgres) or [`Bun.SQL`](#bunsql)). The example below uses `kysely-postgres-js`.
|
||||
|
||||
```sh
|
||||
npm install kysely-postgres-js postgres
|
||||
@@ -79,6 +79,38 @@ export const database = new Kysely<DB>({ dialect })
|
||||
export const databaseTypegen = new KyselyTypegenPostgresDialect({ database })
|
||||
```
|
||||
|
||||
#### Bun.SQL
|
||||
|
||||
With [Bun](https://bun.com/), pass the built-in [`Bun.SQL`](https://bun.com/docs/runtime/sql) client to `PostgresJSDialect` instead of `postgres`, so no extra driver is required.
|
||||
|
||||
```sh
|
||||
bun add kysely-postgres-js
|
||||
```
|
||||
|
||||
```ts
|
||||
// database.ts
|
||||
import Bun from "bun"
|
||||
import { Kysely } from "kysely"
|
||||
import { PostgresJSDialect } from "kysely-postgres-js"
|
||||
import { KyselyTypegenPostgresDialect } from "kysely-typegen/postgres"
|
||||
|
||||
import type { DB } from "./codegen.ts"
|
||||
|
||||
const dialect = new PostgresJSDialect({
|
||||
postgres: new Bun.SQL({
|
||||
adapter: "postgres",
|
||||
database: process.env["DATABASE_NAME"] ?? "database",
|
||||
hostname: process.env["DATABASE_HOST"] ?? "localhost",
|
||||
username: process.env["DATABASE_USER"] ?? "user",
|
||||
password: process.env["DATABASE_PASSWORD"] ?? "password",
|
||||
port: Number.parseInt(process.env["DATABASE_PORT"] ?? "5432", 10),
|
||||
}),
|
||||
})
|
||||
|
||||
export const database = new Kysely<DB>({ dialect })
|
||||
export const databaseTypegen = new KyselyTypegenPostgresDialect({ database })
|
||||
```
|
||||
|
||||
#### PGlite
|
||||
|
||||
[PGlite](https://pglite.dev/) is PostgreSQL compiled to WebAssembly that runs in-process. Use Kysely's built-in [`PGliteDialect`](https://kysely-org.github.io/kysely-apidoc/classes/PGliteDialect.html) with `KyselyTypegenPostgresDialect`: PGlite is introspected exactly like PostgreSQL. Because `kysely-typegen` is a library, you pass it the `Kysely` instance you already have, so it works with in-process databases like PGlite, including in-memory ones, without a connection string or CLI-specific dialect support.
|
||||
@@ -179,6 +211,12 @@ Run with Node.js (using `--env-file` to load environment variables, no `dotenv`
|
||||
node --env-file=.env scripts/typegen.ts
|
||||
```
|
||||
|
||||
Or run with Bun (loads `.env` automatically):
|
||||
|
||||
```sh
|
||||
bun scripts/typegen.ts
|
||||
```
|
||||
|
||||
### Using the type definitions
|
||||
|
||||
Import `DB` into `new Kysely<DB>`, and you're done!
|
||||
|
||||
Reference in new issue
Block a user