mirror of
https://github.com/theoludwig/kysely-typegen.git
synced 2026-05-22 16:23:25 +02:00
refactor: move typegen() to KyselyTypegenDialect base
This commit is contained in:
+39
-48
@@ -70,11 +70,48 @@ export abstract class KyselyTypegenDialect {
|
||||
return result
|
||||
}
|
||||
|
||||
public abstract typegen(): Promise<{
|
||||
public async typegen(): Promise<{
|
||||
lines: string[]
|
||||
enums: EnumMetadata[]
|
||||
tables: TableMetadata[]
|
||||
}>
|
||||
}> {
|
||||
const [tables, enums] = await Promise.all([this.getTables(), this.getEnums()])
|
||||
const lines: string[] = [
|
||||
`// This file was automatically generated by \`kysely-typegen\`.`,
|
||||
"// Do not edit this file manually.",
|
||||
"",
|
||||
'import type { ColumnType } from "kysely"',
|
||||
"",
|
||||
"export type Generated<T> = T extends ColumnType<infer S, infer I, infer U> ? ColumnType<S, I | undefined, U> : ColumnType<T, T | undefined, T>",
|
||||
"",
|
||||
"export type Timestamp = ColumnType<Date, Date | string, Date | string>",
|
||||
"",
|
||||
"export type Numeric = ColumnType<string, number | string, number | string>",
|
||||
"",
|
||||
"export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>",
|
||||
"",
|
||||
"export type Json = JsonValue",
|
||||
"",
|
||||
"export type JsonArray = JsonValue[]",
|
||||
"",
|
||||
"export interface JsonObject {",
|
||||
" [x: string]: JsonValue | undefined",
|
||||
"}",
|
||||
"",
|
||||
"export type JsonPrimitive = boolean | number | string | null",
|
||||
"",
|
||||
"export type JsonValue = JsonArray | JsonObject | JsonPrimitive",
|
||||
"",
|
||||
]
|
||||
lines.push(...this.getEnumsTypegen(enums))
|
||||
lines.push(...this.getTablesTypegen(tables, enums))
|
||||
lines.push("export interface DB {")
|
||||
for (const table of tables) {
|
||||
lines.push(` ${table.name}: ${table.name}`)
|
||||
}
|
||||
lines.push("}")
|
||||
return { lines, enums, tables }
|
||||
}
|
||||
}
|
||||
export class KyselyTypegenPostgresDialect extends KyselyTypegenDialect {
|
||||
public database: KyselyTypegenDialect["database"]
|
||||
@@ -142,50 +179,4 @@ export class KyselyTypegenPostgresDialect extends KyselyTypegenDialect {
|
||||
}
|
||||
return enums
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate TypeScript types based on the database schema, including tables and enums.
|
||||
*/
|
||||
public async typegen(): Promise<{
|
||||
lines: string[]
|
||||
enums: EnumMetadata[]
|
||||
tables: TableMetadata[]
|
||||
}> {
|
||||
const [tables, enums] = await Promise.all([this.getTables(), this.getEnums()])
|
||||
const lines: string[] = [
|
||||
`// This file was automatically generated by \`kysely-typegen\`.`,
|
||||
"// Do not edit this file manually.",
|
||||
"",
|
||||
'import type { ColumnType } from "kysely"',
|
||||
"",
|
||||
"export type Generated<T> = T extends ColumnType<infer S, infer I, infer U> ? ColumnType<S, I | undefined, U> : ColumnType<T, T | undefined, T>",
|
||||
"",
|
||||
"export type Timestamp = ColumnType<Date, Date | string, Date | string>",
|
||||
"",
|
||||
"export type Numeric = ColumnType<string, number | string, number | string>",
|
||||
"",
|
||||
"export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>",
|
||||
"",
|
||||
"export type Json = JsonValue",
|
||||
"",
|
||||
"export type JsonArray = JsonValue[]",
|
||||
"",
|
||||
"export interface JsonObject {",
|
||||
" [x: string]: JsonValue | undefined",
|
||||
"}",
|
||||
"",
|
||||
"export type JsonPrimitive = boolean | number | string | null",
|
||||
"",
|
||||
"export type JsonValue = JsonArray | JsonObject | JsonPrimitive",
|
||||
"",
|
||||
]
|
||||
lines.push(...this.getEnumsTypegen(enums))
|
||||
lines.push(...this.getTablesTypegen(tables, enums))
|
||||
lines.push("export interface DB {")
|
||||
for (const table of tables) {
|
||||
lines.push(` ${table.name}: ${table.name}`)
|
||||
}
|
||||
lines.push("}")
|
||||
return { lines, enums, tables }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user