Skip to content

Commit 1f7c331

Browse files
authored
chore: use kysely_column_metadata (#52)
1 parent 888a77f commit 1f7c331

3 files changed

Lines changed: 87 additions & 44 deletions

File tree

packages/kysely-tailordb-codegen/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
},
2525
"devDependencies": {
2626
"@biomejs/biome": "1.9.4",
27-
"@tailor-platform/function-kysely-tailordb": "0.1.2",
2827
"@tailor-platform/function-types": "0.3.0",
2928
"@types/fs-extra": "11.0.4",
3029
"@types/node": "22.15.33",
3130
"esbuild": "0.25.5",
32-
"kysely": "0.27.6",
31+
"kysely": "0.28.2",
3332
"kysely-codegen": "0.18.5",
3433
"typescript": "5.8.3",
3534
"unenv": "1.10.0"

packages/kysely-tailordb-codegen/pnpm-lock.yaml

Lines changed: 22 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { TailordbDialect } from "@tailor-platform/function-kysely-tailordb";
2-
import { Kysely } from "kysely";
1+
import type { TableMetadata } from "kysely";
32
import {
4-
type PostgresDB,
3+
EnumCollection,
54
PostgresDialect,
65
TypeScriptSerializer,
76
} from "kysely-codegen";
@@ -10,11 +9,69 @@ export default async (args: { namespace: string }) => {
109
const client = new tailordb.Client({
1110
namespace: args.namespace,
1211
});
13-
const db = new Kysely<PostgresDB>({
14-
dialect: new TailordbDialect(client),
12+
const columns = (
13+
await client.queryObject<RawColumnMetadata>(
14+
// To avoid unstable dependencies on SQL generated by Kysely, issue a SELECT statement to the special metadata table `kysely_column_metadata`.
15+
// Tailor Platform must return results corresponding to `RawColumnMetadata` for this SQL.
16+
"SELECT * FROM kysely_column_metadata;",
17+
)
18+
).rows;
19+
const tables = parseTableMetadata(columns);
20+
const dialect = new PostgresDialect();
21+
const metadata = dialect.introspector.createDatabaseMetadata({
22+
tables,
23+
domains: [],
24+
partitions: [],
25+
enums: new EnumCollection(),
1526
});
16-
const dialect = new PostgresDialect({ domains: false });
17-
const metadata = await dialect.introspector.introspect({ db });
1827
const data = new TypeScriptSerializer().serializeFile(metadata, dialect);
1928
return { data };
2029
};
30+
31+
// Since Kysely's parseTableMetadata is not exported, it is copied below.
32+
// https://github.com/kysely-org/kysely/blob/0.28.2/src/dialect/postgres/postgres-introspector.ts#L103
33+
const parseTableMetadata = (columns: RawColumnMetadata[]): TableMetadata[] => {
34+
return columns.reduce<TableMetadata[]>((tables, it) => {
35+
let table = tables.find(
36+
(tbl) => tbl.name === it.table && tbl.schema === it.schema,
37+
);
38+
39+
if (!table) {
40+
table = Object.freeze({
41+
name: it.table,
42+
isView: it.table_type === "v",
43+
schema: it.schema,
44+
columns: [],
45+
});
46+
47+
tables.push(table);
48+
}
49+
50+
table.columns.push(
51+
Object.freeze({
52+
name: it.column,
53+
dataType: it.type,
54+
dataTypeSchema: it.type_schema,
55+
isNullable: !it.not_null,
56+
isAutoIncrementing: !!it.auto_incrementing,
57+
hasDefaultValue: it.has_default,
58+
comment: it.column_description ?? undefined,
59+
}),
60+
);
61+
62+
return tables;
63+
}, []);
64+
};
65+
66+
interface RawColumnMetadata {
67+
column: string;
68+
table: string;
69+
table_type: string;
70+
schema: string;
71+
not_null: boolean;
72+
has_default: boolean;
73+
type: string;
74+
type_schema: string;
75+
auto_incrementing: boolean | null;
76+
column_description: string | null;
77+
}

0 commit comments

Comments
 (0)