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
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
defineConfig
✓ import { defineConfig } from 'kysely-codegen/config';
Used in configuration files (e.g., `.kysely-codegenrc.ts`) to provide type-safe configuration options for the CLI tool. Available since v0.20.0.
generateCli
✓ import { generateCli } from 'kysely-codegen/cli';
For programmatic execution of the `kysely-codegen` CLI logic within Node.js scripts. This allows embedding the type generation process in custom build pipelines.
DB
✓ import { DB } from './src/db.d.ts';
✗ import { DB } from 'kysely-codegen';
The `DB` type is *generated* by `kysely-codegen` into a `.d.ts` file (e.g., `src/db.d.ts` if `--out-file ./src/db.d.ts` is used, or `types.d.ts` by default). The `kysely-codegen` package itself does not directly export `DB`. Imports should point to the specific path where your types were generated. The example `import { DB } from 'kysely-codegen'` in the README is often misinterpreted.
This quickstart demonstrates installing `kysely-codegen`, configuring the database URL, generating TypeScript types, and then using the `DB` interface with Kysely to perform type-safe queries and insertions.
// 1. Install dependencies (e.g., for PostgreSQL)
// npm install --save-dev kysely-codegen
// npm install kysely pg
// 2. Create a .env file with your database connection string
// DATABASE_URL=postgres://user:password@host:port/database_name
// 3. Generate types from your database schema (run in your terminal)
// npx kysely-codegen --out-file ./src/db.d.ts
// 4. Use the generated types in your application code
import { Kysely, PostgresDialect, Insertable } from 'kysely';
import { Pool } from 'pg';
// Assuming the generated file is at './src/db.d.ts'. Adjust path as needed.
import { DB, User, Company } from './src/db.d.ts';
// Initialize Kysely with the generated DB type
const db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({
connectionString: process.env.DATABASE_URL ?? 'postgres://user:pass@localhost:5432/mydb',
}),
}),
});
// Example function to fetch all users
async function getAllUsers() {
console.log('Fetching users...');
const users = await db.selectFrom('user').selectAll().execute();
console.log('Found users:', users);
return users;
}
// Example function to insert a new user using Insertable type
async function createNewUser(userData: Insertable<User>) {
console.log('Creating a new user...');
const newUser = await db
.insertInto('user')
.values(userData)
.returningAll()
.executeTakeFirstOrThrow();
console.log('Created user:', newUser);
return newUser;
}
async function runExample() {
try {
const users = await getAllUsers();
const newUserPayload: Insertable<User> = {
email: `user_${Date.now()}@example.com`,
name: 'Generated User',
is_active: true,
company_id: null, // Assuming nullable or optional in your schema
};
await createNewUser(newUserPayload);
// Remember to close the database pool in a real application
await db.destroy();
console.log('Database connection closed.');
} catch (error) {
console.error('An error occurred during quickstart example:', error);
}
}
runExample();
kysely-codegen --version
Errors
Common errors & fixes
error: unknown option '--schema'
The `--schema` CLI option was renamed in `kysely-codegen` v0.18.0.
fixUse `--default-schema` instead of `--schema`. For other renames, refer to the v0.18.0 changelog (e.g., `--singular` to `--singularize`, `--runtime-enums-style` merged into `--runtime-enums`).
FATAL: password authentication failed for user "your_user"
Incorrect database connection string, invalid credentials, or network access issues preventing connection to the database server.
fixDouble-check your `DATABASE_URL` environment variable for correct username, password, host, port, and database name. Ensure the database server is running and accessible from where `kysely-codegen` is executed. Consider percent-encoding special characters in your password.
Module 'kysely-codegen' has no exported member 'DB'.
Attempting to import the generated `DB` type directly from the `kysely-codegen` package, instead of from the file where the types were generated.
fixAdjust your import statement to point to the actual generated file, for example: `import { DB } from './path/to/your/db.d.ts';`. Ensure `kysely-codegen` has been run and the output file exists. Error: Column 'column_name' not found in table 'table_name'
The generated types (`DB`) are out of sync with the actual database schema, or a query is referencing a non-existent column/table.
fixRun `npx kysely-codegen` again to regenerate the type definitions from your current database schema. Verify the column and table names in your Kysely query match the database.
Audit
Dependencies
kyselyrequiredRequired as the core type-safe SQL query builder library.
pgoptionalPostgreSQL driver for schema introspection.
mysql2optionalMySQL driver for schema introspection.
better-sqlite3optionalSQLite driver for schema introspection.
@libsql/kysely-libsqloptionalLibSQL driver for schema introspection.
tediousoptionalMSSQL driver for schema introspection.
tarnoptionalConnection pooling for MSSQL driver.
@tediousjs/connection-stringoptionalConnection string parsing for MSSQL driver.
kysely-bun-sqliteoptionalBun SQLite driver for schema introspection.
kysely-bun-workeroptionalBun worker-based SQLite driver for schema introspection.