Auto-include aggregation associations to prevent N+1 queries
This release focuses on eliminating N+1 queries in Sequelize-based aggregations by auto-detecting model associations and including them in queries. The change updates the query building and row processing flow, integrates the optimization into key operations, and adds targeted tests and documentation.
Performance Enhancements
- Prevent N+1 queries by auto-including aggregation associations (commit 9b3e876)
- Add addAggregationIncludes() in src/QueryBuilder.ts to detect model associations for aggregations, append Sequelize include entries, and return includedAggregations
- Update src/RowProcessor.ts: add includedAggregations parameter and skip buildAggregation() when data was preloaded via INCLUDE
- Wire the optimization into operations:
- src/ops/all.ts: use addAggregationIncludes() after buildQuery(); pass includedAggregations to processRow()
- src/ops/get.ts: add aggregation includes for primary key and composite key lookups; pass includedAggregations to processRow()
- src/ops/find.ts, src/ops/create.ts, src/ops/update.ts: pass undefined for includedAggregations since these operations don’t control the query
Testing Improvements
- Add tests to validate the N+1 prevention logic and integration paths (commit 9b3e876)
- tests/QueryBuilder-aggregation.test.ts: unit tests for addAggregationIncludes()
- tests/aggregation-n-plus-one.test.ts and tests/aggregation-pressure.test.ts: integration and performance scenarios
- Update tests/ops/all.test.ts to mock addAggregationIncludes() and expect the includedAggregations argument
Documentation Updates
- Add implementation and test summaries (commit 9b3e876)
- N1_FIX_IMPLEMENTATION_SUMMARY.md
- TEST_SUMMARY.md
- Bump React dependencies in the docs to latest patch versions (commit f5ecaf9)
- docs/package.json: react ^19.1.0 → ^19.1.2, react-dom ^19.1.0 → ^19.1.2
Version
- Bump package.json version to 4.4.75 (commit 0874917)