Registry / database / prisma-kysely

prisma-kysely

JSON →
library3.1.0jsnpmunverified

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.

npm install prisma-kysely
INSTALL
IMPORT
SIG · PRISMA-KYSELY
P
prisma-kysely
databasejavascriptv3.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DB
import type { DB } from '../src/db/types';
import { DB } from '../src/db/types'; // 'type' keyword is important for tree-shaking
This is an import for the *generated* Kysely database types. The path depends on your `output` and `fileName` configuration in `schema.prisma`.
Kysely
import { Kysely } from 'kysely';
While `prisma-kysely` generates the types, Kysely itself is a separate library that you will install and use in conjunction with the generated types.
generator kysely
generator kysely { provider = "prisma-kysely" }
generator client { provider = "prisma-client-js" } // Not using prisma-kysely
This is how you configure the `prisma-kysely` generator within your `prisma/schema.prisma` file, enabling it to generate the Kysely types.

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.

import { Kysely, PostgresDialect } from 'kysely'; import { Pool } from 'pg'; import { config } from 'dotenv'; import type { DB } from './db/types'; // Adjust path based on your generator config // Load environment variables from .env file config(); // prisma/schema.prisma /* datasource db { provider = "postgresql" url = env("DATABASE_URL") } generator kysely { provider = "prisma-kysely" output = "./src/db" fileName = "types.ts" enumFileName = "enums.ts" banner = """ import type { Decimal } from 'decimal.js'; """ } model User { id String @id @default(uuid()) email String @unique name String? posts Post[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt balance Decimal? @db.Decimal(10, 2) } model Post { id String @id @default(uuid()) title String content String? published Boolean @default(false) author User @relation(fields: [authorId], references: [id]) authorId String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } */ // Configure the Kysely dialect (example for PostgreSQL) const dialect = new PostgresDialect({ pool: new Pool({ connectionString: process.env.DATABASE_URL ?? 'postgresql://user:password@localhost:5432/mydb', }), }); // Instantiate Kysely with your generated database types export const db = new Kysely<DB>({ dialect, }); // Example usage: Fetch a user and their post count async function getUserWithPostCount(userId: string) { try { const userWithPosts = await db .selectFrom('User') .selectAll('User') .leftJoin('Post', 'Post.authorId', 'User.id') .where('User.id', '=', userId) .select((eb) => eb.fn.count('Post.id').as('postCount')) .executeTakeFirst(); if (userWithPosts) { console.log(`User: ${userWithPosts.name}, Email: ${userWithPosts.email}, Posts: ${userWithPosts.postCount}`); return userWithPosts; } else { console.log(`User with ID ${userId} not found.`); return null; } } catch (error) { console.error('Error fetching user with post count:', error); throw error; } } // To run this example, you'd need a DATABASE_URL env var and 'pg', 'dotenv' packages. // Example call (replace with an actual user ID from your database): // getUserWithPostCount('some-actual-user-id').catch(console.error);
Debug
Known issues
breakingUpgrading 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.
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`.
affects: >=3.0.0
breakingUpgrading to `prisma-kysely` v2.0.0 moved its supported Node.js version from 16 to 24 and required Prisma 6.10.1+.
fix
Ensure your Node.js environment is compatible with Node.js 24 and update your `prisma` dependency to `6.10.1` or later.
affects: >=2.0.0 <3.0.0
gotchaIf 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.
fix
Add a `banner` configuration to your `kysely` generator in `schema.prisma`. For example: `banner = """import type { Decimal } from 'decimal.js';"""`.
affects: >=1.0.0
gotcha`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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Generator "kysely" could not be found.
The `prisma-kysely` package is not installed, or the `provider` path in `schema.prisma` is incorrect.
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.
TypeError: Type 'Decimal | null' is not assignable to type 'Decimal'.
A custom scalar type, such as `Decimal` or `BigInt`, used in the Prisma schema is not correctly imported into the generated Kysely types file.
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';"`.
Error: A database URL must be provided in your Prisma configuration.
When upgrading to Prisma 7.0.0+, the `database url` must be moved from the `datasource` block to a `prisma.config.ts` file.
fix
Refer to the official Prisma 7.0.0 upgrade guide for instructions on configuring your database URL in `prisma.config.ts`.
Error: Prisma Client is not compatible with your Prisma Schema.
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.
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.
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies
prismarequiredPeer dependency for the Prisma CLI and ecosystem tooling, required for schema definition and code generation.
Agent activity
6 hits · last 30 days
node
6
Resources
prisma-kysely — npm install prisma-kysely · libregistry