Migration to 0.18.0
The follow CLI options have been changed:
--schema
has been renamed to--default-schema
.--singular
has been renamed to--singularize
.--runtime-enums
and--runtime-enums-style
have been merged into a single CLI option--runtime-enums
.
Configuration file
All 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 for all available configuration file formats.
The default configuration:
{
"camelCase": false,
"dateParser": "timestamp",
"defaultSchemas": [], // ["public"] for PostgreSQL.
"dialect": null,
"domains": true,
"envFile": null,
"excludePattern": null,
"includePattern": null,
"logLevel": "warn",
"numericParser": "string",
"outFile": "./node_modules/kysely-codegen/dist/db.d.ts",
"overrides": {},
"partitions": false,
"print": false,
"runtimeEnums": false,
"singularize": false,
"typeOnlyImports": true,
"url": "env(DATABASE_URL)",
"verify": false
}
The configuration object adds support for more advanced options:
{
"camelCase": true,
"overrides": {
"columns": {
"users.settings": "{ theme: 'dark' }"
}
},
"singularize": {
"/^(.*?)s?$/": "$1_model",
"/(bacch)(?:us|i)$/i": "$1us"
}
}
The generated output:
export interface UserModel {
settings: { theme: 'dark' };
}
// ...
export interface DB {
bacchi: Bacchus;
users: UserModel;
}
Custom serializers and dialects
The new configuration support also adds support for supplying custom serializers.
Here is a stub example of a basic Zod serializer (.kysely-codegenrc.ts
):
import { toKyselyCamelCase } from '../../generator';
import type { Config } from '../config';
const config: Config = {
logLevel: 'debug',
outFile: null,
serializer: {
serializeFile: (metadata) => {
let output = 'import { z } from "zod";\n\n';
for (const table of metadata.tables) {
output += 'export const ';
output += toKyselyCamelCase(table.name);
output += 'Schema = z.object({\n';
for (const column of table.columns) {
output += ' ';
output += column.name;
output += ': ';
switch (column.dataType) {
case 'int4':
output += 'z.number().int()';
break;
default:
output += 'z.unknown()';
}
output += ',\n';
}
output += '});\n\n';
}
return output;
},
},
url: 'postgres://user:password@localhost:5433/database',
};
export default config;
Example output:
import { z } from "zod";
export const usersSchema = z.object({
baz_qux: z.number().int(),
});
Similarly, it's also possible to supply a custom dialect
value, allowing you to create a completely new kysely-codegen dialects or extending an existing one with extra logic.
What's Changed
- feat!: Merge "runtime-enums" and "runtime-enums-style" options
- feat!: Deprecate "schemas" option and rename it to "defaultSchemas"
- feat!: Rename "singular" option to "singularize"
- feat: Support kysely-codegen configuration files (using cosmiconfig)
- feat: Add support for custom singularization rules
- feat: Merge all
...IdentifierNode
classes into a singleIdentifierNode
class - feat: Add
skipAutogenerationFileComment
serializer option - fix: Fix
overrides
not always applying to the correct column - fix: Fix test flakiness by running all tests in sequence
- fix: Make bun sqlite codegen work again (re-introduce KyselyBunSqliteIntrospectorDialect using the sqlite database from bun:sqlite and the Kysely dialect from 'kysely-bun-sqlite') by @meck93 in #238
- fix: Fix update function example @ README. by @igalklebanov in #227
- chore: Upgrade all dependencies
- chore: Allow @libsql/kysely-libsql@^0.4.1 as a peer dependency @alsiola in #233
New Contributors
Full Changelog: 0.17.0...0.18.0