mirror of
https://github.com/theoludwig/kysely-typegen.git
synced 2026-05-22 16:23:25 +02:00
Compare commits
3 Commits
v1.0.0
...
c025a63c8c
| Author | SHA1 | Date | |
|---|---|---|---|
|
c025a63c8c
|
|||
|
77f520fce7
|
|||
|
786087ee06
|
@@ -22,7 +22,7 @@ Why `kysely-typegen` if there is already `kysely-codegen`? Comparison:
|
||||
|
||||
For example: you can use [Node.js with the `--env-file` CLI option](https://nodejs.org/api/environment_variables.html), `dotenv` dependency is not required (but can be used), **you are in control**.
|
||||
|
||||
**Note:** `kysely-typegen` doesn't have the same features and customization as `kysely-codegen`, it has less features to keep it simple and lightweight, but can be extended to your own needs.
|
||||
**Note:** `kysely-typegen` doesn't have the same features and customization as `kysely-codegen`, it has less features to keep it simple and lightweight, but can be extended to your own needs. For more details, why this project was created, and the design decisions, see: <https://github.com/RobinBlomberg/kysely-codegen/issues/175>.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
@@ -76,9 +76,7 @@ const dialect = new PostgresJSDialect({
|
||||
}),
|
||||
})
|
||||
|
||||
export const database = new Kysely<DB>({
|
||||
dialect,
|
||||
})
|
||||
export const database = new Kysely<DB>({ dialect })
|
||||
```
|
||||
|
||||
### Generate the type definitions
|
||||
@@ -86,8 +84,9 @@ export const database = new Kysely<DB>({
|
||||
Create a script that uses `kysely-typegen` to introspect your database and write the generated types to a file:
|
||||
|
||||
```ts
|
||||
// codegen.ts
|
||||
// This is an example, you can be more creative and include more logic, for example time it takes to generate, number of tables/enums generated, etc.
|
||||
// scripts/typegen.ts
|
||||
// This is an example, you can be more creative:
|
||||
// time it takes to generate, number of tables/enums generated, etc.
|
||||
import fs from "node:fs"
|
||||
import path from "node:path"
|
||||
|
||||
@@ -95,12 +94,10 @@ import { KyselyTypegenPostgresDialect } from "kysely-typegen"
|
||||
|
||||
import { database } from "./database.ts"
|
||||
|
||||
const databaseTypegen = new KyselyTypegenPostgresDialect({
|
||||
database,
|
||||
})
|
||||
const databaseTypegen = new KyselyTypegenPostgresDialect({ database })
|
||||
const result = await databaseTypegen.typegen()
|
||||
const codegenContent = result.lines.join("\n")
|
||||
const codegenPath = path.join(process.cwd(), "src/codegen.ts")
|
||||
const codegenPath = path.join(process.cwd(), "codegen.ts")
|
||||
await fs.promises.writeFile(codegenPath, codegenContent, "utf-8")
|
||||
await database.destroy()
|
||||
```
|
||||
@@ -108,7 +105,7 @@ await database.destroy()
|
||||
Run with Node.js (using `--env-file` to load environment variables, no `dotenv` dependency required):
|
||||
|
||||
```sh
|
||||
node --env-file=.env scripts/codegen.ts
|
||||
node --env-file=.env scripts/typegen.ts
|
||||
```
|
||||
|
||||
### Using the type definitions
|
||||
@@ -118,7 +115,7 @@ Import `DB` into `new Kysely<DB>`, and you're done!
|
||||
```ts
|
||||
import { database } from "./database.ts"
|
||||
|
||||
const rows = await database.selectFrom("users").selectAll().execute()
|
||||
const rows = await database.selectFrom("User").selectAll().execute()
|
||||
// ^ { createdAt: Date; email: string; id: number; ... }[]
|
||||
```
|
||||
|
||||
|
||||
@@ -103,506 +103,7 @@ exports[`typegen > generate types matching snapshot 1`] = `
|
||||
" Users: Users",
|
||||
"}"
|
||||
],
|
||||
"enums": [
|
||||
{
|
||||
"name": "Currency",
|
||||
"values": [
|
||||
"EUR",
|
||||
"GBP",
|
||||
"USD"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "OrderStatus",
|
||||
"values": [
|
||||
"cancelled",
|
||||
"paid",
|
||||
"pending",
|
||||
"shipped"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "UserRole",
|
||||
"values": [
|
||||
"admin",
|
||||
"guest",
|
||||
"member"
|
||||
]
|
||||
}
|
||||
],
|
||||
"tables": [
|
||||
{
|
||||
"columns": [
|
||||
{
|
||||
"dataType": "uuid",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "id"
|
||||
},
|
||||
{
|
||||
"dataType": "bool",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colBool"
|
||||
},
|
||||
{
|
||||
"dataType": "bool",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": true,
|
||||
"name": "colBoolNullable"
|
||||
},
|
||||
{
|
||||
"dataType": "bool",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colBoolDefault"
|
||||
},
|
||||
{
|
||||
"dataType": "int2",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colInt2"
|
||||
},
|
||||
{
|
||||
"dataType": "int4",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colInt4"
|
||||
},
|
||||
{
|
||||
"dataType": "int8",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colInt8"
|
||||
},
|
||||
{
|
||||
"dataType": "float4",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colFloat4"
|
||||
},
|
||||
{
|
||||
"dataType": "float8",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colFloat8"
|
||||
},
|
||||
{
|
||||
"dataType": "numeric",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colNumeric"
|
||||
},
|
||||
{
|
||||
"dataType": "money",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colMoney"
|
||||
},
|
||||
{
|
||||
"dataType": "text",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colText"
|
||||
},
|
||||
{
|
||||
"dataType": "varchar",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colVarchar"
|
||||
},
|
||||
{
|
||||
"dataType": "bpchar",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colBpchar"
|
||||
},
|
||||
{
|
||||
"dataType": "bytea",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colBytea"
|
||||
},
|
||||
{
|
||||
"dataType": "date",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colDate"
|
||||
},
|
||||
{
|
||||
"dataType": "time",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colTime"
|
||||
},
|
||||
{
|
||||
"dataType": "timetz",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colTimetz"
|
||||
},
|
||||
{
|
||||
"dataType": "timestamp",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colTimestamp"
|
||||
},
|
||||
{
|
||||
"dataType": "timestamptz",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colTimestamptz"
|
||||
},
|
||||
{
|
||||
"dataType": "json",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colJson"
|
||||
},
|
||||
{
|
||||
"dataType": "jsonb",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colJsonb"
|
||||
},
|
||||
{
|
||||
"dataType": "uuid",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colUuid"
|
||||
},
|
||||
{
|
||||
"dataType": "inet",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colInet"
|
||||
},
|
||||
{
|
||||
"dataType": "cidr",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colCidr"
|
||||
},
|
||||
{
|
||||
"dataType": "macaddr",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colMacaddr"
|
||||
},
|
||||
{
|
||||
"dataType": "bit",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colBit"
|
||||
},
|
||||
{
|
||||
"dataType": "varbit",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colVarbit"
|
||||
},
|
||||
{
|
||||
"dataType": "xml",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colXml"
|
||||
},
|
||||
{
|
||||
"dataType": "tsvector",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colTsvector"
|
||||
},
|
||||
{
|
||||
"dataType": "tsquery",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colTsquery"
|
||||
},
|
||||
{
|
||||
"dataType": "point",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colPoint"
|
||||
},
|
||||
{
|
||||
"dataType": "line",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colLine"
|
||||
},
|
||||
{
|
||||
"dataType": "lseg",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colLseg"
|
||||
},
|
||||
{
|
||||
"dataType": "box",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colBox"
|
||||
},
|
||||
{
|
||||
"dataType": "path",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colPath"
|
||||
},
|
||||
{
|
||||
"dataType": "polygon",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colPolygon"
|
||||
},
|
||||
{
|
||||
"dataType": "oid",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colOid"
|
||||
},
|
||||
{
|
||||
"dataType": "text",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": true,
|
||||
"name": "colTextNullable"
|
||||
},
|
||||
{
|
||||
"dataType": "jsonb",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colJsonbDefault"
|
||||
},
|
||||
{
|
||||
"dataType": "timestamp",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "colTimestampDefault"
|
||||
},
|
||||
{
|
||||
"dataType": "timestamptz",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "createdAt"
|
||||
},
|
||||
{
|
||||
"dataType": "timestamptz",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": true,
|
||||
"name": "updatedAt"
|
||||
}
|
||||
],
|
||||
"isForeign": false,
|
||||
"isView": false,
|
||||
"name": "AllTypes",
|
||||
"schema": "public"
|
||||
},
|
||||
{
|
||||
"columns": [
|
||||
{
|
||||
"dataType": "int8",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": true,
|
||||
"isNullable": false,
|
||||
"name": "id"
|
||||
},
|
||||
{
|
||||
"dataType": "uuid",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "userId"
|
||||
},
|
||||
{
|
||||
"dataType": "OrderStatus",
|
||||
"dataTypeSchema": "public",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "status"
|
||||
},
|
||||
{
|
||||
"dataType": "Currency",
|
||||
"dataTypeSchema": "public",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "currency"
|
||||
},
|
||||
{
|
||||
"dataType": "int4",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "amountCents"
|
||||
},
|
||||
{
|
||||
"dataType": "text",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": true,
|
||||
"name": "note"
|
||||
},
|
||||
{
|
||||
"dataType": "timestamptz",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "createdAt"
|
||||
}
|
||||
],
|
||||
"isForeign": false,
|
||||
"isView": false,
|
||||
"name": "Orders",
|
||||
"schema": "public"
|
||||
},
|
||||
{
|
||||
"columns": [
|
||||
{
|
||||
"dataType": "uuid",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "id"
|
||||
},
|
||||
{
|
||||
"dataType": "varchar",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "username"
|
||||
},
|
||||
{
|
||||
"dataType": "text",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": false,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": true,
|
||||
"name": "email"
|
||||
},
|
||||
{
|
||||
"dataType": "UserRole",
|
||||
"dataTypeSchema": "public",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "role"
|
||||
},
|
||||
{
|
||||
"dataType": "bool",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "isActive"
|
||||
},
|
||||
{
|
||||
"dataType": "timestamptz",
|
||||
"dataTypeSchema": "pg_catalog",
|
||||
"hasDefaultValue": true,
|
||||
"isAutoIncrementing": false,
|
||||
"isNullable": false,
|
||||
"name": "createdAt"
|
||||
}
|
||||
],
|
||||
"isForeign": false,
|
||||
"isView": false,
|
||||
"name": "Users",
|
||||
"schema": "public"
|
||||
}
|
||||
]
|
||||
"tablesCount": 3,
|
||||
"enumsCount": 3
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -237,6 +237,10 @@ describe("typegen", () => {
|
||||
const result = await databaseTypegen.typegen()
|
||||
|
||||
// Assert - Then
|
||||
testContext.assert.snapshot(result)
|
||||
testContext.assert.snapshot({
|
||||
lines: result.lines,
|
||||
tablesCount: result.tables.length,
|
||||
enumsCount: result.enums.length,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+6
-1
@@ -173,7 +173,12 @@ export class KyselyTypegenPostgresDialect extends KyselyTypegenDialect {
|
||||
const enums = new Map<string, string[]>()
|
||||
for (const row of rows) {
|
||||
const data = row as { enumName: string; enumValue: string }
|
||||
enums.set(data.enumName, [...(enums.get(data.enumName) ?? []), data.enumValue])
|
||||
const existing = enums.get(data.enumName)
|
||||
if (existing == null) {
|
||||
enums.set(data.enumName, [data.enumValue])
|
||||
} else {
|
||||
existing.push(data.enumValue)
|
||||
}
|
||||
}
|
||||
return enums
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user