Documentation

Docs / Reference

ADR 0061 - Sequelize models and model payload version 2

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

Sequelize v6 models (docs/contract/claims/sequelize-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 to version 2 (gate, statement and expression kinds, validity). Discovery, node shapes, row and assumption ids and the oracle are fixed by the task file's claim contract, the investigation results and Revision 1; steps SQB2-SQB5 implement them and add their implementation notes here.

Context

Sequelize states tables in TypeScript or JavaScript, ESM or CommonJS: class X extends Model with one module-level X.init({attributes}, {sequelize, ...options}) (opencollective-api, 52 models), or sequelize.define("name", {attributes}, {options}) inside a CommonJS factory module.exports = (sequelize, DataTypes) => {...} that an index module requires and calls with the instance (Gladys, 32 models). Associations are module-level calls (A.belongsTo(B, {...}), 197 in opencollective-api's models/index.ts) or calls inside x.associate = (models) => {...} run by a loop in the index module (53 in Gladys). Gladys's instance options come from require("../utils/getConfig")(), which returns require("../config/config")[NODE_ENV] (open point O2, accepted in Revision 1): the global define (underscored, freezeTableName, the timestamp names) decides every table and column name.

As for Drizzle (ADR 0059), 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. The version 1 model payload keeps only module-level const/let/var declarations whose initializer is a call, tagged template or object literal, and the default export, and opens only for Drizzle files (G1-G3). Sequelize needs, beyond that (task file, "Payload decision", cache table): module-level expression statements (X.init(...), A.belongsTo(...), module.exports = e, x.associate = e), class headers (extends), new Sequelize(...), destructured requires (const { DataTypes } = require("sequelize")), function bodies as statement lists (factories, associate), and files that import nothing from Sequelize (factories; Gladys's options and environment modules).

Options considered

  1. A separate Sequelize payload. A second gated cache field with its own tree types. Rejected: the reduced tree, its validity rules and its wrappers would be duplicated; Drizzle and Sequelize read the same JavaScript syntax.
  2. Widen the model payload, tool-neutral, additive (chosen). Version 2 adds gate rows, a module-level statement list and new expression kinds; version 1 fields keep their meaning, so the Drizzle linker reads the same values (its kind switches send the new kinds to the branches that version 1's unknown reached).
  3. Capture every JavaScript file's CommonJS exports. Would cover any options module reached by require, but Gladys alone has hundreds of CommonJS modules whose exports are large function bodies. Rejected for the two narrow syntactic rows G7 and G8 below.
  4. Gate by the files a gated file requires. A file's payload would depend on other files, which breaks caching by content hash (AGENTS.md: caches attach to content). Rejected.

Decision

typescript.CachedFacts.Models stays the field; ModelSyntaxVersion is 2 and Valid() rejects every other version, so version 1 entries miss once. The aggregate extractor version moves to the next free extractors-N (extractors-35 on this branch: main is at extractors-32 and the unmerged T018h stack is at extractors-34, so this branch takes the number after it for now and the next free number at its own merge time; only the living documents name it, never other ADRs or task records). The payload table in docs/lang/typescript-models.md names every capture branch; TestModelPayloadContract keeps code and table 1:1 over the drizzle-syntax and sequelize-syntax fixtures.

Gate (ModelGate(file, root, facts), first matching row; G1-G3 unchanged):

G5-G8 walk the tree only when a substring precheck on the file text could succeed (require and sequelize; define; dialect; init; [).

Statements (gated files only): module-level expression statements whose expression is a call or an = assignment (S3), class declarations with name, exported, default, abstract, the extends expression and a count of class and member decorators, never their arguments (S4), object-pattern declarators with their bindings and initializer (S5), and S1 also keeps new initializers. Function bodies of arrows (block bodies) and function expressions are ordered statement lists: declarations, destructuring declarations, expression statements, class declarations, returns, and an unknown entry for every other statement, so a consumer sees a conditional or loop between two statements (rows B-*). Module-level statements other than these stay uncaptured (SD), as in version 1.

Expressions: new (callee, arguments), function (name, parameters, statements; async and generator functions stay unknown, as async arrows do), assign (= only), index (o[e]), binary (operator and both operands) and unary (typeof, !, ...). this, regular expressions, conditionals, class expressions and compound assignments stay unknown. The task file listed new, function, regex and this; index is needed by O2's config[env], and binary/unary by the recognised associate loop's filter (typeof m.associate === "function"), which the linker must read before it trusts the loop.

CommonJS reach reads require("<relative>") initializers and module.exports = e / exports.x = e statements from the payload of gated files only; it is not a general CommonJS resolver (ADR 0014's deviation stays for every other consumer).

Consequences

Known gaps

Implementation notes

Payload version 2

Discovery and contract table

Models, attributes and options

References, associations, failures and wiring

Attribute helpers (task Revision 2)

Oracle

Review findings (task Revision 3)

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

Accepted after the green [ci windows] run 43605833309 on f921361 (Windows and postgres green; postgres job 0 FAIL, only env-gated skips).

Report a problem with this pageSource: docs/adr/0061-sequelize-models.md