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
11 changes: 11 additions & 0 deletions src/lib/file-generators/generated/[schema_name]/[TableName].ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ export const createResultingTypeFile = (
},
],
})
statements.push({
kind: StructureKind.TypeAlias,
isExported: true,
name: `${pascal_table_name}InitializerWithPgtuiBugs`,
type: `ReproducePgtuiBugs<${pascal_table_name}Initializer>`,
docs: [
{
description: `@deprecated Reproduces type bugs from the legacy \`pgtui\` library and should not be used in new code.\n\nSpecifically:\n- Columns ending in \`_id\` are incorrectly typed as \`string\`, regardless of their actual database type.\n- \`jsonb\` columns are typed as \`any\` instead of a more specific type.`,
},
],
})
}

return project.createSourceFile(
Expand Down
19 changes: 14 additions & 5 deletions src/lib/file-generators/generated/[schema_name]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const createSchemaGeneratedTypeIndexFile = (
{
kind: StructureKind.ImportDeclaration,
moduleSpecifier: '../utils',
namedImports: ['KyselyTable'],
namedImports: ['KyselyTable', 'KnexTable'],
isTypeOnly: true,
},
]
Expand All @@ -47,7 +47,10 @@ export const createSchemaGeneratedTypeIndexFile = (
namedImports: [
pascal_table_name,
...(table.is_affected_by_pgtui_bugs
? [`${pascal_table_name}WithPgtuiBugs`]
? [
`${pascal_table_name}WithPgtuiBugs`,
`${pascal_table_name}InitializerWithPgtuiBugs`,
]
: []),
`${pascal_table_name}Initializer`,
],
Expand Down Expand Up @@ -83,9 +86,15 @@ export const createSchemaGeneratedTypeIndexFile = (
schema.name === main_schema
? table.name
: `"${schema.name}.${table.name}"`,
type: table.is_affected_by_pgtui_bugs
? `${pascal_table_names[table.name]}WithPgtuiBugs`
: (pascal_table_names[table.name] ?? ''),
type: `KnexTable<${
table.is_affected_by_pgtui_bugs
? `${pascal_table_names[table.name]}WithPgtuiBugs`
: pascal_table_names[table.name]
}, ${
table.is_affected_by_pgtui_bugs
? `${pascal_table_names[table.name]}InitializerWithPgtuiBugs`
: `${pascal_table_names[table.name]}Initializer`
}>`,
})),
})
}
Expand Down
31 changes: 27 additions & 4 deletions src/lib/file-generators/generated/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const createGeneratedUtilsFile = (

const file_source = `import type { JSONValue } from "zapatos/db"
import { type ColumnType } from "kysely"
import { Knex } from "knex"

type PartialWithNever<T> = {
[P in keyof T as T[P] extends never ? never : P]?: T[P]
Expand All @@ -30,6 +31,16 @@ export type KyselyTable<Selectable, Initializer> = {
>
}

type KnexInsertableTable<T> = {
[K in keyof T]: Knex.MaybeRawColumn<T[K]>
}

export type KnexTable<Selectable, Initializer> = Knex.CompositeTableType<
Selectable,
Partial<KnexInsertableTable<Initializer>>, // TODO remove \`Partial\` once we fix the code
Partial<KnexInsertableTable<Initializer>>
>

type KeysOfUnion<T> = T extends T ? keyof T : never

type NullableKeys<T> = {
Expand All @@ -56,8 +67,17 @@ export type CreateCustomTypes<
> = Sub

// Replaces the values of From with the values of To and ignores the rest of the properties of To
type ReplaceValueTypes<From, To> = Omit<From, keyof To> &
Pick<To, Extract<keyof To, keyof From>>
type ReplaceValueTypes<From, To> = {
[K in keyof From]: K extends keyof To ? To[K] : From[K]
}

type AllowJsonStringify<OriginalInsertable, CustomizedInsertable> = {
[K in keyof OriginalInsertable]: K extends keyof CustomizedInsertable
? [JSONValue] extends [OriginalInsertable[K]]
? CustomizedInsertable[K] | string
: CustomizedInsertable[K]
: never
}

// Works to make From properties optional but it would not work to make them mandatory as From[K] would include undefined
// TODO improve to keep unions (if possible)
Expand All @@ -75,9 +95,12 @@ export type CustomizeDbType<DbType, CustomProperties> = ReplaceValueTypes<
>

export type CustomizeDbTypeInitializer<DbTypeInitializer, CustomProperties> =
ReplaceValueTypes<
AllowJsonStringify<
DbTypeInitializer,
ReplaceKeyTypes<CustomProperties, DbTypeInitializer>
ReplaceValueTypes<
DbTypeInitializer,
ReplaceKeyTypes<CustomProperties, DbTypeInitializer>
>
>

// TODO Migrating from pgtui to our custom types is a pain because of two bugs in pgtui.
Expand Down