Skip to content

thorwebdev/kysely-postgres-js

 
 

Repository files navigation

kysely-postgres-js

Powered by TypeScript

Kysely dialect for PostgreSQL using the Postgres.js client under the hood.

Installation

NPM 7+

npm i kysely-postgres-js

NPM <7

npm i kysely-postgres-js kysely postgres

Yarn

yarn add kysely-postgres-js kysely postgres

PNPM

pnpm add kysely-postgres-js kysely postgres

Deno

This package uses/extends some Kysely types and classes, which are imported using its NPM package name -- not a relative file path or CDN url. It also uses [Postgres.js] which is imported using its NPM package name -- not a relative file path or CDN url.

To fix that, add an import_map.json file.

{
  "imports": {
    "kysely": "https://cdn.jsdelivr.net/npm/kysely@0.23.5/dist/esm/index.js",
    "postgres": "https://deno.land/x/postgres@3.3.4"
  }
}

Usage

import {type GeneratedAlways, Kysely} from 'kysely'
import {PostgresJSDialect} from 'kysely-postgres-js'
import postgres from 'postgres'

interface Database {
  person: {
    id: GeneratedAlways<number>
    first_name: string | null
    last_name: string | null
    age: number
  }
}

const db = new Kysely<Database>({
  dialect: new PostgresJSDialect({
    connectionString: 'postgres://admin@localhost:5434/test',
    options: {
      max: 10,
    },
    postgres,
  }),
})

// or...

const db = new Kysely<Database>({
  dialect: new PostgresJSDialect({
    options: {
      database: 'test',
      host: 'localhost',
      max: 10,
      port: 5434,
      user: 'admin',
    },
    postgres,
  }),
})

Caveats

Single connection

Postgres.js doesn't provide single connection getter method/s. To get a single connection, you have to create an instance with a pool that has at most one connection (max: 1). This is not aligned with Kysely's current design. As a result, db.connection() will not work as expected when using a pool with more than one connection. If you need to use a single connection, you should instantiate a new Kysely instance with a pool that has at most one connection.

Transactions

For transactions, this dialect creates additional pools with at most one connection, so db.transaction().execute(...) will work as expected. Keep in mind, this means that total number of connections to the database might exceed the pool size passed to Kysely initially.

License

MIT License, see LICENSE

About

Kysely dialect for PostgreSQL using the Postgres.js client.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 93.1%
  • JavaScript 6.9%