Skip to content

Commit 2311288

Browse files
feat: add option to ignore enum values when generating pg types (#339)
1 parent cb1b783 commit 2311288

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

packages/pg-config/src/PgConfig.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,17 @@ export interface TypesConfig {
313313
* @default []
314314
*/
315315
ignoreTables: string[];
316+
317+
/**
318+
* Enum values to exclude from generated types.
319+
* This can be useful when you know you have no records
320+
* that use an enum value anymore, but cannot remove it
321+
* from the database type because the migration would be
322+
* too costly.
323+
*
324+
* @default {}
325+
*/
326+
ignoreEnumValues: Record<string, string[] | undefined>;
316327
}
317328

318329
export const TypesConfigSchema: ft.Runtype<TypesConfig> = ft
@@ -380,6 +391,11 @@ export const TypesConfigSchema: ft.Runtype<TypesConfig> = ft
380391
),
381392

382393
ignoreTables: withDefault<string[]>(ft.Array(ft.String), []),
394+
395+
ignoreEnumValues: withDefault<Record<string, string[] | undefined>>(
396+
ft.Record(ft.String, ft.Array(ft.String)),
397+
{},
398+
),
383399
})
384400
.withConstraint((value) => true, {name: `TypesConfig`});
385401

packages/pg-config/src/__tests__/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ test('get root config', () => {
1919
enumFileName: '_enums.ts',
2020
enumTypeMode: 'union_alias',
2121
enumTypeName: '{{ TYPE_NAME | pascal-case }}',
22+
ignoreEnumValues: {},
2223
ignoreTables: [],
2324
includeTables: null,
2425
primaryKeyFileName: '{{ TABLE_NAME }}.ts',
@@ -62,6 +63,7 @@ test('valid config', () => {
6263
enumFileName: '_enums.ts',
6364
enumTypeMode: 'union_alias',
6465
enumTypeName: '{{ TYPE_NAME | pascal-case }}',
66+
ignoreEnumValues: {},
6567
ignoreTables: [],
6668
includeTables: null,
6769
primaryKeyFileName: '{{ TABLE_NAME }}.ts',
@@ -102,6 +104,7 @@ test('valid config', () => {
102104
enumFileName: '_enums.ts',
103105
enumTypeMode: 'union_alias',
104106
enumTypeName: '{{ TYPE_NAME | pascal-case }}',
107+
ignoreEnumValues: {},
105108
ignoreTables: [],
106109
includeTables: null,
107110
primaryKeyFileName: '{{ TABLE_NAME }}.ts',

packages/pg-schema-print-types/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PgConfig from '@databases/pg-config';
22
import PgDataTypeID from '@databases/pg-data-type-id';
3-
import type {Schema} from '@databases/pg-schema-introspect';
3+
import {TypeKind, type Schema} from '@databases/pg-schema-introspect';
44
import {getIgnoreTest, writeFiles} from '@databases/shared-print-types';
55
import PgPrintContext from './PgPrintContext';
66
import getTypeScriptType from './getTypeScriptType';
@@ -21,6 +21,15 @@ function filterSchema(unfilteredSchema: Schema, options: Options): Schema {
2121
);
2222
return {
2323
...unfilteredSchema,
24+
types: unfilteredSchema.types.map((t) => {
25+
if (t.kind !== TypeKind.Enum) return t;
26+
const ignoredValues = options.ignoreEnumValues?.[t.typeName];
27+
if (!ignoredValues?.length) return t;
28+
return {
29+
...t,
30+
values: t.values.filter((v) => !ignoredValues.includes(v)),
31+
};
32+
}),
2433
classes: unfilteredSchema.classes
2534
.filter((c) => !ignoredClassIds.has(c.classID))
2635
.map((c) => ({

0 commit comments

Comments
 (0)