{"id":18064915,"url":"https://github.com/eoin-obrien/prisma-extension-kysely","last_synced_at":"2025-05-15T10:07:06.090Z","repository":{"id":209501033,"uuid":"723911201","full_name":"eoin-obrien/prisma-extension-kysely","owner":"eoin-obrien","description":"Drop down to raw SQL in Prisma without sacrificing type safety!","archived":false,"fork":false,"pushed_at":"2025-05-14T17:08:05.000Z","size":712,"stargazers_count":262,"open_issues_count":8,"forks_count":10,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-14T18:25:03.779Z","etag":null,"topics":["kysely","prisma","prisma-extension","query-builder","sql","type-safety","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/prisma-extension-kysely","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/eoin-obrien.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,"zenodo":null}},"created_at":"2023-11-27T02:34:02.000Z","updated_at":"2025-05-14T17:08:10.000Z","dependencies_parsed_at":"2023-11-27T18:25:38.513Z","dependency_job_id":"46db5a4f-60eb-44ee-a5c6-6a96b6f22491","html_url":"https://github.com/eoin-obrien/prisma-extension-kysely","commit_stats":{"total_commits":233,"total_committers":3,"mean_commits":77.66666666666667,"dds":0.3948497854077253,"last_synced_commit":"4949e85a4ed428433f40bd982aecf9a4af886605"},"previous_names":["eoin-obrien/prisma-extension-kysely"],"tags_count":8,"template":false,"template_full_name":"prisma/prisma-client-extension-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eoin-obrien%2Fprisma-extension-kysely","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eoin-obrien%2Fprisma-extension-kysely/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eoin-obrien%2Fprisma-extension-kysely/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eoin-obrien%2Fprisma-extension-kysely/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eoin-obrien","download_url":"https://codeload.github.com/eoin-obrien/prisma-extension-kysely/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254202715,"owners_count":22031815,"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":["kysely","prisma","prisma-extension","query-builder","sql","type-safety","typescript"],"created_at":"2024-10-31T06:08:27.479Z","updated_at":"2025-05-15T10:07:01.043Z","avatar_url":"https://github.com/eoin-obrien.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prisma Kysely Extension\n\n[![npm version](https://badge.fury.io/js/prisma-extension-kysely.svg)](https://badge.fury.io/js/prisma-extension-kysely)\n[![npm downloads](https://img.shields.io/npm/dm/prisma-extension-kysely.svg)](https://www.npmjs.com/package/prisma-extension-kysely)\n[![GitHub license](https://img.shields.io/github/license/eoin-obrien/prisma-extension-kysely.svg)](https://www.npmjs.com/package/prisma-extension-kysely)\n[![Node.js CI](https://github.com/eoin-obrien/prisma-extension-kysely/actions/workflows/ci.yml/badge.svg)](https://github.com/eoin-obrien/prisma-extension-kysely/actions/workflows/ci.yml)\n[![Node.js Package](https://github.com/eoin-obrien/prisma-extension-kysely/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/eoin-obrien/prisma-extension-kysely/actions/workflows/npm-publish.yml)\n[![codecov](https://codecov.io/gh/eoin-obrien/prisma-extension-kysely/graph/badge.svg?token=C18C7BGISJ)](https://codecov.io/gh/eoin-obrien/prisma-extension-kysely)\n[![Maintainability](https://api.codeclimate.com/v1/badges/241b8b2b35abafc8af6e/maintainability)](https://codeclimate.com/github/eoin-obrien/prisma-extension-kysely/maintainability)\n\nWriting and maintaining raw SQL queries for Prisma can be a tedious and error-prone task. The moment you need to write a query that is not supported out-of-the-box by Prisma, you lose all of that type-safety and autocompletion. This is where `prisma-extension-kysely` comes in! It allows you to easily write raw SQL queries in a type-safe manner with [`kysely`](https://kysely.dev/) and integrate them seamlessly with Prisma.\n\nAnd the best part? You can use all of your favorite [`kysely`](https://kysely.dev/) plugins with `prisma-extension-kysely` too!\n\nYou don't have to take our word for it, though:\n\n\u003e I have to say, this is BY FAR the most amazing community package I've seen in the Prisma ecosystem!\n\u003e\n\u003e It makes it so much more convenient to drop down to raw SQL when needed without sacrificing DX — best of both worlds! 🚀\n\n— [Nikolas Burk, DevRel @ Prisma](https://twitter.com/nikolasburk/status/1747901827960471699)\n\n## Features\n\n- **Type-safe** — Write raw SQL queries in a type-safe manner with `kysely`\n- **Seamless integration** — Use `kysely` queries with Prisma as if they were native\n- **Autocompletion** — Get autocompletion for your queries in your IDE\n- **Type inference** — Get type inference for your queries in your IDE\n\n## Get started\n\nClick the **Use this template** button and provide details for your Client extension\n\nInstall the dependencies:\n\n```shell\nnpm install prisma-extension-kysely kysely\n```\n\nSet up the excellent [`prisma-kysely`](https://www.npmjs.com/package/prisma-kysely) library to automatically generate types for your database:\n\n```shell\nnpm install -D prisma-kysely\n```\n\nAdd `prisma-kysely` as a generator to your `schema.prisma`:\n\n```prisma\ngenerator kysely {\n  provider = \"prisma-kysely\"\n}\n```\n\nGenerate the types:\n\n```shell\nnpx prisma generate\n```\n\nExtend your Prisma Client:\n\n```typescript\nimport kyselyExtension from \"prisma-extension-kysely\";\nimport type { DB } from \"./prisma/generated/types\";\n\nconst prisma = new PrismaClient().$extends(\n  kyselyExtension({\n    kysely: (driver) =\u003e\n      new Kysely\u003cDB\u003e({\n        dialect: {\n          // This is where the magic happens!\n          createDriver: () =\u003e driver,\n          // Don't forget to customize these to match your database!\n          createAdapter: () =\u003e new PostgresAdapter(),\n          createIntrospector: (db) =\u003e new PostgresIntrospector(db),\n          createQueryCompiler: () =\u003e new PostgresQueryCompiler(),\n        },\n        plugins: [\n          // Add your favorite plugins here!\n        ],\n      }),\n  }),\n);\n```\n\nIt's that simple! Now you can write raw SQL queries with `kysely` and use them with Prisma:\n\n```typescript\n// Replace this...\nconst result = prisma.$queryRaw`SELECT * FROM User WHERE id = ${id}`;\n\n// With this!\nconst query = prisma.$kysely\n  .selectFrom(\"User\")\n  .selectAll()\n  .where(\"id\", \"=\", id);\n\n// Thanks to kysely's magic, everything is type-safe!\nconst result = await query.execute();\n\n// You can also execute queries without fetching the results\nawait prisma.$kysely.deleteFrom(\"User\").where(\"id\", \"=\", id).execute();\n```\n\n## Transactions\n\nPrisma's interactive transactions are fully supported by `prisma-extension-kysely`! Just remeber to use `tx.$kysely` instead of `prisma.$kysely`, and you're good to go:\n\n```typescript\nawait prisma.$transaction(async (tx) =\u003e {\n  await tx.$kysely\n    .insertInto(\"User\")\n    .values({ id: 1, name: \"John Doe\" })\n    .execute();\n\n  await tx.$kysely\n    .insertInto(\"User\")\n    .values({ id: 2, name: \"Jane Doe\" })\n    .execute();\n});\n```\n\nDon't try to use Kysely's `transaction` method directly, though. It's not supported by `prisma-extension-kysely`, and it will throw an error if you try to use it.\n\n```typescript\n// Don't do this! Prefer prisma.$transaction instead.\nawait prisma.$kysely.transaction().execute(async (trx) =\u003e {});\n```\n\n## Plugins\n\nDo you love Kysely's plugins? So do we! You can use them with `prisma-extension-kysely` as well:\n\n```typescript\nconst prisma = new PrismaClient().$extends(\n  kyselyExtension({\n    kysely: (driver) =\u003e\n      new Kysely\u003cDB\u003e({\n        dialect: {\n          createDriver: () =\u003e driver,\n          createAdapter: () =\u003e new PostgresAdapter(),\n          createIntrospector: (db) =\u003e new PostgresIntrospector(db),\n          createQueryCompiler: () =\u003e new PostgresQueryCompiler(),\n        },\n        // Use your favorite plugins!\n        plugins: [new CamelCasePlugin()],\n      }),\n  }),\n);\n```\n\nIf you're using the `CamelCasePlugin`, don't forget to add the `camelCase` option to your Prisma schema too:\n\n```prisma\ngenerator kysely {\n  provider = \"prisma-kysely\"\n  camelCase = true\n}\n```\n\nTake a look at [the camel case example](examples/camel-case/) to see it in action! Check out the [Kysely documentation](https://kysely.dev/) for more information about plugins.\n\n## Read Replicas\n\nUsing read replicas with `prisma-extension-kysely` is a breeze!\nJust use the excellent [`@prisma/extension-read-replicas`](https://www.npmjs.com/package/@prisma/extension-read-replicas) extension as normal.\nPay attention to how it's configured, though:\n\n```typescript\n// Use a common config for primary and replica clients (or different configs)\nconst kyselyExtensionArgs: PrismaKyselyExtensionArgs\u003cDB\u003e = {\n  kysely: (driver) =\u003e\n    new Kysely\u003cDB\u003e({\n      dialect: {\n        createAdapter: () =\u003e new SqliteAdapter(),\n        createDriver: () =\u003e driver,\n        createIntrospector: (db) =\u003e new SqliteIntrospector(db),\n        createQueryCompiler: () =\u003e new SqliteQueryCompiler(),\n      },\n    }),\n};\n\n// Initialize the replica client(s) and add the Kysely extension\nconst replicaClient = new PrismaClient({\n  datasourceUrl: \"YOUR_REPLICA_URL\", // Replace this with your replica's URL!\n  log: [{ level: \"query\", emit: \"event\" }],\n}).$extends(kyselyExtension(kyselyExtensionArgs));\n\n// Initialize the primary client and add the Kysely extension and the read replicas extension\nconst prisma = new PrismaClient()\n  .$extends(kyselyExtension(kyselyExtensionArgs)) // Apply the Kysely extension before the read replicas extension!\n  .$extends(\n    readReplicas({\n      replicas: [replicaClient],\n    }),\n  ); // Apply the read replicas extension after the Kysely extension!\n```\n\nSee how we're setting up the replica client as a fully-fledged Prisma client and extending it separately? That's the secret sauce!\nIt make sure that the replica client has a separate Kysely instance. If you try to use bare URLs, you'll run into trouble;\nit'll share the same Kysely instance as the primary client, and you'll get unpleasant surprises!\n\n```typescript\n// Don't do this! It won't work as expected.\nreadReplicas({\n  url: \"postgresql://user:password@localhost:5432/dbname\",\n});\n```\n\nAlso, note that we're applying the Kysely extension before the read replicas extension. This is important! If you apply the read replicas extension first, you won't get `.$kysely` on the primary client.\n\nCheck out the [read replicas example](examples/read-replicas/) for a runnable example!\n\n## Examples\n\nCheck out the [examples](examples) directory for a sample project!\n\n```shell\ncd examples/basic\nnpm install\nnpx prisma db push\nnpm run dev\n```\n\n## Learn more\n\n- [Kysely](https://kysely.dev/)\n- [`prisma-kysely`](https://www.npmjs.com/package/prisma-kysely)\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feoin-obrien%2Fprisma-extension-kysely","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feoin-obrien%2Fprisma-extension-kysely","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feoin-obrien%2Fprisma-extension-kysely/lists"}