Repository navigation
fix(core): cache the Model class in isModelStatic to avoid repeated require() - #18234
Conversation
…equire() Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/test/unit/utils/model-utils.test.ts`:
- Around line 14-37: This test mutates Sequelize model state but the file lacks
per-test lifecycle hooks; add a beforeEach that calls sequelize.sync({ force:
true }) (or equivalent reset) and an afterEach that closes the Sequelize
instance (sequelize.close()) to ensure isolation between tests, and update any
tests referencing MyModel or calling isModelStatic to rely on those hooks so
Sequelize instances and models are reset between tests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: d67f9ece-e460-46b8-9dc8-197179d3c1cb
📒 Files selected for processing (2)
packages/core/src/utils/model-utils.tspackages/core/test/unit/utils/model-utils.test.ts
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
SippieCup
left a comment
There was a problem hiding this comment.
I just made the small change for you so we can get this merged.
In addition, I updated the assignment to work on older versions of node that we (unforunately) still support.
…test Filtering Module._load calls on requests ending in '/model' let the test pass if the require were spelled differently (e.g. '../model.js'). isModelStatic should not load anything once the cache is warm. Co-Authored-By: Claude Opus 5.5 <[email protected]>
model-typescript.ts already imports Model statically, so the per-call
require('./model.js') in addAttributeGetterAndSetters was not needed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@SippieCup about the Also, since your approval I pushed two small commits:
(A static import in |
Co-authored-by: Claude Opus 5.5 <[email protected]>
* origin/main: (54 commits) fix: use bind parameters in the Oracle catalog queries (sequelize#18498) meta: update mysql docker tag to v8.4.13 (sequelize#18577) meta: update dependency eslint-plugin-jsdoc to v65.4.0 (sequelize#18575) meta: update dependency node to v24.22.1 (sequelize#18574) meta: update dependency @oclif/core to ^5.1.9 (sequelize#18573) fix(core): skip removing foreign keys in dropAllTables when dropping with cascade (sequelize#18497) fix(core): drop each model only once in Sequelize#drop (sequelize#18572) fix(postgres): drop the enum types of a model when its table is dropped (sequelize#18554) fix(core): avoid lodash omit in the Model constructor (sequelize#18556) meta(core): support async functions in expectPerDialect (sequelize#18569) meta: update dependency ibm_db to ^4.1.0 (sequelize#18571) fix(core): make rejectInvalidOptions iterate supportable options and accept undefined (sequelize#18557) fix(core): respect the omitNull option of calls and models, fix query generator typings (sequelize#18565) meta(core): convert unit/model tests to TypeScript (sequelize#18564) meta(core): convert unit/instance tests to TypeScript (sequelize#18563) meta: update sequelize AUTHORS (sequelize#18549) feat(mssql): add useUnicodeStrings dialect option (sequelize#18548) meta: retry image pulls in the database start scripts (sequelize#18340) meta(core): follow-ups to the isModelStatic cache from sequelize#18234 (sequelize#18559) meta: update dependency uuid to v14 (sequelize#18544) ... # Conflicts: # packages/postgres/src/query-interface.js
* origin/main: (26 commits) fix: use bind parameters in the Oracle catalog queries (sequelize#18498) meta: update mysql docker tag to v8.4.13 (sequelize#18577) meta: update dependency eslint-plugin-jsdoc to v65.4.0 (sequelize#18575) meta: update dependency node to v24.22.1 (sequelize#18574) meta: update dependency @oclif/core to ^5.1.9 (sequelize#18573) fix(core): skip removing foreign keys in dropAllTables when dropping with cascade (sequelize#18497) fix(core): drop each model only once in Sequelize#drop (sequelize#18572) fix(postgres): drop the enum types of a model when its table is dropped (sequelize#18554) fix(core): avoid lodash omit in the Model constructor (sequelize#18556) meta(core): support async functions in expectPerDialect (sequelize#18569) meta: update dependency ibm_db to ^4.1.0 (sequelize#18571) fix(core): make rejectInvalidOptions iterate supportable options and accept undefined (sequelize#18557) fix(core): respect the omitNull option of calls and models, fix query generator typings (sequelize#18565) meta(core): convert unit/model tests to TypeScript (sequelize#18564) meta(core): convert unit/instance tests to TypeScript (sequelize#18563) meta: update sequelize AUTHORS (sequelize#18549) feat(mssql): add useUnicodeStrings dialect option (sequelize#18548) meta: retry image pulls in the database start scripts (sequelize#18340) meta(core): follow-ups to the isModelStatic cache from sequelize#18234 (sequelize#18559) meta: update dependency uuid to v14 (sequelize#18544) ... # Conflicts: # packages/core/src/abstract-dialect/query-generator.js # packages/core/src/abstract-dialect/query-interface-typescript.ts # packages/core/test/unit/model/bulk-create.test.js # packages/core/test/unit/sql/insert.test.js
Pull Request Checklist
Closes #18233
Description of Changes
The problem we hit
isModelStatic()callsrequire('../model')on every invocation:isModelStaticis on a hot path — it runs (directly, and viaisSameInitialModel/extractModelDefinition) once per association while building queries. Although Node caches the loaded module, therequire()call itself still funnels throughModule._loadand re-resolves the request every time.We ran into this in production: under our Node module-resolution setup, that per-call
require('../model')repeatedly walkedtrySelf → findParentPackageJSON → fs.stat, and on include-heavy queries (many associations) the resolution overhead showed up at the top of our CPU profiles. We were running this fix as apatch-packagepatch against@sequelize/[email protected]and wanted to upstream it.Even on a plain Node setup where each resolution is cheaper, the work is pure waste: the resolved
Modelclass never changes once the module graph is initialized.The fix
Cache the resolved
Modelclass in a module-level variable and reuse it on subsequent calls. The lazyrequire(the cyclic-import workaround) is preserved for the first call, so module-load ordering is unchanged; only the redundant re-resolution on later calls is removed.Tests
Added a unit test that spies on
Module._loadand asserts theModelmodule is not re-resolved across 50isModelStaticcalls after warm-up.I verified locally that the test passes with the fix and fails without it (the uncached version resolves the module once per call — 50 times). Behavior is unchanged and remains covered by the existing
isModelStatic/isSameInitialModeltests;yarn eslintandyarn lerna run test-typings --scope=@sequelize/coreboth pass.List of Breaking Changes
None. This is an internal, behavior-preserving performance optimization.
Summary by CodeRabbit
Refactor
Tests