{"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install prisma-kysely"],"cli":null},"imports":["import type { DB } from '../src/db/types';","import { Kysely } from 'kysely';","generator kysely { provider = \"prisma-kysely\" }"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}