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-replica-dialect

v0.1.2

Published

Kysely dialect for MySQL and Postgres with support for replicas

Downloads

14

Readme

A Kysely dialect for MySQL and Postgres that supports using read replication. The dialect uses the respective core kysely dialects under the hood.

Features

  • ⚡️  Well-tested and production ready
  • 💯  100% test coverage
  • 🍃  Light - The library has zero dependencies (other than kysely itself)
  • 🐘🐬  Works with both MySQL and Postgres
  • ✅  Easy to add to your existing project

Read replication

Read replication allows distributing SELECT queries across multiple read replicas while directing all writes and updates to a primary database instance. This can improve read performance and scalability.

kysely-replica-dialect adds support for MySQL and Postgres read replication in Kysely, which is not available in the main library. You define a primary database for writes and one or more read replicas for queries. Note that kysely-replica-dialect does not handle the actual replication setup. That is managed by the database itself.

Installation

Available in NPM.

The only required peer-dependency is kysely. You can install the library with your favorite package manager:

# with pnpm
pnpm add kysely-replica-dialect

# with yarn
yarn add kysely-replica-dialect

# with npm
npm install kysely-replica-dialect

Usage

Each write or transaction query will use the write pool. For SELECT, the read pool will be used. Read and write replicas within the pool are switched using the underlying driver (mysql2 or pg).

Since this library uses kysely core drivers under the hood, the extra dialect config is passed to there. This means that functionality for use onCreateConnection and onReserveConnection stays the same.

MySQL

You can pass a new instance of MysqlReplicaDialect as the dialect option when creating a new Kysely instance:

import { Kysely } from "kysely";
import { createPool } from "mysql2";
import { MysqlReplicaDialect } from "kysely-replica-dialect";

const writePool = createPool({
    database: "some_db",
    host: "localhost:3306",
});

const readPool = createPool({
    database: "some_db",
    host: "localhost:3307",
});

const db = new Kysely<DB>({
  dialect: new MysqlReplicaDialect({
    pools: {
      read: readPool,
      write: writePool,
    },
    ...yourOtherDialectConfig,
  }),
});

Postgres

Similarily to Mysql, you can pass a new instance of PostgresReplicaDialect as the dialect option when creating a new Kysely instance:

import { Kysely } from "kysely";
import { Pool } from "pg";
import { PostgresReplicaDialect } from "kysely-replica-dialect";

const writePool = new Pool({
    database: "some_db",
    host: "localhost:3306",
});

const readPool = new Pool({
    database: "some_db",
    host: "localhost:3307",
});

const db = new Kysely<TestDB>({
  dialect: new PostgresReplicaDialect({
    pools: {
      read: readPool,
      write: writePool,
    },
    ...yourOtherDialectConfig,
  }),
});

Pool as function

If you want the pool to only be created once it's first used, pool can be a function (just like in kysely):

import { createPool } from "mysql2";
import { Pool } from "pg";

new MysqlReplicaDialect({
    pools: {
      read: async () => createPool({ database: "some_db", host: "localhost:3307" }),
      write: async () => createPool({ database: "some_db", host: "localhost:3306" }),
    },
});

new PostgresReplicaDialect({
    pools: {
      read: async () => new Pool({ database: "some_db", host: "localhost:3307" }),
      write: async () => new Pool({ database: "some_db", host: "localhost:3306" }),
    },
});

Transactions

When using the dialect within a transaction, the write pool will always be used. This is because there is no way to know beforehand what queries will be executed within the transaction so we cannot decide if read or write pool should be used.