Skip to content

fix(core): expose Model and association classes on Sequelize lazily - #18625

Open
tony1233200 wants to merge 1 commit into
sequelize:mainfrom
tony1233200:fix/18558-decorators-legacy-tdz
Open

tony1233200 wants to merge 1 commit into
sequelize:mainfrom
tony1233200:fix/18558-decorators-legacy-tdz

Conversation

@tony1233200

@tony1233200 tony1233200 commented Nov 20, 2026 •

Copy link
Copy Markdown

fix(core): expose Model and association classes on Sequelize lazily

Fixes #18558

Root cause

packages/core/src/sequelize.js assigned Sequelize.Model = Model (and the
four association classes) eagerly at module top level. model.js and
sequelize.js import each other, so when a module that imports model.js
first — e.g. @sequelize/core/decorators-legacy — triggers evaluation,
sequelize.js reads the Model binding while model.js is still
initializing, and Node throws
ReferenceError: Cannot access 'Model' before initialization (same for the
association classes, which flow through the same cycle: has-one.ts
imports { Model } from ../model as a value).

Fix

Expose Model, BelongsToAssociation, HasOneAssociation,
HasManyAssociation and BelongsToManyAssociation on Sequelize through
lazy getters (Object.defineProperty(..., { enumerable: true, get() {...} }))
instead of plain assignments — the same pattern already used for the
DataTypes statics just below. The bindings resolve on first access, by
which time all modules have loaded. AbstractQueryInterface is unaffected
(it does not participate in the cycle) and stays a plain assignment.

Reproduction

// CJS
require('@sequelize/core/decorators-legacy');
const { Model, Sequelize } = require('@sequelize/core');
console.log(Sequelize.Model === Model); // true
// ESM
await import('@sequelize/core/decorators-legacy');
const { Model, Sequelize } = await import('@sequelize/core');
console.log(Sequelize.Model === Model); // true
  • Before: both snippets throw
    ReferenceError: Cannot access 'Model' before initialization.
  • After: both print true, no error.

Tests

New regression tests in
packages/core/test/unit/decorators-legacy-import.test.ts (each spawns a
fresh process that imports decorators-legacy first, CJS and ESM):

  • Without the fix: 0 passing, 2 failing (both throw the TDZ error).
  • With the fix: 2 passing.

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/core and
    @sequelize/core/decorators-legacy both pass (CJS/ESM export parity
    unchanged). The 9 failures in that run are only the not-yet-built dialect
    packages (@sequelize/db2, @sequelize/mariadb, …), unrelated to this
    change.
  • test/unit/deep-exports.test.ts: 2 passing; the 1 failure is a
    pre-existing harness artifact (rejectedWith needs chai-as-promised,
    registered by test/support.ts, which that file does not import).

Lint: eslint on both changed files is clean; prettier --check is clean.


AI disclosure: this change was developed with AI assistance (Claude) and verified by running the reproduction and tests above.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where importing the legacy decorators package before the core package could prevent the core package from loading. This import order now works in both CommonJS and ESM environments.

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
@tony1233200
tony1233200 requested a review from a team as a code owner November 20, 2026 12:32
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Sequelize constructor now exposes Model and four association classes through getters. New tests check that importing decorators-legacy before core succeeds in both CommonJS and ESM.

Changes: Decorator-first imports

Layer / File(s) Summary
Lazy static exports and import-order tests
packages/core/src/sequelize.js, packages/core/test/unit/decorators-legacy-import.test.ts
Sequelize.Model and four association class statics now use enumerable, configurable getters. Two isolated child-process tests check decorator-first imports in CommonJS and ESM. AbstractQueryInterface remains directly assigned.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Suggested reviewers: ephys

Merge Risk: Minimal · up to a74b4

This change lets applications load the legacy decorators entrypoint before the core package without a load-order error. Access to the same classes through Sequelize is preserved. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the core change: Sequelize.Model and the association classes are exposed through lazy getters, which fixes the load-order TDZ error. The title uses the conventional fix(core) prefix an…
Linked Issues check Passed Directly linked issue #18558 requires that loading @sequelize/core/decorators-legacy before @sequelize/core no longer throws the TDZ error "Cannot access 'Model' before initialization", in CJS and ESM…
Out of Scope Changes check Passed The PR changes two files: packages/core/src/sequelize.js (getter conversion for the Model and association statics, with AbstractQueryInterface left as a plain assignment) and packages/core/test/unit/d…
Full details: Title check

Explanation

The title names the core change: Sequelize.Model and the association classes are exposed through lazy getters, which fixes the load-order TDZ error. The title uses the conventional fix(core) prefix and states the main change. The summary and objectives both describe lazy getters for Model and the association statics, so the title matches the changeset.

Full details: Linked Issues check

Explanation

Directly linked issue #18558 requires that loading @sequelize/core/decorators-legacy before @sequelize/core no longer throws the TDZ error "Cannot access 'Model' before initialization", in CJS and ESM. The issue names lazy getters on Sequelize.Model and the association classes as a valid fix. The PR summary states that Model, BelongsToAssociation, HasOneAssociation, HasManyAssociation and BelongsToManyAssociation are now enumerable, configurable getters in packages/core/src/sequelize.js, which removes the eager top-level read traced in the issue. The head blob of sequelize.js confirms the file imports these bindings. Its top-level content was truncated, so the getter code itself was not read directly. The new test file packages/core/test/unit/decorators-legacy-import.test.ts adds CJS and ESM child-process tests for the reported load order and checks Sequelize.Model === Model. Test results are author-reported; this review did not run them. The issue's secondary note about utils/model-utils.ts is a related finding, not a stated requirement of this issue. Evidence limits: the whole-PR diff could not be read because changed blobs are missing from the checkout.

Full details: Out of Scope Changes check

Explanation

The PR changes two files: packages/core/src/sequelize.js (getter conversion for the Model and association statics, with AbstractQueryInterface left as a plain assignment) and packages/core/test/unit/decorators-legacy-import.test.ts (regression tests). Both directly implement or verify the objective of #18558. No unrelated changes were identified in the summary. The whole-PR diff could not be read directly, so this verdict relies on the change summary and the PR description.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Biome (2.6.5)

packages/core/src/sequelize.js

File contains syntax errors that prevent linting: Line 3: Illegal use of an import declaration outside of a module; Line 4: Illegal use of an import declaration outside of a module; Line 5: Illegal use of an import declaration outside of a module; Line 6: Illegal use of an import declaration outside of a module; Line 7: Illegal use of an import declaration outside of a module; Line 8: Illegal use of an import declaration outside of a module; Line 9: Illegal use of an import declaration outside of a module; Line 10: Illegal use of an import declaration outside of a module; Line 11: Illegal use of an import declaration outside of a module; Line 12: Illegal use of an import declaration outside of a module; Line 13: Illegal use of an import declaration outside of a module; Line 14: Illegal use of an import declaration outside of a module; Line 15: Illegal use of an import declaration outside of a module; Line 16: Illegal use of an import declaration outside of a module; Line 17: Illegal us

... [truncated 1660 characters] ...

42: Illegal use of an import declaration outside of a module; Line 43: Illegal use of an import declaration outside of a module; Line 51: Illegal use of an import declaration outside of a module; Line 52: Illegal use of an import declaration outside of a module; Line 60: Illegal use of an import declaration outside of a module; Line 61: Illegal use of an import declaration outside of a module; Line 62: Illegal use of an import declaration outside of a module; Line 63: Illegal use of an import declaration outside of a module; Line 64: Illegal use of an import declaration outside of a module; Line 71: Illegal use of an export declaration outside of a module; Line 924: Illegal use of an export declaration outside of a module; Line 940: Illegal use of an export declaration outside of a module

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 39b4979d-f6a8-4e13-b885-330faa44bf3a
Commits

Reviewing files that changed from the base of the PR and between 53d1e8a and a74b462.

Files selected for processing: 2

  • packages/core/src/sequelize.js
  • packages/core/test/unit/decorators-legacy-import.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@SippieCup SippieCup left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@tony1233200

Copy link
Copy Markdown
Author

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!

@tony1233200 tony1233200 reopened this Nov 27, 2026
@tony1233200

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Importing @sequelize/core/decorators-legacy before @sequelize/core throws "Cannot access 'Model' before initialization"

2 participants