Documentation

Docs / Reference

ADR 0064 - TypeORM models and model payload version 5

Status: Accepted (delegated review, 2026-11-16)

TypeORM 0.3.x and 1.x entities as model-defined tables (docs/contract/claims/typeorm-models.md, plan the T018 task record (internal) "ORMs in the other first-tier languages"). Written with the change that widens the cached TypeScript model payload of ADR 0059 and ADR 0061 to version 5. Discovery, node shapes, row and assumption ids and the oracle are fixed by the task file's claim contract, the investigation results and Revisions 1-3 (with the TA2 and TA3 sections); steps TOB2-TOB5 implement them and add their implementation notes here.

Context

TypeORM states tables as decorated TypeScript classes: @Entity("x") on a class, @Column({...}), @PrimaryGeneratedColumn(), @ManyToOne(() => T, (t) => t.inverse, {...}), @JoinColumn, @JoinTable, @Index, @Unique and @Check on its instance properties and on the class. A column without an explicit type takes the design type the TypeScript compiler emits for the property's annotation (emitDecoratorMetadata), so the declared type is part of the input. The entities a data source creates are those its entities option reaches (class references or globs), and the options themselves are often chosen at run time: seerr's new DataSource(getDataSource()), whose if/else if/else returns one of five option objects through conditional expressions; NestJS apps pass them through TypeOrmModule.forRoot/forRootAsync factories, @nestjs/config registerAs namespaces, imported option consts and spreads of local objects (rows TO-K3 to TO-K8). Column defaults and enum columns name members of TypeScript enums declared in modules that import nothing from an ORM.

The cached model payload (version 4) keeps module-level declarations, statements, class headers and decorator counts, never decorator arguments or instance members, no declared types, no enums, no array initializers, no if with an else and no conditional expressions; its gate opens only for Drizzle and Sequelize files. As for ADR 0059 and 0061, the link pass has no tree on a cache hit (internal/index/index.go), and a second parse path outside the bounded workers is ruled out.

Options considered

  1. Read retained sources at link time. Rejected for the reasons of ADR 0059 and 0061: no tree on a cache hit, a second unbounded parse path.
  2. Reuse DeclarationSyntax / TemplateSyntax (the Angular class decorator caches, ungated). Rejected as the payload: they keep class decorators only, no members, no declared types, lossy values (property maps without order) and are not gated. (TemplateSyntax does keep NestJS @Module decorator arguments in every file, which the census uses for forFeature calls outside the gate; see Known gaps.)
  3. A separate TypeORM payload. Rejected as for Sequelize: the reduced tree, its validity and wrappers would be duplicated.
  4. Widen the model payload, tool-neutral and additive (chosen). Version 5 adds three gate rows, class decorator expressions, instance members with a reduced declared-type tree, enum declarations, array, string and conditional initializers, else branches and conditional expressions. Version 1-4 fields keep their meaning.

Decision

Payload version 5

ModelSyntaxVersion is 5 and Valid() rejects every other version, so version 4 entries miss once. The aggregate extractor version moves to extractors-37 (extractors-36 is the GORM Go model payload, ADR 0063; only living documents name it). The payload table in docs/lang/typescript-models.md names every capture branch; TestModelPayloadContract keeps code and table 1:1 over the drizzle-syntax, sequelize-syntax and new typeorm-syntax fixtures.

Gate rows, after G1-G9 (first matching row):

Capture additions:

Discovery, node shapes and rows

As fixed by the task file: package internal/extract/models/typeorm; packages depending on typeorm 0.3.x or 1.x (per-major rules, TO-V9; other majors a marker), pins from the lockfile; groups per module-level new DataSource(<options>) (candidate option objects through a selector function), ormconfig (0.3 only) and NestJS root modules (forRoot/forRootAsync, TO-K3, with TO-K5 registerAs, TO-K7 imported option consts and TO-K8 scope-neutral spreads); entities through the entities option (classes and the glob subset) and forFeature with autoLoadEntities (TO-K4); the dialect or dialect set (per-dialect claims through the model kit, Revision 1 O1); the tsconfig decorator and strictness flags (TO-V3), read from the shared resolver's tsconfig chain (typescript.ModuleResolver.CompilerFlags, added: the nearest config and its relative extends, a package extends unread); SnakeNamingStrategy as the only custom naming read (TO-K6). Records are model-defined table and column nodes (source: "model", tool: "typeorm", set: "models/typeorm/<data source file>") with contains and foreign_key edges in the T018c2 record shape and the T018g0 kit (claims per fact with verification: oracle for Postgres and SQLite forms the oracle compared, mapped for MySQL, MariaDB, MSSQL and the other SQLite-family drivers), so T018f's Schema view shows them without view changes. The claim contract (rows TO-K1 to TO-K8, E1-E3, C1-C6, X1-X4, R1-R4, P1, P2, U1, V and D; assumptions TO-V1 to TO-V10) is kept 1:1 with contract.go and docs/extract/schema-typeorm.md by the contract test.

Oracle

TypeORM's own DataSource.initialize() with synchronize: true and dropSchema: true on a scratch Postgres 17 and a scratch SQLite file (the entity modules transpiled with the corpus's typescript pin, side effects stubbed), each corpus with its own TypeORM pin, plus a migrations drift check that applies the corpus's migration classes to an empty database; TestTypeORMOracle compares every claimed fact with the catalog (zero disagreements). MySQL, MariaDB and MSSQL facts are mapped from the cited driver source.

Consequences

Known gaps

Implementation notes

Payload version 5

Relations, constraints and wiring

Oracle and TOB4 open points

Independent review fixes (Revision 4)

Decisions on the independent review (the T018g6 task record (internal), "Revision 4"); the status stays Proposed.

Review notes (delegated review, 2026-11-16)

Report a problem with this pageSource: docs/adr/0064-typeorm-models.md