Skip to content

Commit e345047

Browse files
committed
fix(bindx-form): use Contember ColumnType enum instead of PostgreSQL type strings
Generator now emits normalized ColumnType values ('String', 'Integer', 'Double', etc.) instead of raw PostgreSQL types ('text', 'double precision', 'boolean'). This removes the need for the columnTypeAliases map in useFormInputHandler and prevents the class of bugs where a PostgreSQL alias was missing.
1 parent b5eabc6 commit e345047

4 files changed

Lines changed: 8 additions & 22 deletions

File tree

packages/bindx-client/src/schema/SchemaRegistry.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class SchemaRegistry<TModels extends Record<string, object> = Record<stri
3838

3939
for (const [fieldName, field] of entity.fields) {
4040
if (field.__typename === '_Column') {
41-
fields[fieldName] = { type: 'scalar' }
41+
fields[fieldName] = { type: 'scalar', columnType: field.type }
4242
} else if (field.__typename === '_Relation') {
4343
const inverse = field.side === 'owning' ? field.inversedBy ?? undefined : field.ownedBy
4444
const isMany = field.type === 'OneHasMany' || field.type === 'ManyHasMany'
@@ -281,6 +281,8 @@ interface RawContemberFieldDef {
281281
readonly entity?: string
282282
readonly target?: string
283283
readonly inverse?: string
284+
readonly columnType?: string
285+
readonly enumName?: string
284286
}
285287

286288
/**
@@ -295,7 +297,7 @@ function normalizeEntityDef(entityDef: EntitySchemaDef): EntitySchemaDef {
295297
const raw = fieldDef as RawContemberFieldDef
296298

297299
if (raw.type === 'column') {
298-
fields[fieldName] = { type: 'scalar' }
300+
fields[fieldName] = { type: 'scalar', columnType: raw.columnType }
299301
needsNormalization = true
300302
} else if (raw.type === 'one') {
301303
fields[fieldName] = {

packages/bindx-form/src/hooks/useFormInputHandler.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,25 +148,9 @@ const defaultTypeHandlers: Partial<Record<ColumnType, HandlerFactory>> = {
148148
Enum: createStringHandler,
149149
}
150150

151-
/**
152-
* Maps Contember DB column types (lowercase) to ColumnType (PascalCase).
153-
* Handles both formats so the handler works with schema-provided types.
154-
*/
155-
const columnTypeAliases: Record<string, ColumnType> = {
156-
text: 'String',
157-
integer: 'Integer',
158-
double: 'Double',
159-
date: 'Date',
160-
timestamptz: 'DateTime',
161-
time: 'Time',
162-
bool: 'Bool',
163-
uuid: 'Uuid',
164-
jsonb: 'Json',
165-
}
166-
167151
function resolveColumnType(columnType: string | undefined): ColumnType | undefined {
168152
if (!columnType) return undefined
169-
return (columnTypeAliases[columnType] ?? columnType) as ColumnType
153+
return columnType as ColumnType
170154
}
171155

172156
/**

packages/bindx-generator/src/NameSchemaGenerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export class NameSchemaGenerator {
5454
visitColumn: ctx => {
5555
scalars.push(ctx.column.name)
5656
fields[ctx.column.name] = ctx.column.type === Model.ColumnType.Enum
57-
? { type: 'column', columnType: ctx.column.columnType, enumName: ctx.column.columnType }
58-
: { type: 'column', columnType: ctx.column.columnType }
57+
? { type: 'column', columnType: ctx.column.type, enumName: ctx.column.columnType }
58+
: { type: 'column', columnType: ctx.column.type }
5959
},
6060
})
6161

packages/bindx-generator/tests/entityGenerator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('NameSchemaGenerator', () => {
1717

1818
expect(schema.entities['Post']?.fields['author']).toEqual({ type: 'one', entity: 'Author', nullable: false })
1919
expect(schema.entities['Post']?.fields['tags']).toEqual({ type: 'many', entity: 'Tag', relationKind: 'manyHasMany', nullable: undefined })
20-
expect(schema.entities['Post']?.fields['title']).toEqual({ type: 'column', columnType: 'text' })
20+
expect(schema.entities['Post']?.fields['title']).toEqual({ type: 'column', columnType: 'String' })
2121
expect(schema.enums['PostStatus']).toEqual(['draft', 'published', 'archived'])
2222
})
2323
})

0 commit comments

Comments
 (0)