npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

kysely-bun-dialects

v1.0.0

Published

Kysely dialects for Bun's built-in SQLite, MySQL, and PostgreSQL drivers

Readme

kysely-bun-dialects

Kysely dialects for Bun's built-in database drivers. Supports SQLite (bun:sqlite), MySQL, and PostgreSQL via Bun.SQL — no external database packages required.

Installation

bun add kysely-bun-dialects kysely

Dialects

SQLite

Uses Bun's built-in bun:sqlite module.

import { BunSqliteDialect } from 'kysely-bun-dialects'
import { Database } from 'bun:sqlite'
import { Kysely, sql, type Generated } from 'kysely'

interface UserTable {
  id: Generated<number>
  email: string
  name: string
}

interface Database {
  users: UserTable
}

const db = new Kysely<Database>({
  dialect: new BunSqliteDialect({
    database: new Database('app.db'),
  }),
})

Pass :memory: for an in-memory database:

new BunSqliteDialect({
  database: new Database(':memory:'),
})

Or use a factory function if you need to defer opening the file:

new BunSqliteDialect({
  database: () => new Database('app.db'),
})

PostgreSQL

Uses Bun's built-in Bun.SQL with the Postgres adapter.

import { BunPostgresDialect } from 'kysely-bun-dialects'
import { SQL } from 'bun'
import { Kysely, type Generated, type ColumnType } from 'kysely'

interface UserTable {
  id: Generated<number>
  email: string
  name: string
  created_at: ColumnType<Date, string | undefined, never>
}

interface AppDatabase {
  users: UserTable
}

const db = new Kysely<AppDatabase>({
  dialect: new BunPostgresDialect({
    sql: new SQL(process.env.DATABASE_URL),
  }),
})

Use returningAll() on inserts since Postgres supports RETURNING:

const user = await db
  .insertInto('users')
  .values({ email: '[email protected]', name: 'Ada Lovelace' })
  .returningAll()
  .executeTakeFirstOrThrow()

MySQL

Uses Bun's built-in Bun.SQL with the MySQL adapter.

import { BunMySqlDialect } from 'kysely-bun-dialects'
import { SQL } from 'bun'
import { Kysely, type Generated, type ColumnType } from 'kysely'

interface UserTable {
  id: Generated<number>
  email: string
  name: string
  created_at: ColumnType<Date, string | undefined, never>
}

interface AppDatabase {
  users: UserTable
}

const db = new Kysely<AppDatabase>({
  dialect: new BunMySqlDialect({
    sql: new SQL('mysql://user:pass@localhost:3306/mydb'),
  }),
})

MySQL doesn't support RETURNING, so fetch the inserted row separately using insertId:

const result = await db
  .insertInto('users')
  .values({ email: '[email protected]', name: 'Ada Lovelace' })
  .executeTakeFirstOrThrow()

const user = await db
  .selectFrom('users')
  .selectAll()
  .where('id', '=', Number(result.insertId))
  .executeTakeFirstOrThrow()

Subpath imports

Each dialect is also available as a subpath import if you want to avoid loading all three:

import { BunSqliteDialect } from 'kysely-bun-dialects/sqlite'
import { BunMySqlDialect } from 'kysely-bun-dialects/mysql'
import { BunPostgresDialect } from 'kysely-bun-dialects/postgres'

Connection options

SQLite options

| Option | Type | Description | |--------|------|-------------| | database | Database \| () => Database | A bun:sqlite Database instance, or a factory function | | onCreateConnection | (conn) => void \| Promise<void> | Called once when the connection is first created |

MySQL & PostgreSQL options

Both BunMySqlDialect and BunPostgresDialect accept the same shape:

| Option | Type | Description | |--------|------|-------------| | sql | SQL \| () => SQL \| Promise<SQL> | A Bun.SQL instance, or a factory function | | onCreateConnection | (conn) => void \| Promise<void> | Called once after a connection is reserved from the pool | | onReserveConnection | (conn) => void \| Promise<void> | Called every time a connection is reserved from the pool |

Creating a Bun.SQL instance

Pass a connection URL:

new SQL('postgres://user:pass@localhost:5432/mydb')
new SQL('mysql://user:pass@localhost:3306/mydb')

Or an options object:

new SQL({
  hostname: 'localhost',
  port: 5432,
  username: 'user',
  password: 'pass',
  database: 'mydb',
  max: 10,            // connection pool size
  idleTimeout: 30,    // seconds before idle connections are closed
})

Transactions

Transactions work the same way as any other Kysely dialect:

await db.transaction().execute(async (trx) => {
  const user = await trx
    .insertInto('users')
    .values({ email: '[email protected]', name: 'Ada Lovelace' })
    .returningAll()
    .executeTakeFirstOrThrow()

  await trx
    .insertInto('posts')
    .values({ user_id: user.id, title: 'Hello world' })
    .execute()
})

Requirements

License

MIT