Support per-dialect column types on @Column, e.g. dialectTypes: { postgres: 'jsonb' }
Feature Description
Column types declared in @Column({ type: ... }) are static — the same value is applied no matter which driver syncs or migrates the entity. But column types are not portable across databases:
jsonbdoes not exist on mysql/mariadb/sqlite (#11930)tinyintdoes not exist on postgres (ERROR: type "tinyint" does not existon the first sync)- MariaDB 10.7 has a native
UUIDtype with no entity-level way to pick it per connection (#8832) - sqlserver wants
nvarchar(max)where other drivers would usetext(#11665)
Today the workarounds are either dropping to the lowest common denominator type, or hand-writing migrations with if (queryRunner.connection.options.type === 'postgres') branches — after which synchronize no longer reflects what the database actually looks like.
The Solution
Extend ColumnOptions with a per-driver type override, keyed by the connection type:
@Column({ type: 'json', dialectTypes: { postgres: 'jsonb', mariadb: 'longtext' } })
payload: string;
@Column({ type: 'tinyint', dialectTypes: { postgres: 'smallint' } })
level: number;Semantics are the same model other ORMs already use:
- when driver
Xis active anddialectTypes[X]is set, resolve that physical type through the driver for validation, schema synchronization and SQL type declarations - otherwise fall back to
type, exactly as today - value conversion, hydration and transformers continue to use the logical column type; schema comparison, required PostgreSQL extensions and driver-specific parameter/return declarations use the physical type
Considered Alternatives
- Abstract types like
simple-json: portable but lossy — no access to native features such as jsonb indexing - Hand-written migrations with provider checks: works, but duplicates the schema outside the entity and breaks
synchronize ValueTransformer: transforms values, not the SQL column type
Additional Context
The same feature is a built-in elsewhere, so the design is well proven: ent has field.Float("amount").SchemaType(map[string]string{dialect.MySQL: "decimal(6,2)", dialect.Postgres: "numeric"}), SQLAlchemy has String(255).with_variant(VARCHAR(255, charset="utf8"), "mysql"), and a related GORM proposal for dialect-qualified type tags is still open (go-gorm/gorm#7867).
The implementation is being reviewed in #12884. It resolves supported type names, numeric length/precision parameters and driver-supported max lengths through the active driver. AI assistance was used for the implementation and this technical summary.
Relevant Database Driver(s)
- mysql
- postgres
- sqlite
- sqlserver
- cockroachdb
- better-sqlite3
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, and I know how to start.
Source: typeorm/typeorm