{"id":25688687,"url":"https://github.com/robinellgren/kysely-replica-dialect","last_synced_at":"2025-04-24T01:10:00.374Z","repository":{"id":277850598,"uuid":"933596966","full_name":"robinellgren/kysely-replica-dialect","owner":"robinellgren","description":"Kysely dialect for MySQL and Postgres with support for replicas","archived":false,"fork":false,"pushed_at":"2025-02-17T10:46:18.000Z","size":27890,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T08:39:12.205Z","etag":null,"topics":["dialect","kysely","mysql","pg","postgres","postgresql","replication","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/kysely-replica-dialect","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robinellgren.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-16T11:04:52.000Z","updated_at":"2025-02-18T07:11:32.000Z","dependencies_parsed_at":"2025-02-16T18:49:42.374Z","dependency_job_id":"6d27bf67-8023-4941-98ed-2c200c952bf1","html_url":"https://github.com/robinellgren/kysely-replica-dialect","commit_stats":null,"previous_names":["robinellgren/kysely-mysql-replica","robinellgren/kysely-replica-dialect"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinellgren%2Fkysely-replica-dialect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinellgren%2Fkysely-replica-dialect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinellgren%2Fkysely-replica-dialect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinellgren%2Fkysely-replica-dialect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robinellgren","download_url":"https://codeload.github.com/robinellgren/kysely-replica-dialect/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250540941,"owners_count":21447427,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dialect","kysely","mysql","pg","postgres","postgresql","replication","typescript"],"created_at":"2025-02-24T21:05:20.429Z","updated_at":"2025-04-24T01:10:00.340Z","avatar_url":"https://github.com/robinellgren.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003ekysely-replica-dialect\u003c/h1\u003e\n  \u003ca href=\"https://codecov.io/gh/robinellgren/kysely-replica-dialect\" \u003e\n    \u003cimg src=\"https://codecov.io/gh/robinellgren/kysely-replica-dialect/branch/main/graph/badge.svg?token=655MHJWNHP\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmcharts.com/compare/kysely-replica-dialect?interval=7\"\u003e\n    \u003cimg alt=\"weekly downloads\" src=\"https://img.shields.io/npm/dw/kysely-replica-dialect\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/kysely-replica-dialect\"\u003e\n    \u003cimg alt=\"NPM Badge\" src=\"https://img.shields.io/npm/v/kysely-replica-dialect.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/robinellgren/kysely-replica-dialect/actions/workflows/release.yml\"\u003e\n    \u003cimg alt=\"Release Badge\" src=\"https://github.com/robinellgren/kysely-replica-dialect/actions/workflows/release.yml/badge.svg\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/robinellgren/kysely-replica-dialect/blob/main/LICENSE\"\u003e\n    \u003cimg alt=\"MIT License\" src=\"https://img.shields.io/github/license/robinellgren/kysely-replica-dialect\" /\u003e\n  \u003c/a\u003e\n\u003c/div\u003e\n\nA [Kysely](https://github.com/koskimas/kysely) dialect for `MySQL` and `Postgres` that supports using **read replication**. The dialect uses the respective core `kysely` dialects under the hood.\n\n## Features\n\n- ⚡️\u0026nbsp; Well-tested and production ready\n- 💯\u0026nbsp; 100% test coverage\n- 🍃\u0026nbsp; Light - The library has zero dependencies (other than `kysely` itself)\n- 🐘🐬\u0026nbsp; Works with both `MySQL` and `Postgres`\n- ✅\u0026nbsp; Easy to add to your existing project\n\n## Read replication\n\nRead 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.\n\n`kysely-replica-dialect` adds support for `MySQL` and `Postgres` read replication in Kysely, which [is not available in the main library](https://github.com/kysely-org/kysely/issues/450). 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.\n\n## Installation\n\nAvailable in [NPM](https://www.npmjs.com/package/kysely-replica-dialect).\n\nThe only required peer-dependency is `kysely`.\nYou can install the library with your favorite package manager:\n\n```bash\n# with pnpm\npnpm add kysely-replica-dialect\n\n# with yarn\nyarn add kysely-replica-dialect\n\n# with npm\nnpm install kysely-replica-dialect\n```\n\n## Usage\n\u003e 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`).\n\nSince 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.\n\n### MySQL\n\nYou can pass a new instance of `MysqlReplicaDialect` as the `dialect` option when creating a new `Kysely` instance:\n\n```typescript\nimport { Kysely } from \"kysely\";\nimport { createPool } from \"mysql2\";\nimport { MysqlReplicaDialect } from \"kysely-replica-dialect\";\n\nconst writePool = createPool({\n    database: \"some_db\",\n    host: \"localhost:3306\",\n});\n\nconst readPool = createPool({\n    database: \"some_db\",\n    host: \"localhost:3307\",\n});\n\nconst db = new Kysely\u003cDB\u003e({\n  dialect: new MysqlReplicaDialect({\n    pools: {\n      read: readPool,\n      write: writePool,\n    },\n    ...yourOtherDialectConfig,\n  }),\n});\n```\n\n### Postgres\n\nSimilarily to Mysql, you can pass a new instance of `PostgresReplicaDialect` as the `dialect` option when creating a new `Kysely` instance:\n\n```typescript\nimport { Kysely } from \"kysely\";\nimport { Pool } from \"pg\";\nimport { PostgresReplicaDialect } from \"kysely-replica-dialect\";\n\nconst writePool = new Pool({\n    database: \"some_db\",\n    host: \"localhost:3306\",\n});\n\nconst readPool = new Pool({\n    database: \"some_db\",\n    host: \"localhost:3307\",\n});\n\nconst db = new Kysely\u003cTestDB\u003e({\n  dialect: new PostgresReplicaDialect({\n    pools: {\n      read: readPool,\n      write: writePool,\n    },\n    ...yourOtherDialectConfig,\n  }),\n});\n```\n\n### Pool as function\n\nIf you want the pool to only be created once it's first used, pool can be a function (just like in `kysely`):\n\n```typescript\nimport { createPool } from \"mysql2\";\nimport { Pool } from \"pg\";\n\nnew MysqlReplicaDialect({\n    pools: {\n      read: async () =\u003e createPool({ database: \"some_db\", host: \"localhost:3307\" }),\n      write: async () =\u003e createPool({ database: \"some_db\", host: \"localhost:3306\" }),\n    },\n});\n\nnew PostgresReplicaDialect({\n    pools: {\n      read: async () =\u003e new Pool({ database: \"some_db\", host: \"localhost:3307\" }),\n      write: async () =\u003e new Pool({ database: \"some_db\", host: \"localhost:3306\" }),\n    },\n});\n```\n\n## Transactions\n\nWhen 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.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinellgren%2Fkysely-replica-dialect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobinellgren%2Fkysely-replica-dialect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinellgren%2Fkysely-replica-dialect/lists"}