{"id":13675772,"url":"https://github.com/valtyr/prisma-kysely","last_synced_at":"2026-01-16T02:17:01.435Z","repository":{"id":109011089,"uuid":"609623532","full_name":"valtyr/prisma-kysely","owner":"valtyr","description":"🪄 Generate Kysely types directly from your Prisma schema!","archived":false,"fork":false,"pushed_at":"2024-06-03T11:32:00.000Z","size":2426,"stargazers_count":1025,"open_issues_count":24,"forks_count":37,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-20T23:55:57.721Z","etag":null,"topics":["d1","generator","kysely","prisma"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/prisma-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/valtyr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"ko_fi":"valtyr"}},"created_at":"2023-03-04T18:32:22.000Z","updated_at":"2025-04-20T19:56:21.000Z","dependencies_parsed_at":"2023-03-09T20:30:28.131Z","dependency_job_id":"91b1765c-0aca-44fa-9686-02278b191d8e","html_url":"https://github.com/valtyr/prisma-kysely","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"0cd75f023c325d49638a7ced92306c0fe00a278f"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valtyr%2Fprisma-kysely","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valtyr%2Fprisma-kysely/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valtyr%2Fprisma-kysely/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valtyr%2Fprisma-kysely/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valtyr","download_url":"https://codeload.github.com/valtyr/prisma-kysely/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251404452,"owners_count":21584094,"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":["d1","generator","kysely","prisma"],"created_at":"2024-08-02T12:01:01.630Z","updated_at":"2026-01-12T05:11:40.217Z","avatar_url":"https://github.com/valtyr.png","language":"TypeScript","funding_links":["https://ko-fi.com/valtyr","https://ko-fi.com/N4N7QQZVA"],"categories":["Type Generators","TypeScript"],"sub_categories":[],"readme":"![Prisma Kysely](assets/logo-hero.png)\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://www.npmjs.com/package/prisma-kysely\"\u003e\u003cimg src=\"https://badge.fury.io/js/prisma-kysely.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://ko-fi.com/N4N7QQZVA\"\u003e\u003cimg height=20 src=\"https://ko-fi.com/img/githubbutton_sm.svg\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cbr/\u003e\n\n![Hero image \"Generate Kysely types directly from your Prisma\nschema\"](assets/hero.png)\n\n\u003cbr/\u003e\n\nDo you like Prisma's migration flow, schema language and DX but not the\nlimitations of the Prisma Client? Do you want to harness the raw power of SQL\nwithout losing the safety of the TypeScript type system?\n\n**Enter `prisma-kysely`**!\n\n### Setup\n\n1. Install `prisma-kysely` using your package manager of choice:\n\n   ```sh\n   bun add prisma-kysely\n   ```\n\n2. Replace (or augment) the default client generator in your `schema.prisma`\n   file with the following:\n\n   ```prisma\n   generator kysely {\n       provider = \"prisma-kysely\"\n\n       // Optionally provide a destination directory for the generated file\n       // and a filename of your choice\n       output = \"../src/db\"\n       fileName = \"types.ts\"\n       // Optionally generate runtime enums to a separate file\n       enumFileName = \"enums.ts\"\n   }\n   ```\n\n3. Run `prisma migrate dev` or `prisma generate` and use your freshly generated\n   types when instantiating Kysely!\n\n### Motivation\n\nPrisma's migration and schema definition workflow is undeniably great, and the\ntypesafety of the Prisma client is top notch, but there comes a time in every\nPrisma user's life where the client becomes just a bit too limiting. Sometimes\nwe just need to write our own multi table joins and squeeze that extra drop of\nperformance out of our apps. The Prisma client offers two options: using their\nsimplified query API or going all-in with raw SQL strings, sacrificing type\nsafety.\n\nThis is where Kysely shines. Kysely provides a toolbox to write expressive,\ntype-safe SQL queries with full autocompletion. The problem with Kysely though\nis that it's not super opinionated when it comes to schema definition and\nmigration. What many users resort to is using something like Prisma to define\nthe structure of their databases, and `kysely-codegen` to introspect their\ndatabases post-migration.\n\nThis package, `prisma-kysely`, is meant as a more integrated and convenient way\nto keep Kysely types in sync with Prisma schemas. After making the prerequisite\nchanges to your schema file, it's just as convenient and foolproof as using\nPrisma's own client.\n\nI've been using this combo for a few months now in tandem with Cloudflare's D1\nfor my private projects and Postgres at work. It's been a game-changer, and I\nhope it's just as useful for you! 😎\n\n### Config\n\n| Key                      | Description                                                                                                                                                                                                                                                                                                                                                                         | Default    |\n| :----------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- |\n| `output`                 | The directory where generated code will be saved                                                                                                                                                                                                                                                                                                                                    |            |\n| `fileName`               | The filename for the generated file                                                                                                                                                                                                                                                                                                                                                 | `types.ts` |\n| `importExtension`        | The extension to append to imports. E.g: `\".js\"` or `\".ts\"`. Use `\"\"` to append nothing.                                                                                                                                                                                                                                                                                            | `\"\"`       |\n| `enumFileName`           | The filename for the generated enums. Omitting this will generate enums and files in the same file.                                                                                                                                                                                                                                                                                 |            |\n| `camelCase`              | Enable support for Kysely's camelCase plugin                                                                                                                                                                                                                                                                                                                                        | `false`    |\n| `exportWrappedTypes`     | Kysely wrapped types such as `Selectable\u003cModel\u003e` are also exported as described in the [Kysely documentation](https://kysely.dev/docs/getting-started#types). The exported types follow the naming conventions of the document.                                                                                                                                                     | `false`    |\n| `readOnlyIds`            | Use Kysely's `GeneratedAlways` for `@id` fields with default values, preventing insert and update.                                                                                                                                                                                                                                                                                  | `false`    |\n| `[typename]TypeOverride` | Allows you to override the resulting TypeScript type for any Prisma type. Useful when targeting a different environment than Node (e.g. WinterCG compatible runtimes that use UInt8Arrays instead of Buffers for binary types etc.) Check out the [config validator](https://github.com/valtyr/prisma-kysely/blob/main/src/utils/validateConfig.ts) for a complete list of options. |            |\n| `dbTypeName`             | Allows you to override the exported type with all tables                                                                                                                                                                                                                                                                                                                            | `DB`       |\n| `groupBySchema`          | When using multiple schemas, group all models and enums for a schema into their own namespace. (Ex: `model Dog { @@schema(\"animals\") }` will be available under `Animals.Dog`)                                                                                                                                                                                                      | `false`    |\n| `filterBySchema`         | When using multiple schemas, only include models and enums for the specified schema.                                                                                                                                                                                                                                                                                                | `false`    |\n| `defaultSchema`          | When using multiple schemas, declare `which schema should not be wrapped by a namespace.                                                                                                                                                                                                                                                                                            | `'public'` |\n\n### Per-field type overrides\n\nIn some cases, you might want to override a type for a specific field. This\ncould be useful, for example, for constraining string types to certain literal\nvalues. Be aware though that this does not of course come with any runtime\nvalidation, and in most cases won't be guaranteed to match the actual data in\nthe database.\n\nThat disclaimer aside, here's how it works: Add a `@kyselyType(...)` declaration\nto the Prisma docstring (deliniated using three slashes `///`) for the field\nwith your type inside the parentheses.\n\n```prisma\nmodel User {\n  id          String @id\n  name        String\n\n  /// @kyselyType('member' | 'admin')\n  role        String\n}\n```\n\nThe parentheses can include any valid TS type declaration.\n\nThe output for the example above would be as follows:\n\n```ts\nexport type User = {\n  id: string;\n  name: string;\n  role: \"member\" | \"owner\";\n};\n```\n\n### Gotchas\n\n#### Default values\n\nBy default (no pun intended) the Prisma Query Engine uses JS based\nimplementations for certain default values, namely: `uuid()` and `cuid()`. This\nmeans that they don't end up getting defined as default values on the database\nlevel, and end up being pretty useless for us.\n\nPrisma does provide a nice solution to this though, in the form of\n`dbgenerated()`. This allows us to use any valid default value expression that\nour database supports:\n\n```prisma\nmodel PostgresUser {\n   id    String @id @default(dbgenerated(\"gen_random_uuid()\"))\n}\n\nmodel SQLiteUser {\n   id    String @id @default(dbgenerated(\"(uuid())\"))\n}\n```\n\n[Check out the Prisma Docs for more\ninfo.](https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#attribute-functions)\n\n### Contributions\n\nOMG you actually want to contribute? I'm so thankful! 🙇‍♂️\n\nHere's everything you need to do (let me know if something's missing...)\n\n1. Fork and pull the repository\n2. Run `bun install` and `bun dev` to start `tsc` in watch mode.\n3. Make changes to the source code\n4. Test your changes by creating `prisma/schema.prisma`, running `bun prisma generate` and checking the output in `prisma/types.ts`. The provider must be set\n   as follows to reference the dev build:\n   ```prisma\n   generator kysely {\n       provider = \"node ./dist/bin.js\"\n   }\n   ```\n5. Create a pull request! If your changes make sense, I'll try my best to review\n   and merge them quickly.\n\nI'm not 100% sure the [type\nmaps](https://github.com/valtyr/prisma-kysely/blob/main/src/helpers/generateFieldType.ts)\nare correct for every dialect, so any and all contributions on that front would\nbe greatly appreciated. The same goes for any bug you come across or improvement\nyou can think of.\n\n### Shoutouts\n\n- I wouldn't have made this library if I hadn't used Robin Blomberg's amazing\n  [Kysely Codegen](https://github.com/RobinBlomberg/kysely-codegen). For anyone\n  that isn't using Prisma for migrations I wholeheartedly recommend his package.\n- The implicit many-to-many table generation code is partly inspired by and\n  partly stolen from\n  [`prisma-dbml-generator`](https://github.com/notiz-dev/prisma-dbml-generator/blob/752f89cf40257a9698913294b38843ac742f8345/src/generator/many-to-many-tables.ts).\n  Many-too-many thanks to them!\n- Igal Klebanov ([@igalklebanov](https://github.com/igalklebanov)) and Jökull Sólberg ([@jokull](https://github.com/jokull)) for being this library's\n  main proponents on Twitter!\n- The authors and maintainers of Kysely ❤️‍🔥\n\n```diff\n+ Boyce-Codd gang unite! 💽\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvaltyr%2Fprisma-kysely","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvaltyr%2Fprisma-kysely","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvaltyr%2Fprisma-kysely/lists"}