{"_id":"@ashlesss/kysely-codegen","name":"@ashlesss/kysely-codegen","dist-tags":{"latest":"0.19.1"},"versions":{"0.19.1":{"name":"@ashlesss/kysely-codegen","version":"0.19.1","author":{"name":"Robin Blomberg"},"license":"MIT","main":"./dist/index.js","types":"./dist/index.d.ts","bin":{"kysely-codegen":"dist/cli/bin.js"},"repository":{"type":"git","url":"git+https://github.com/ashless/kysely-codegen.git"},"bugs":{"url":"https://github.com/RobinBlomberg/kysely-codegen/issues"},"homepage":"https://github.com/RobinBlomberg/kysely-codegen#readme","dependencies":{"chalk":"4.1.2","cosmiconfig":"^9.0.0","dotenv":"^17.2.1","dotenv-expand":"^12.0.2","git-diff":"^2.0.6","micromatch":"^4.0.8","minimist":"^1.2.8","pluralize":"^8.0.0","zod":"^4.1.5"},"devDependencies":{"@babel/core":"^7.28.3","@babel/eslint-parser":"^7.28.0","@libsql/kysely-libsql":"^0.4.1","@robinblomberg/eslint-config-prettier":"^0.1.4","@robinblomberg/eslint-config-robinblomberg":"0.30.1","@robinblomberg/prettier-config":"^0.2.0","@tediousjs/connection-string":"^0.5.0","@types/better-sqlite3":"^7.6.13","@types/bun":"^1.2.21","@types/git-diff":"^2.0.7","@types/micromatch":"^4.0.9","@types/minimist":"^1.2.5","@types/node":"^24.3.0","@types/pg":"^8.15.5","@types/pluralize":"^0.0.33","@typescript-eslint/eslint-plugin":"^8.41.0","@typescript-eslint/parser":"^8.41.0","better-sqlite3":"^12.2.0","cspell-cli":"^9.2.0","eslint":"^8.57.0","eslint-plugin-import":"^2.32.0","eslint-plugin-jsdoc":"48.11.0","eslint-plugin-sonarjs":"^3.0.5","eslint-plugin-sort-exports":"^0.9.1","eslint-plugin-sort-keys":"^2.3.5","eslint-plugin-storybook":"^9.1.3","eslint-plugin-unicorn":"56.0.1","execa":"^9.6.0","knip":"^5.63.0","kysely":"^0.28.5","kysely-bun-sqlite":"^0.4.0","kysely-bun-worker":"^1.2.1","madge":"^8.0.0","mysql2":"^3.14.4","npm-run-all":"^4.1.5","pg":"^8.16.3","postgres-interval":"^4.0.2","prettier":"^3.6.2","rimraf":"^6.0.1","tarn":"^3.0.2","tedious":"^18.6.1","ts-dedent":"^2.2.0","tsx":"^4.20.5","typescript":"^5.9.2","vitest":"^3.2.4"},"peerDependencies":{"@libsql/kysely-libsql":">=0.3.0 <0.5.0","@tediousjs/connection-string":">=0.5.0 <0.6.0","better-sqlite3":">=7.6.2 <13.0.0","kysely":">=0.27.0 <1.0.0","kysely-bun-sqlite":">=0.3.2 <1.0.0","kysely-bun-worker":">=1.2.0 <2.0.0","mysql2":">=2.3.3 <4.0.0","pg":">=8.8.0 <9.0.0","tarn":">=3.0.0 <4.0.0","tedious":">=18.0.0 <20.0.0"},"peerDependenciesMeta":{"@libsql/kysely-libsql":{"optional":true},"@tediousjs/connection-string":{"optional":true},"better-sqlite3":{"optional":true},"kysely":{"optional":false},"kysely-bun-sqlite":{"optional":true},"kysely-bun-worker":{"optional":true},"mysql2":{"optional":true},"pg":{"optional":true},"tarn":{"optional":true},"tedious":{"optional":true}},"engines":{"node":">=20.0.0"},"eslintConfig":{"extends":["@robinblomberg/robinblomberg","@robinblomberg/prettier"],"ignorePatterns":"**/*.snapshot.ts","rules":{"@typescript-eslint/consistent-type-imports":[1,{"disallowTypeAnnotations":false,"fixStyle":"separate-type-imports","prefer":"type-imports"}],"unicorn/no-typeof-undefined":[1,{"checkGlobalVariables":false}],"unicorn/prefer-node-protocol":0}},"knip":{"ignore":["src/cli/test/config-with-custom-serializer.ts","src/cli/test/config.cjs","src/db.ts","**/*.snapshot.ts"],"ignoreBinaries":["docker-compose","ncu"],"ignoreDependencies":["@babel/core","@babel/eslint-parser","@libsql/kysely-libsql","@tediousjs/connection-string","@typescript-eslint/eslint-plugin","@typescript-eslint/parser","better-sqlite3","eslint-plugin-import","eslint-plugin-jsdoc","eslint-plugin-sonarjs","eslint-plugin-sort-exports","eslint-plugin-sort-keys","eslint-plugin-storybook","eslint-plugin-unicorn","kysely-bun-sqlite","kysely-bun-worker","mysql2","pg","tarn","tedious"]},"madge":{"detectiveOptions":{"ts":{"skipTypeImports":true}}},"prettier":"@robinblomberg/prettier-config","scripts":{"build":"rimraf dist && tsc --project ./tsconfig.build.json","check:types":"tsc --noEmit","ci":"run-s ci:*","ci:build":"pnpm build","ci:circular-imports":"madge -c ./src/index.ts","ci:eslint":"pnpm lint:eslint --max-warnings=0 --report-unused-disable-directives","ci:prettier":"pnpm lint:prettier","ci:spelling":"cspell-cli -e dist -e pnpm-lock.yaml -e '*.svg' .","ci:test":"pnpm test","ci:unused":"knip","dev":"tsx watch ./src/cli/bin.ts","docker:up":"docker-compose up -d","fix":"run-s fix:*","fix:eslint":"eslint --fix src","fix:prettier":"prettier --write src","lint":"run-p lint:*","lint:eslint":"eslint src","lint:prettier":"prettier --check src","start":"pnpm build && DATABASE_URL=postgres://user:password@localhost:5433/database node ./dist/cli/bin.js","test":"vitest run --globals --fileParallelism=false --maxConcurrency=1 --maxWorkers=1 --sequence.seed=1","test:watch":"vitest watch --globals --fileParallelism=false --maxConcurrency=1 --maxWorkers=1 --sequence.seed=1","upgrade":"ncu -u"},"_id":"@ashlesss/kysely-codegen@0.19.1","description":"`kysely-codegen` generates Kysely type definitions from your database. That's it.","_integrity":"sha512-7ZETbLaQdslSmLwpV22AdXeYlwCghZb3VBXHfFnHnJcyrGpfcLyRPIO+Cb70VDZqVkRzDxvxq2Iz+Za4qtvljg==","_resolved":"/tmp/aedf8d611a72650c0cf11b0207c03549/ashlesss-kysely-codegen-0.19.1.tgz","_from":"file:ashlesss-kysely-codegen-0.19.1.tgz","_nodeVersion":"22.16.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-7ZETbLaQdslSmLwpV22AdXeYlwCghZb3VBXHfFnHnJcyrGpfcLyRPIO+Cb70VDZqVkRzDxvxq2Iz+Za4qtvljg==","shasum":"ac98cca5c01f3c9bb13f41c8d3def0a76c8128f0","tarball":"https://registry.npmjs.org/@ashlesss/kysely-codegen/-/kysely-codegen-0.19.1.tgz","fileCount":281,"unpackedSize":347578,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIGmxb72+gk4uVRmo4kJIffuNN5f1n5ICA3cdvgYtrfdDAiB+id5BOFkt3sf8pKgCJFixpNDtmwQDYTLedivF3LWtMw=="}]},"_npmUser":{"name":"ashlesss","email":"hidahza@gmail.com"},"directories":{},"maintainers":[{"name":"ashlesss","email":"hidahza@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/kysely-codegen_0.19.1_1763613189607_0.3864579268836814"},"_hasShrinkwrap":false}},"time":{"created":"2025-11-20T04:33:09.523Z","0.19.1":"2025-11-20T04:33:09.828Z","modified":"2025-11-20T04:33:10.092Z"},"maintainers":[{"name":"ashlesss","email":"hidahza@gmail.com"}],"description":"`kysely-codegen` generates Kysely type definitions from your database. That's it.","homepage":"https://github.com/RobinBlomberg/kysely-codegen#readme","repository":{"type":"git","url":"git+https://github.com/ashless/kysely-codegen.git"},"author":{"name":"Robin Blomberg"},"bugs":{"url":"https://github.com/RobinBlomberg/kysely-codegen/issues"},"license":"MIT","readme":"# ![kysely-codegen](./assets/kysely-codegen-logo.svg) <!-- omit from toc -->\n\n`kysely-codegen` generates Kysely type definitions from your database. That's it.\n\n## Table of contents <!-- omit from toc -->\n\n- [Installation](#installation)\n- [Generating type definitions](#generating-type-definitions)\n- [Using the type definitions](#using-the-type-definitions)\n- [CLI arguments](#cli-arguments) - [Basic example](#basic-example) - [Named imports with aliasing](#named-imports-with-aliasing)\n- [Configuration file](#configuration-file)\n\n## Installation\n\n```sh\nnpm install --save-dev kysely-codegen\n```\n\nYou will also need to install Kysely with your driver of choice:\n\n```sh\n# PostgreSQL\nnpm install kysely pg\n\n# MySQL\nnpm install kysely mysql2\n\n# SQLite\nnpm install kysely better-sqlite3\n\n# MSSQL\nnpm install kysely tedious tarn @tediousjs/connection-string@0.5.0\n\n# LibSQL\nnpm install @libsql/kysely-libsql\n```\n\n## Generating type definitions\n\nThe most convenient way to get started is to create an `.env` file with your database connection string:\n\n```sh\n# PostgreSQL\nDATABASE_URL=postgres://username:password@yourdomain.com/database\n\n# MySQL\nDATABASE_URL=mysql://username:password@yourdomain.com/database\n\n# SQLite\nDATABASE_URL=C:/Program Files/sqlite3/db\n\n# MSSQL\nDATABASE_URL=Server=mssql;Database=database;User Id=user;Password=password\n\n# LibSQL\nDATABASE_URL=libsql://token@host:port/database\n```\n\n> If your URL contains a password with special characters, those characters may need to be [percent-encoded](https://en.wikipedia.org/wiki/Percent-encoding#Reserved_characters).\n>\n> If you are using _PlanetScale_, make sure your URL contains this SSL query string parameter: `ssl={\"rejectUnauthorized\":true}`\n\nThen run the following command, or add it to the scripts section in your package.json file:\n\n```sh\nkysely-codegen\n```\n\nThis command will generate a `.d.ts` file from your database, for example:\n\n<!-- prettier-ignore -->\n```ts\nimport { ColumnType } from 'kysely';\n\nexport type Generated<T> = T extends ColumnType<infer S, infer I, infer U>\n  ? ColumnType<S, I | undefined, U>\n  : ColumnType<T, T | undefined, T>;\n\nexport type Timestamp = ColumnType<Date, Date | string, Date | string>;\n\nexport interface Company {\n  id: Generated<number>;\n  name: string;\n}\n\nexport interface User {\n  company_id: number | null;\n  created_at: Generated<Timestamp>;\n  email: string;\n  id: Generated<number>;\n  is_active: boolean;\n  name: string;\n  updated_at: Timestamp;\n}\n\nexport interface DB {\n  company: Company;\n  user: User;\n}\n```\n\nTo specify a different output file:\n\n```sh\nkysely-codegen --out-file ./src/db/db.d.ts\n```\n\n## Using the type definitions\n\nImport `DB` into `new Kysely<DB>`, and you're done!\n\n```ts\nimport { Kysely, PostgresDialect } from 'kysely';\nimport { DB } from 'kysely-codegen';\nimport { Pool } from 'pg';\n\nconst db = new Kysely<DB>({\n  dialect: new PostgresDialect({\n    pool: new Pool({\n      connectionString: process.env.DATABASE_URL,\n    }),\n  }),\n});\n\nconst rows = await db.selectFrom('users').selectAll().execute();\n//    ^ { created_at: Date; email: string; id: number; ... }[]\n```\n\nIf you need to use the generated types in e.g. function parameters and type definitions, you may need to use the Kysely `Insertable`, `Selectable`, `Updateable` types. Note that you don't need to explicitly annotate query return values, as it's recommended to let Kysely infer the types for you.\n\n```ts\nimport { Insertable, Updateable } from 'kysely';\nimport { DB } from 'kysely-codegen';\nimport { db } from './db';\n\nasync function insertUser(user: Insertable<User>) {\n  return await db\n    .insertInto('users')\n    .values(user)\n    .returningAll()\n    .executeTakeFirstOrThrow();\n  // ^ Selectable<User>\n}\n\nasync function updateUser(id: number, user: Updateable<User>) {\n  return await db\n    .updateTable('users')\n    .set(user)\n    .where('id', '=', id)\n    .returning(['email', 'id'])\n    .executeTakeFirstOrThrow();\n  // ^ { email: string; id: number; }\n}\n```\n\nRead the [Kysely documentation](https://kysely.dev/docs/getting-started) for more information.\n\n## CLI arguments\n\n#### --camel-case <!-- omit from toc -->\n\nUse the Kysely CamelCasePlugin for generated table column names.\n\n**Example:**\n\n```ts\nexport interface User {\n  companyId: number | null;\n  createdAt: Generated<Timestamp>;\n  email: string;\n  id: Generated<number>;\n  isActive: boolean;\n  name: string;\n  updatedAt: Timestamp;\n}\n```\n\n#### --config-file <!-- omit from toc -->\n\nSpecify the path to the configuration file to use.\n\n#### --custom-imports <!-- omit from toc -->\n\nSpecify custom type imports to use with type overrides. This is particularly useful when using custom types from external packages or local files.\n\n##### Basic example\n\n```sh\nkysely-codegen --custom-imports='{\"InstantRange\":\"./custom-types\",\"MyCustomType\":\"@my-org/custom-types\"}'\n```\n\n##### Named imports with aliasing\n\nYou can import specific named exports and optionally alias them using the `#` syntax:\n\n```sh\nkysely-codegen --custom-imports='{\"MyType\":\"./types#OriginalType\",\"DateRange\":\"@org/utils#CustomDateRange\"}'\n```\n\nThis generates:\n\n```ts\nimport type { OriginalType as MyType } from './types';\nimport type { CustomDateRange as DateRange } from '@org/utils';\n```\n\nThen you can use these imported types in your overrides:\n\n```sh\nkysely-codegen --overrides='{\"columns\":{\"events.date_range\":\"ColumnType<DateRange, DateRange, never>\"}}'\n```\n\n#### --date-parser <!-- omit from toc -->\n\nSpecify which parser to use for PostgreSQL date values. (values: `string`/`timestamp`, default: `timestamp`)\n\n#### --default-schema [value] <!-- omit from toc -->\n\nSet the default schema(s) for the database connection.\n\nMultiple schemas can be specified:\n\n```sh\nkysely-codegen --default-schema=public --default-schema=hidden\n```\n\n#### --dialect [value] <!-- omit from toc -->\n\nSet the SQL dialect. (values: `postgres`/`mysql`/`sqlite`/`mssql`/`libsql`/`bun-sqlite`/`kysely-bun-sqlite`/`worker-bun-sqlite`)\n\n#### --env-file [value] <!-- omit from toc -->\n\nSpecify the path to an environment file to use.\n\n#### --help, -h <!-- omit from toc -->\n\nPrint all command line options.\n\n#### --include-pattern [value], --exclude-pattern [value] <!-- omit from toc -->\n\nYou can choose which tables should be included during code generation by providing a glob pattern to the `--include-pattern` and `--exclude-pattern` flags. We use [micromatch](https://github.com/micromatch/micromatch) under the hood, which provides advanced glob support. For instance, if you only want to include your public tables:\n\n```sh\nkysely-codegen --include-pattern=\"public.*\"\n```\n\nYou can also include only certain tables within a schema:\n\n```sh\nkysely-codegen --include-pattern=\"public.+(user|post)\"\n```\n\nOr exclude an entire class of tables:\n\n```sh\nkysely-codegen --exclude-pattern=\"documents.*\"\n```\n\n#### --log-level [value] <!-- omit from toc -->\n\nSet the terminal log level. (values: `debug`/`info`/`warn`/`error`/`silent`, default: `warn`)\n\n#### --no-domains <!-- omit from toc -->\n\nSkip generating types for PostgreSQL domains. (default: `false`)\n\n#### --numeric-parser <!-- omit from toc -->\n\nSpecify which parser to use for PostgreSQL numeric values. (values: `string`/`number`/`number-or-string`, default: `string`)\n\n#### --overrides <!-- omit from toc -->\n\nSpecify type overrides for specific table columns in JSON format.\n\n**Example:**\n\n```sh\nkysely-codegen --overrides='{\"columns\":{\"table_name.column_name\":\"{foo:\\\"bar\\\"}\"}}'\n```\n\n#### --out-file [value] <!-- omit from toc -->\n\nSet the file build path. (default: `./node_modules/kysely-codegen/dist/db.d.ts`)\n\n#### --partitions <!-- omit from toc -->\n\nInclude partitions of PostgreSQL tables in the generated code.\n\n#### --print <!-- omit from toc -->\n\nPrint the generated output to the terminal instead of a file.\n\n#### --runtime-enums <!-- omit from toc -->\n\nThe PostgreSQL `--runtime-enums` option generates runtime enums instead of string unions. You can optionally specify which naming convention to use for runtime enum keys. (values: [`pascal-case`, `screaming-snake-case`], default: `screaming-snake-case`)\n\n**Examples:**\n\n`--runtime-enums=false`\n\n```ts\nexport type Status = 'CONFIRMED' | 'UNCONFIRMED';\n```\n\n`--runtime-enums` or `--runtime-enums=screaming-snake-case`\n\n```ts\nexport enum Status {\n  CONFIRMED = 'CONFIRMED',\n  UNCONFIRMED = 'UNCONFIRMED',\n}\n```\n\n`--runtime-enums=pascal-case`\n\n```ts\nexport enum Status {\n  Confirmed = 'CONFIRMED',\n  Unconfirmed = 'UNCONFIRMED',\n}\n```\n\n#### --singularize <!-- omit from toc -->\n\nSingularize generated type aliases, e.g. as `BlogPost` instead of `BlogPosts`. The codegen uses the [pluralize](https://www.npmjs.com/package/pluralize) package for singularization.\n\nYou can specify custom singularization rules in the [configuration file](#configuration-file).\n\n#### --type-mapping <!-- omit from toc -->\n\nSpecify type mappings for database types, in JSON format. This allows you to automatically map database types to custom TypeScript types.\n\n**Example:**\n\n```sh\nkysely-codegen --type-mapping='{\"timestamptz\":\"Temporal.Instant\",\"tstzrange\":\"InstantRange\"}' --custom-imports='{\"Temporal\":\"@js-temporal/polyfill\",\"InstantRange\":\"./custom-types\"}'\n```\n\nThis is especially useful when you want to use modern JavaScript types like Temporal API instead of Date objects:\n\n```json\n{\n  \"typeMapping\": {\n    \"date\": \"Temporal.PlainDate\",\n    \"daterange\": \"DateRange\",\n    \"interval\": \"Temporal.Duration\",\n    \"time\": \"Temporal.PlainTime\",\n    \"timestamp\": \"Temporal.Instant\",\n    \"timestamptz\": \"Temporal.Instant\",\n    \"tsrange\": \"InstantRange\",\n    \"tstzrange\": \"InstantRange\"\n  }\n}\n```\n\nType mappings are automatically applied to all columns of the specified database type, eliminating the need to override each column individually. This feature works with all supported databases, though some types (like PostgreSQL range types) are database-specific.\n\n#### --type-only-imports <!-- omit from toc -->\n\nGenerate code using the TypeScript 3.8+ `import type` syntax. (default: `true`)\n\n#### --url [value] <!-- omit from toc -->\n\nSet the database connection string URL. This may point to an environment variable. (default: `env(DATABASE_URL)`)\n\n#### --verify <!-- omit from toc -->\n\nVerify that the generated types are up-to-date. (default: `false`)\n\n## Configuration file\n\nAll codegen options can also be configured in a `.kysely-codegenrc.json` (or `.js`, `.ts`, `.yaml` etc.) file or the `kysely-codegen` property in `package.json`. See [Cosmiconfig](https://github.com/cosmiconfig/cosmiconfig) for all available configuration file formats.\n\nThe default configuration:\n\n```json\n{\n  \"camelCase\": false,\n  \"customImports\": {},\n  \"dateParser\": \"timestamp\",\n  \"defaultSchemas\": [], // [\"public\"] for PostgreSQL.\n  \"dialect\": null,\n  \"domains\": true,\n  \"envFile\": null,\n  \"excludePattern\": null,\n  \"includePattern\": null,\n  \"logLevel\": \"warn\",\n  \"numericParser\": \"string\",\n  \"outFile\": \"./node_modules/kysely-codegen/dist/db.d.ts\",\n  \"overrides\": {},\n  \"partitions\": false,\n  \"print\": false,\n  \"runtimeEnums\": false,\n  \"singularize\": false,\n  \"typeMapping\": {},\n  \"typeOnlyImports\": true,\n  \"url\": \"env(DATABASE_URL)\",\n  \"verify\": false\n}\n```\n\nThe configuration object adds support for more advanced options:\n\n```json\n{\n  \"camelCase\": true,\n  \"customImports\": {\n    \"InstantRange\": \"./custom-types\",\n    \"MyCustomType\": \"@my-org/custom-types\",\n    \"AliasedType\": \"./types#OriginalType\"\n  },\n  \"overrides\": {\n    \"columns\": {\n      \"events.date_range\": \"ColumnType<InstantRange, InstantRange, never>\",\n      \"posts.author_type\": \"AliasedType\",\n      \"users.settings\": \"{ theme: 'dark' }\"\n    }\n  },\n  \"singularize\": {\n    \"/^(.*?)s?$/\": \"$1_model\",\n    \"/(bacch)(?:us|i)$/i\": \"$1us\"\n  },\n  \"typeMapping\": {\n    \"date\": \"Temporal.PlainDate\",\n    \"interval\": \"Temporal.Duration\",\n    \"timestamptz\": \"Temporal.Instant\"\n  }\n}\n```\n\nThe generated output:\n\n```ts\nimport type { InstantRange } from './custom-types';\nimport type { MyCustomType } from '@my-org/custom-types';\nimport type { OriginalType as AliasedType } from './types';\nimport type { Temporal } from '@js-temporal/polyfill';\n\nexport interface EventModel {\n  createdAt: Temporal.Instant;\n  dateRange: ColumnType<InstantRange, InstantRange, never>;\n  eventDate: Temporal.PlainDate;\n}\n\nexport interface UserModel {\n  settings: { theme: 'dark' };\n}\n\n// ...\n\nexport interface DB {\n  bacchi: Bacchus;\n  events: EventModel;\n  users: UserModel;\n}\n```\n","readmeFilename":"README.md","_rev":"1-6eedb8ce09bbc13911ec771cf5a3358e"}