mirror of
https://github.com/theoludwig/kysely-typegen.git
synced 2026-05-22 16:23:25 +02:00
e9adb364e0
To prepare for additional dialects and keep the default entry tiny,
`KyselyTypegenPostgresDialect` is now exported from a dedicated
`kysely-typegen/postgres` subpath instead of the package root.
The `KyselyTypegenDialect` base class is also reworked to support
dialects where enums are declared per-column rather
than as a named top-level type. The abstract `getEnumsMap()` method
is replaced by an optional `introspectEnums()` hook returning both
`named` and `inline` enums.
BREAKING CHANGE: `KyselyTypegenPostgresDialect` is no longer exported
from `kysely-typegen`. Import it from `kysely-typegen/postgres`.
Before:
```ts
import { KyselyTypegenPostgresDialect } from "kysely-typegen"
```
After:
```ts
import { KyselyTypegenPostgresDialect } from "kysely-typegen/postgres"
```
BREAKING CHANGE: custom dialects extending `KyselyTypegenDialect`
must replace `protected getEnumsMap()` with `protected introspectEnums()`,
which returns `{ named, inline }` instead of a flat `Map<string, string[]>`.
Before:
```ts
protected async getEnumsMap(): Promise<Map<string, string[]>> {
return new Map([["Role", ["admin", "member"]]])
}
```
After:
```ts
protected override async introspectEnums(): Promise<IntrospectedEnums> {
return {
named: [{ name: "Role", values: ["admin", "member"] }],
inline: new Map(),
}
}
```
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import type { EnumMetadata, IntrospectedEnums } from "./index.ts"
|
|
import { KyselyTypegenDialect } from "./index.ts"
|
|
|
|
export class KyselyTypegenPostgresDialect extends KyselyTypegenDialect {
|
|
// These types have been found through experimentation in Adminer and in the 'pg' source code.
|
|
public override readonly scalars: Record<string, string> = {
|
|
bit: "string",
|
|
bool: "boolean",
|
|
box: "string",
|
|
bpchar: "string",
|
|
bytea: "Buffer",
|
|
cidr: "string",
|
|
date: "Timestamp",
|
|
float4: "number",
|
|
float8: "number",
|
|
inet: "string",
|
|
int2: "number",
|
|
int4: "number",
|
|
int8: "Int8",
|
|
json: "Json",
|
|
jsonb: "Json",
|
|
line: "string",
|
|
lseg: "string",
|
|
macaddr: "string",
|
|
money: "string",
|
|
numeric: "Numeric",
|
|
oid: "number",
|
|
path: "string",
|
|
polygon: "string",
|
|
text: "string",
|
|
time: "string",
|
|
timestamp: "Timestamp",
|
|
timestamptz: "Timestamp",
|
|
timetz: "string",
|
|
tsquery: "string",
|
|
tsvector: "string",
|
|
uuid: "string",
|
|
varbit: "string",
|
|
varchar: "string",
|
|
xml: "string",
|
|
}
|
|
|
|
protected override async introspectEnums(): Promise<IntrospectedEnums> {
|
|
const rows = (await this.database
|
|
.withoutPlugins()
|
|
.selectFrom("pg_type as type")
|
|
.innerJoin("pg_enum as enum", "type.oid", "enum.enumtypid")
|
|
.select(["type.typname as name", "enum.enumlabel as value"])
|
|
.execute()) as Array<{ name: string; value: string }>
|
|
const grouped = new Map<string, string[]>()
|
|
for (const { name, value } of rows) {
|
|
const existing = grouped.get(name)
|
|
if (existing == null) {
|
|
grouped.set(name, [value])
|
|
} else {
|
|
existing.push(value)
|
|
}
|
|
}
|
|
const named: EnumMetadata[] = []
|
|
for (const [name, values] of grouped) {
|
|
named.push({ name, values })
|
|
}
|
|
return { named, inline: new Map() }
|
|
}
|
|
}
|