Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/kysely-tailordb-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
"@tailor-platform/function-kysely-tailordb": "0.1.2",
"@tailor-platform/function-types": "0.3.0",
"@types/fs-extra": "11.0.4",
"@types/node": "22.15.33",
"esbuild": "0.25.5",
"kysely": "0.27.6",
"kysely": "0.28.2",
"kysely-codegen": "0.18.5",
"typescript": "5.8.3",
"unenv": "1.10.0"
Expand Down
57 changes: 22 additions & 35 deletions packages/kysely-tailordb-codegen/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 64 additions & 7 deletions packages/kysely-tailordb-codegen/src/function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TailordbDialect } from "@tailor-platform/function-kysely-tailordb";
import { Kysely } from "kysely";
import type { TableMetadata } from "kysely";
import {
type PostgresDB,
EnumCollection,
PostgresDialect,
TypeScriptSerializer,
} from "kysely-codegen";
Expand All @@ -10,11 +9,69 @@ export default async (args: { namespace: string }) => {
const client = new tailordb.Client({
namespace: args.namespace,
});
const db = new Kysely<PostgresDB>({
dialect: new TailordbDialect(client),
const columns = (
await client.queryObject<RawColumnMetadata>(
// To avoid unstable dependencies on SQL generated by Kysely, issue a SELECT statement to the special metadata table `kysely_column_metadata`.
// Tailor Platform must return results corresponding to `RawColumnMetadata` for this SQL.
"SELECT * FROM kysely_column_metadata;",
)
).rows;
const tables = parseTableMetadata(columns);
const dialect = new PostgresDialect();
const metadata = dialect.introspector.createDatabaseMetadata({
tables,
domains: [],
partitions: [],
enums: new EnumCollection(),
});
const dialect = new PostgresDialect({ domains: false });
const metadata = await dialect.introspector.introspect({ db });
const data = new TypeScriptSerializer().serializeFile(metadata, dialect);
return { data };
};

// Since Kysely's parseTableMetadata is not exported, it is copied below.
// https://github.com/kysely-org/kysely/blob/0.28.2/src/dialect/postgres/postgres-introspector.ts#L103
const parseTableMetadata = (columns: RawColumnMetadata[]): TableMetadata[] => {
return columns.reduce<TableMetadata[]>((tables, it) => {
let table = tables.find(
(tbl) => tbl.name === it.table && tbl.schema === it.schema,
);

if (!table) {
table = Object.freeze({
name: it.table,
isView: it.table_type === "v",
schema: it.schema,
columns: [],
});

tables.push(table);
}

table.columns.push(
Object.freeze({
name: it.column,
dataType: it.type,
dataTypeSchema: it.type_schema,
isNullable: !it.not_null,
isAutoIncrementing: !!it.auto_incrementing,
hasDefaultValue: it.has_default,
comment: it.column_description ?? undefined,
}),
);

return tables;
}, []);
};

interface RawColumnMetadata {
column: string;
table: string;
table_type: string;
schema: string;
not_null: boolean;
has_default: boolean;
type: string;
type_schema: string;
auto_incrementing: boolean | null;
column_description: string | null;
}