Skip to content

Commit 8e533e5

Browse files
authored
feat: Add knex insertable and updatable (#7)
1 parent c4ff986 commit 8e533e5

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

src/lib/file-generators/generated/[schema_name]/[TableName].ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ export const createResultingTypeFile = (
9191
},
9292
],
9393
})
94+
statements.push({
95+
kind: StructureKind.TypeAlias,
96+
isExported: true,
97+
name: `${pascal_table_name}InitializerWithPgtuiBugs`,
98+
type: `ReproducePgtuiBugs<${pascal_table_name}Initializer>`,
99+
docs: [
100+
{
101+
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.`,
102+
},
103+
],
104+
})
94105
}
95106

96107
return project.createSourceFile(

src/lib/file-generators/generated/[schema_name]/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const createSchemaGeneratedTypeIndexFile = (
3232
{
3333
kind: StructureKind.ImportDeclaration,
3434
moduleSpecifier: '../utils',
35-
namedImports: ['KyselyTable'],
35+
namedImports: ['KyselyTable', 'KnexTable'],
3636
isTypeOnly: true,
3737
},
3838
]
@@ -47,7 +47,10 @@ export const createSchemaGeneratedTypeIndexFile = (
4747
namedImports: [
4848
pascal_table_name,
4949
...(table.is_affected_by_pgtui_bugs
50-
? [`${pascal_table_name}WithPgtuiBugs`]
50+
? [
51+
`${pascal_table_name}WithPgtuiBugs`,
52+
`${pascal_table_name}InitializerWithPgtuiBugs`,
53+
]
5154
: []),
5255
`${pascal_table_name}Initializer`,
5356
],
@@ -83,9 +86,15 @@ export const createSchemaGeneratedTypeIndexFile = (
8386
schema.name === main_schema
8487
? table.name
8588
: `"${schema.name}.${table.name}"`,
86-
type: table.is_affected_by_pgtui_bugs
87-
? `${pascal_table_names[table.name]}WithPgtuiBugs`
88-
: (pascal_table_names[table.name] ?? ''),
89+
type: `KnexTable<${
90+
table.is_affected_by_pgtui_bugs
91+
? `${pascal_table_names[table.name]}WithPgtuiBugs`
92+
: pascal_table_names[table.name]
93+
}, ${
94+
table.is_affected_by_pgtui_bugs
95+
? `${pascal_table_names[table.name]}InitializerWithPgtuiBugs`
96+
: `${pascal_table_names[table.name]}Initializer`
97+
}>`,
8998
})),
9099
})
91100
}

src/lib/file-generators/generated/utils.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const createGeneratedUtilsFile = (
1313

1414
const file_source = `import type { JSONValue } from "zapatos/db"
1515
import { type ColumnType } from "kysely"
16+
import { Knex } from "knex"
1617
1718
type PartialWithNever<T> = {
1819
[P in keyof T as T[P] extends never ? never : P]?: T[P]
@@ -30,6 +31,16 @@ export type KyselyTable<Selectable, Initializer> = {
3031
>
3132
}
3233
34+
type KnexInsertableTable<T> = {
35+
[K in keyof T]: Knex.MaybeRawColumn<T[K]>
36+
}
37+
38+
export type KnexTable<Selectable, Initializer> = Knex.CompositeTableType<
39+
Selectable,
40+
Partial<KnexInsertableTable<Initializer>>, // TODO remove \`Partial\` once we fix the code
41+
Partial<KnexInsertableTable<Initializer>>
42+
>
43+
3344
type KeysOfUnion<T> = T extends T ? keyof T : never
3445
3546
type NullableKeys<T> = {
@@ -56,8 +67,17 @@ export type CreateCustomTypes<
5667
> = Sub
5768
5869
// Replaces the values of From with the values of To and ignores the rest of the properties of To
59-
type ReplaceValueTypes<From, To> = Omit<From, keyof To> &
60-
Pick<To, Extract<keyof To, keyof From>>
70+
type ReplaceValueTypes<From, To> = {
71+
[K in keyof From]: K extends keyof To ? To[K] : From[K]
72+
}
73+
74+
type AllowJsonStringify<OriginalInsertable, CustomizedInsertable> = {
75+
[K in keyof OriginalInsertable]: K extends keyof CustomizedInsertable
76+
? [JSONValue] extends [OriginalInsertable[K]]
77+
? CustomizedInsertable[K] | string
78+
: CustomizedInsertable[K]
79+
: never
80+
}
6181
6282
// Works to make From properties optional but it would not work to make them mandatory as From[K] would include undefined
6383
// TODO improve to keep unions (if possible)
@@ -75,9 +95,12 @@ export type CustomizeDbType<DbType, CustomProperties> = ReplaceValueTypes<
7595
>
7696
7797
export type CustomizeDbTypeInitializer<DbTypeInitializer, CustomProperties> =
78-
ReplaceValueTypes<
98+
AllowJsonStringify<
7999
DbTypeInitializer,
80-
ReplaceKeyTypes<CustomProperties, DbTypeInitializer>
100+
ReplaceValueTypes<
101+
DbTypeInitializer,
102+
ReplaceKeyTypes<CustomProperties, DbTypeInitializer>
103+
>
81104
>
82105
83106
// TODO Migrating from pgtui to our custom types is a pain because of two bugs in pgtui.

0 commit comments

Comments
 (0)