diff --git a/src/lib/file-generators/custom/[schema_name]/[TableName]CustomTypes.ts b/src/lib/file-generators/custom/[schema_name]/[TableName]CustomTypes.ts index 5103a21..38df57e 100644 --- a/src/lib/file-generators/custom/[schema_name]/[TableName]CustomTypes.ts +++ b/src/lib/file-generators/custom/[schema_name]/[TableName]CustomTypes.ts @@ -34,10 +34,33 @@ export const createCustomTypesFile = ( moduleSpecifier: `../../generated/${schema.name}/${pascal_table_name}QueryTypes`, isTypeOnly: true, }, + ] + + if (table.use_generic_provider) { + statements.push({ + kind: StructureKind.ImportDeclaration, + moduleSpecifier: 'lib/spi/provider-model-types', + namedImports: ['ProviderTypes'], + isTypeOnly: true, + }) + } + + statements.push( (writer) => writer.blankLine(), { kind: StructureKind.TypeAlias, name: `${pascal_table_name}CustomTypes`, + ...(table.use_generic_provider + ? { + typeParameters: [ + { + name: 'T', + constraint: 'ProviderTypes', + default: 'ProviderTypes', + }, + ], + } + : {}), type: `CreateCustomTypes`, isExported: true, }, @@ -47,7 +70,7 @@ export const createCustomTypesFile = ( isExportEquals: false, expression: `${pascal_table_name}CustomTypes`, }, - ] + ) return project.createSourceFile( `${output_dir}/custom/${schema.name}/${pascal_table_name}CustomTypes.ts`, diff --git a/src/lib/file-generators/generated/[schema_name]/[TableName].ts b/src/lib/file-generators/generated/[schema_name]/[TableName].ts index 45029c8..6523d29 100644 --- a/src/lib/file-generators/generated/[schema_name]/[TableName].ts +++ b/src/lib/file-generators/generated/[schema_name]/[TableName].ts @@ -57,6 +57,14 @@ export const createResultingTypeFile = ( isTypeOnly: true, }, ) + if (table.use_generic_provider) { + statements.push({ + kind: StructureKind.ImportDeclaration, + moduleSpecifier: 'lib/spi/provider-model-types', + namedImports: ['ProviderTypes'], + isTypeOnly: true, + }) + } } statements.push( @@ -65,16 +73,38 @@ export const createResultingTypeFile = ( kind: StructureKind.TypeAlias, isExported: true, name: pascal_table_name, + ...(table.use_generic_provider + ? { + typeParameters: [ + { + name: 'T', + constraint: 'ProviderTypes', + default: 'ProviderTypes', + }, + ], + } + : {}), type: table.is_customizable - ? `CustomizeDbType` + ? `CustomizeDbType' : ''}>` : `Selectable${pascal_table_name}`, }, { kind: StructureKind.TypeAlias, isExported: true, name: `${pascal_table_name}Initializer`, + ...(table.use_generic_provider + ? { + typeParameters: [ + { + name: 'T', + constraint: 'ProviderTypes', + default: 'ProviderTypes', + }, + ], + } + : {}), type: table.is_customizable - ? `CustomizeDbTypeInitializer` + ? `CustomizeDbTypeInitializer' : ''}>` : `Insertable${pascal_table_name}`, }, ) @@ -84,7 +114,18 @@ export const createResultingTypeFile = ( kind: StructureKind.TypeAlias, isExported: true, name: `${pascal_table_name}WithPgtuiBugs`, - type: `ReproducePgtuiBugs<${pascal_table_name}>`, + ...(table.use_generic_provider + ? { + typeParameters: [ + { + name: 'T', + constraint: 'ProviderTypes', + default: 'ProviderTypes', + }, + ], + } + : {}), + type: `ReproducePgtuiBugs<${pascal_table_name}${table.use_generic_provider ? '' : ''}>`, 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.`, @@ -95,7 +136,18 @@ export const createResultingTypeFile = ( kind: StructureKind.TypeAlias, isExported: true, name: `${pascal_table_name}InitializerWithPgtuiBugs`, - type: `ReproducePgtuiBugs<${pascal_table_name}Initializer>`, + ...(table.use_generic_provider + ? { + typeParameters: [ + { + name: 'T', + constraint: 'ProviderTypes', + default: 'ProviderTypes', + }, + ], + } + : {}), + type: `ReproducePgtuiBugs<${pascal_table_name}Initializer${table.use_generic_provider ? '' : ''}>`, 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.`, diff --git a/src/lib/read-database-tree.ts b/src/lib/read-database-tree.ts index b50a17b..7527918 100644 --- a/src/lib/read-database-tree.ts +++ b/src/lib/read-database-tree.ts @@ -58,6 +58,7 @@ export const readDatabaseTree = (config: Config): DatabaseTree => { is_customizable, is_affected_by_pgtui_bugs, uses_zapatos_column_type: false, + use_generic_provider: false, } table.selectable_columns = getColumns( @@ -70,6 +71,12 @@ export const readDatabaseTree = (config: Config): DatabaseTree => { table, zapatos_custom_types, ) + if (config.is_generic_provider_enabled === true) { + table.use_generic_provider = Object.prototype.hasOwnProperty.call( + table.selectable_columns, + 'provider_state', + ) + } tables[table_name] = table } diff --git a/src/lib/types.ts b/src/lib/types.ts index 9aa9a9b..369c2a2 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -6,6 +6,7 @@ export interface Config { generate_knex_types?: boolean main_schema?: string file_header?: string + is_generic_provider_enabled?: boolean } export interface DatabaseTree { @@ -24,6 +25,7 @@ export interface Table { is_customizable: boolean is_affected_by_pgtui_bugs: boolean uses_zapatos_column_type: boolean + use_generic_provider: boolean } export interface Column {