diff --git a/src/index.ts b/src/index.ts index 8721ca0..7a0fcfd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 extends ColumnType ? ColumnType : ColumnType", + "", + "export type Timestamp = ColumnType", + "", + "export type Numeric = ColumnType", + "", + "export type Int8 = ColumnType", + "", + "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 extends ColumnType ? ColumnType : ColumnType", - "", - "export type Timestamp = ColumnType", - "", - "export type Numeric = ColumnType", - "", - "export type Int8 = ColumnType", - "", - "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 } - } }