Repository navigation
fix(core): expose Model and association classes on Sequelize lazily - #18625
tony1233200 wants to merge 1 commit into
Conversation
Importing '@sequelize/core/decorators-legacy' before '@sequelize/core' threw 'ReferenceError: Cannot access Model before initialization': sequelize.js read the Model binding eagerly at module load while model.js was still initializing (circular import between model.js and sequelize.js). Expose Model and the four association classes through lazy getters instead, so the bindings resolve on first access, after all modules have loaded. Fixes sequelize#18558
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
Closing this to clear the queue — it has been open for over a week with no maintainer review yet. The branch is kept as-is, so I can reopen it if there is interest. A community reviewer (SippieCup) approved it on 2026-11-21, but the requested maintainer review (WikiRik) never came. Thanks! |
|
Reopening — my automated queue cleanup closed this in error. This PR was already approved, so it should not have been closed. Sorry for the noise; no action needed from maintainers. |
fix(core): expose Model and association classes on Sequelize lazily
Fixes #18558
Root cause
packages/core/src/sequelize.jsassignedSequelize.Model = Model(and thefour association classes) eagerly at module top level.
model.jsandsequelize.jsimport each other, so when a module that importsmodel.jsfirst — e.g.
@sequelize/core/decorators-legacy— triggers evaluation,sequelize.jsreads theModelbinding whilemodel.jsis stillinitializing, and Node throws
ReferenceError: Cannot access 'Model' before initialization(same for theassociation classes, which flow through the same cycle:
has-one.tsimports
{ Model }from../modelas a value).Fix
Expose
Model,BelongsToAssociation,HasOneAssociation,HasManyAssociationandBelongsToManyAssociationonSequelizethroughlazy getters (
Object.defineProperty(..., { enumerable: true, get() {...} }))instead of plain assignments — the same pattern already used for the
DataTypesstatics just below. The bindings resolve on first access, bywhich time all modules have loaded.
AbstractQueryInterfaceis unaffected(it does not participate in the cycle) and stays a plain assignment.
Reproduction
ReferenceError: Cannot access 'Model' before initialization.true, no error.Tests
New regression tests in
packages/core/test/unit/decorators-legacy-import.test.ts(each spawns afresh process that imports
decorators-legacyfirst, CJS and ESM):Related suites (built
lib/,DIALECT=sqlite3):test/unit/decorators/**/*.test.ts+ the new test file: 323 passing,0 failing.
test/esm-named-exports.test.js:@sequelize/coreand@sequelize/core/decorators-legacyboth pass (CJS/ESM export parityunchanged). The 9 failures in that run are only the not-yet-built dialect
packages (
@sequelize/db2,@sequelize/mariadb, …), unrelated to thischange.
test/unit/deep-exports.test.ts: 2 passing; the 1 failure is apre-existing harness artifact (
rejectedWithneedschai-as-promised,registered by
test/support.ts, which that file does not import).Lint:
eslinton both changed files is clean;prettier --checkis clean.AI disclosure: this change was developed with AI assistance (Claude) and verified by running the reproduction and tests above.
Summary by CodeRabbit