{"id":16502,"library":"prisma-kysely","title":"Prisma Kysely Generator","description":"prisma-kysely is a Prisma generator that creates type definitions for Kysely from an existing Prisma schema. It currently stands at version 3.1.0 (published February 2026) and is actively maintained, with updates released to align with new Prisma major versions and introduce minor features. This package addresses the common developer desire to leverage Prisma's excellent schema definition and migration capabilities while utilizing Kysely for type-safe, expressive SQL query building, circumventing the limitations of the Prisma Client. It differentiates itself from alternatives like `kysely-codegen` by generating types directly from the Prisma schema file, eliminating the need for database introspection after migrations. This provides a more integrated and convenient workflow for keeping Kysely types synchronized with your database schema, ensuring full autocompletion and type safety for SQL queries. It's particularly useful for those seeking lean, serverless-ready setups without the Prisma Client's runtime footprint.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/valtyr/prisma-kysely","tags":["javascript","prisma","kysely","generator","typescript","codegen","sql"],"install":[{"cmd":"npm install prisma-kysely","lang":"bash","label":"npm"},{"cmd":"yarn add prisma-kysely","lang":"bash","label":"yarn"},{"cmd":"pnpm add prisma-kysely","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for the Prisma CLI and ecosystem tooling, required for schema definition and code generation.","package":"prisma","optional":false}],"imports":[{"note":"This is an import for the *generated* Kysely database types. The path depends on your `output` and `fileName` configuration in `schema.prisma`.","wrong":"import { DB } from '../src/db/types'; // 'type' keyword is important for tree-shaking","symbol":"DB","correct":"import type { DB } from '../src/db/types';"},{"note":"While `prisma-kysely` generates the types, Kysely itself is a separate library that you will install and use in conjunction with the generated types.","symbol":"Kysely","correct":"import { Kysely } from 'kysely';"},{"note":"This is how you configure the `prisma-kysely` generator within your `prisma/schema.prisma` file, enabling it to generate the Kysely types.","wrong":"generator client { provider = \"prisma-client-js\" } // Not using prisma-kysely","symbol":"generator kysely","correct":"generator kysely { provider = \"prisma-kysely\" }"}],"quickstart":{"code":"import { Kysely, PostgresDialect } from 'kysely';\nimport { Pool } from 'pg';\nimport { config } from 'dotenv';\nimport type { DB } from './db/types'; // Adjust path based on your generator config\n\n// Load environment variables from .env file\nconfig();\n\n// prisma/schema.prisma\n/*\ndatasource db {\n    provider = \"postgresql\"\n    url      = env(\"DATABASE_URL\")\n}\n\ngenerator kysely {\n    provider = \"prisma-kysely\"\n    output = \"./src/db\"\n    fileName = \"types.ts\"\n    enumFileName = \"enums.ts\"\n    banner = \"\"\"\nimport type { Decimal } from 'decimal.js';\n\"\"\"\n}\n\nmodel User {\n    id        String   @id @default(uuid())\n    email     String   @unique\n    name      String?\n    posts     Post[]\n    createdAt DateTime @default(now())\n    updatedAt DateTime @updatedAt\n    balance   Decimal? @db.Decimal(10, 2)\n}\n\nmodel Post {\n    id        String   @id @default(uuid())\n    title     String\n    content   String?\n    published Boolean  @default(false)\n    author    User     @relation(fields: [authorId], references: [id])\n    authorId  String\n    createdAt DateTime @default(now())\n    updatedAt DateTime @updatedAt\n}\n*/\n\n// Configure the Kysely dialect (example for PostgreSQL)\nconst dialect = new PostgresDialect({\n    pool: new Pool({\n        connectionString: process.env.DATABASE_URL ?? 'postgresql://user:password@localhost:5432/mydb',\n    }),\n});\n\n// Instantiate Kysely with your generated database types\nexport const db = new Kysely<DB>({\n    dialect,\n});\n\n// Example usage: Fetch a user and their post count\nasync function getUserWithPostCount(userId: string) {\n    try {\n        const userWithPosts = await db\n            .selectFrom('User')\n            .selectAll('User')\n            .leftJoin('Post', 'Post.authorId', 'User.id')\n            .where('User.id', '=', userId)\n            .select((eb) => eb.fn.count('Post.id').as('postCount'))\n            .executeTakeFirst();\n\n        if (userWithPosts) {\n            console.log(`User: ${userWithPosts.name}, Email: ${userWithPosts.email}, Posts: ${userWithPosts.postCount}`);\n            return userWithPosts;\n        } else {\n            console.log(`User with ID ${userId} not found.`);\n            return null;\n        }\n    } catch (error) {\n        console.error('Error fetching user with post count:', error);\n        throw error;\n    }\n}\n\n// To run this example, you'd need a DATABASE_URL env var and 'pg', 'dotenv' packages.\n// Example call (replace with an actual user ID from your database):\n// getUserWithPostCount('some-actual-user-id').catch(console.error);\n","lang":"typescript","description":"This quickstart demonstrates how to configure `prisma-kysely` in your `schema.prisma` file, then initialize Kysely using the generated types for type-safe database queries. It includes an example of a type-safe SQL query for fetching a user and their post count."},"warnings":[{"fix":"Update your `prisma` dependency to `7.0.0` or later. Ensure your Node.js version is `22.x` or newer. Review the Prisma 7.0.0 upgrade guide for details on moving your database URL configuration to `prisma.config.ts`.","message":"Upgrading to `prisma-kysely` v3.0.0 requires Prisma 7.0.0+ and Node.js >=22.x. Additionally, Prisma 7.0.0 introduced significant changes, including requiring `database url` to be moved from the `datasource` block to a `prisma.config.ts` file.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your Node.js environment is compatible with Node.js 24 and update your `prisma` dependency to `6.10.1` or later.","message":"Upgrading to `prisma-kysely` v2.0.0 moved its supported Node.js version from 16 to 24 and required Prisma 6.10.1+.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Add a `banner` configuration to your `kysely` generator in `schema.prisma`. For example: `banner = \"\"\"import type { Decimal } from 'decimal.js';\"\"\"`.","message":"If your Prisma schema utilizes custom scalar types (e.g., `Decimal`, `BigInt`) that are not native TypeScript types, you must use the `banner` configuration option to import their definitions into the generated Kysely type file. Failure to do so will result in TypeScript compilation errors due to unknown types.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always check the `prisma-kysely` release notes and `package.json` for the exact `prisma` peer dependency range and ensure your installed `prisma` packages fall within that range. Upgrade or downgrade `prisma` as needed.","message":"`prisma-kysely` requires a peer dependency on `prisma`. Mismatched versions between `prisma-kysely` and your installed `prisma` packages (`@prisma/client`, `@prisma/generator-helper`, `@prisma/internals`) can lead to generation failures or unexpected behavior.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Install `prisma-kysely` using your package manager (`npm install prisma-kysely` or `bun add prisma-kysely`) and verify `generator kysely { provider = \"prisma-kysely\" }` in your `schema.prisma` file.","cause":"The `prisma-kysely` package is not installed, or the `provider` path in `schema.prisma` is incorrect.","error":"Error: Generator \"kysely\" could not be found."},{"fix":"In your `schema.prisma`, add a `banner` option to the `kysely` generator to include the necessary type import. Example: `banner = \"import type { Decimal } from 'decimal.js';\"`.","cause":"A custom scalar type, such as `Decimal` or `BigInt`, used in the Prisma schema is not correctly imported into the generated Kysely types file.","error":"TypeError: Type 'Decimal | null' is not assignable to type 'Decimal'."},{"fix":"Refer to the official Prisma 7.0.0 upgrade guide for instructions on configuring your database URL in `prisma.config.ts`.","cause":"When upgrading to Prisma 7.0.0+, the `database url` must be moved from the `datasource` block to a `prisma.config.ts` file.","error":"Error: A database URL must be provided in your Prisma configuration."},{"fix":"Ensure that your `prisma-kysely` package and all `@prisma/*` packages (e.g., `@prisma/client`, `@prisma/generator-helper`) are updated to compatible versions, typically aligning with the `prisma-kysely` major version's supported Prisma version.","cause":"The installed `prisma-kysely` version or the Prisma Client version (`@prisma/client`) is not compatible with your Prisma schema's version requirements, often indicated by `prisma generate` failing.","error":"Error: Prisma Client is not compatible with your Prisma Schema."}],"ecosystem":"npm"}