Skip to content

Commit ddc25bf

Browse files
committed
feat: Add option to add generic provider types
1 parent 62a989e commit ddc25bf

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,33 @@ export const createCustomTypesFile = (
3434
moduleSpecifier: `../../generated/${schema.name}/${pascal_table_name}QueryTypes`,
3535
isTypeOnly: true,
3636
},
37+
]
38+
39+
if (table.use_generic_provider) {
40+
statements.push({
41+
kind: StructureKind.ImportDeclaration,
42+
moduleSpecifier: 'lib/spi/provider-model-types',
43+
namedImports: ['ProviderTypes'],
44+
isTypeOnly: true,
45+
})
46+
}
47+
48+
statements.push(
3749
(writer) => writer.blankLine(),
3850
{
3951
kind: StructureKind.TypeAlias,
4052
name: `${pascal_table_name}CustomTypes`,
53+
...(table.use_generic_provider
54+
? {
55+
typeParameters: [
56+
{
57+
name: 'T',
58+
constraint: 'ProviderTypes',
59+
default: 'ProviderTypes',
60+
},
61+
],
62+
}
63+
: {}),
4164
type: `CreateCustomTypes<Selectable${pascal_table_name}, {}>`,
4265
isExported: true,
4366
},
@@ -47,7 +70,7 @@ export const createCustomTypesFile = (
4770
isExportEquals: false,
4871
expression: `${pascal_table_name}CustomTypes`,
4972
},
50-
]
73+
)
5174

5275
return project.createSourceFile(
5376
`${output_dir}/custom/${schema.name}/${pascal_table_name}CustomTypes.ts`,

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ export const createResultingTypeFile = (
5757
isTypeOnly: true,
5858
},
5959
)
60+
if (table.use_generic_provider) {
61+
statements.push({
62+
kind: StructureKind.ImportDeclaration,
63+
moduleSpecifier: 'lib/spi/provider-model-types',
64+
namedImports: ['ProviderTypes'],
65+
isTypeOnly: true,
66+
})
67+
}
6068
}
6169

6270
statements.push(
@@ -65,16 +73,38 @@ export const createResultingTypeFile = (
6573
kind: StructureKind.TypeAlias,
6674
isExported: true,
6775
name: pascal_table_name,
76+
...(table.use_generic_provider
77+
? {
78+
typeParameters: [
79+
{
80+
name: 'T',
81+
constraint: 'ProviderTypes',
82+
default: 'ProviderTypes',
83+
},
84+
],
85+
}
86+
: {}),
6887
type: table.is_customizable
69-
? `CustomizeDbType<Selectable${pascal_table_name}, ${pascal_table_name}CustomTypes>`
88+
? `CustomizeDbType<Selectable${pascal_table_name}, ${pascal_table_name}CustomTypes${table.use_generic_provider ? '<T>' : ''}>`
7089
: `Selectable${pascal_table_name}`,
7190
},
7291
{
7392
kind: StructureKind.TypeAlias,
7493
isExported: true,
7594
name: `${pascal_table_name}Initializer`,
95+
...(table.use_generic_provider
96+
? {
97+
typeParameters: [
98+
{
99+
name: 'T',
100+
constraint: 'ProviderTypes',
101+
default: 'ProviderTypes',
102+
},
103+
],
104+
}
105+
: {}),
76106
type: table.is_customizable
77-
? `CustomizeDbTypeInitializer<Insertable${pascal_table_name}, ${pascal_table_name}CustomTypes>`
107+
? `CustomizeDbTypeInitializer<Insertable${pascal_table_name}, ${pascal_table_name}CustomTypes${table.use_generic_provider ? '<T>' : ''}>`
78108
: `Insertable${pascal_table_name}`,
79109
},
80110
)
@@ -84,7 +114,18 @@ export const createResultingTypeFile = (
84114
kind: StructureKind.TypeAlias,
85115
isExported: true,
86116
name: `${pascal_table_name}WithPgtuiBugs`,
87-
type: `ReproducePgtuiBugs<${pascal_table_name}>`,
117+
...(table.use_generic_provider
118+
? {
119+
typeParameters: [
120+
{
121+
name: 'T',
122+
constraint: 'ProviderTypes',
123+
default: 'ProviderTypes',
124+
},
125+
],
126+
}
127+
: {}),
128+
type: `ReproducePgtuiBugs<${pascal_table_name}${table.use_generic_provider ? '<T>' : ''}>`,
88129
docs: [
89130
{
90131
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.`,
@@ -95,7 +136,18 @@ export const createResultingTypeFile = (
95136
kind: StructureKind.TypeAlias,
96137
isExported: true,
97138
name: `${pascal_table_name}InitializerWithPgtuiBugs`,
98-
type: `ReproducePgtuiBugs<${pascal_table_name}Initializer>`,
139+
...(table.use_generic_provider
140+
? {
141+
typeParameters: [
142+
{
143+
name: 'T',
144+
constraint: 'ProviderTypes',
145+
default: 'ProviderTypes',
146+
},
147+
],
148+
}
149+
: {}),
150+
type: `ReproducePgtuiBugs<${pascal_table_name}Initializer${table.use_generic_provider ? '<T>' : ''}>`,
99151
docs: [
100152
{
101153
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.`,

src/lib/read-database-tree.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const readDatabaseTree = (config: Config): DatabaseTree => {
5858
is_customizable,
5959
is_affected_by_pgtui_bugs,
6060
uses_zapatos_column_type: false,
61+
use_generic_provider: false,
6162
}
6263

6364
table.selectable_columns = getColumns(
@@ -70,6 +71,12 @@ export const readDatabaseTree = (config: Config): DatabaseTree => {
7071
table,
7172
zapatos_custom_types,
7273
)
74+
if (config.is_generic_provider_enabled === true) {
75+
table.use_generic_provider = Object.prototype.hasOwnProperty.call(
76+
table.selectable_columns,
77+
'provider_state',
78+
)
79+
}
7380

7481
tables[table_name] = table
7582
}

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface Config {
66
generate_knex_types?: boolean
77
main_schema?: string
88
file_header?: string
9+
is_generic_provider_enabled?: boolean
910
}
1011

1112
export interface DatabaseTree {
@@ -24,6 +25,7 @@ export interface Table {
2425
is_customizable: boolean
2526
is_affected_by_pgtui_bugs: boolean
2627
uses_zapatos_column_type: boolean
28+
use_generic_provider: boolean
2729
}
2830

2931
export interface Column {

0 commit comments

Comments
 (0)