Skip to content

fix(entity-manager): validate where criteria in increment/decrement - #12692

Merged
alumni merged 2 commits into
masterfrom
fix/increment-decrement-validate-where
Jul 13, 2026
Merged

alumni merged 2 commits into
masterfrom
fix/increment-decrement-validate-where

Conversation

@naorpeled

Copy link
Copy Markdown
Member

Problem

EntityManager.increment() / decrement() (and the Repository methods that delegate to them) built their UPDATE directly:

this.createQueryBuilder(entityClass, "entity")
    .update(entityClass)
    .set(values)
    .where(conditions)   // conditions went straight to the low-level QB .where()
    .execute()

This bypassed normalizeAndValidateWhereCriteria — the guard that update / delete / softDelete / restore use. Two consequences:

  • Empty criteria ran an unfiltered write. increment(User, {}, "points", 1) rendered UPDATE user SET points = points + 1 with no WHERE — every row. This is the same WHERE 1=1 mass-update class of bug the empty-criteria guard was added to prevent. (Intentional all-row updates already have updateAll.)
  • invalidWhereValuesBehavior was ignored for null/undefined values in the criteria, unlike the other write methods.

Flagged in review of #12690.

Change

Route conditions through normalizeAndValidateWhereCriteria in both increment and decrement, mirroring update exactly (including the isPrimitive → whereInIds branch). They now reject empty criteria and honor invalidWhereValuesBehavior, consistent with the other write methods.

No change to the QueryBuilder layer; low-level .where() remains intentionally exempt.

Tests

  • repository-increment / repository-decrement: empty {} criteria throws Empty criteria(s) and leaves all rows untouched; with invalidWhereValuesBehavior: { null: "throw" } configured, a null in the criteria throws Null value encountered.
  • Full increment/decrement suites pass (18/18 locally on better-sqlite3).

Note

This is a behavior change: an empty criteria that previously ran silently now throws — the intended, safer default (consistent with #12629 for update/delete).

@pkg-pr-new

pkg-pr-new Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12692
    

commit: 61a2864

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Informational

1. Outdated helper JSDoc 🐞 Bug ⚙ Maintainability
Description
normalizeAndValidateWhereCriteria is now used by increment/decrement, but its JSDoc still
claims it is shared only by update/delete/softDelete/restore. This can mislead maintainers about
which write APIs are guarded by the empty-criteria and invalidWhereValuesBehavior logic.
Code

src/entity-manager/EntityManager.ts[R1460-1472]

+        const { criteria: whereCriteria, isPrimitive } =
+            this.normalizeAndValidateWhereCriteria(conditions, "increment")
+
+        const qb = this.createQueryBuilder<Entity>(entityClass as any, "entity")
          .update(entityClass)
          .set(values)
-            .where(conditions)
-            .execute()
+        if (isPrimitive) {
+            qb.whereInIds(whereCriteria)
+        } else {
+            qb.where(whereCriteria)
+        }
+
+        return qb.execute()
Evidence
The helper’s comment explicitly lists only update/delete/softDelete/restore as callers, but the
modified increment (and similarly decrement) now calls normalizeAndValidateWhereCriteria, so
the comment is no longer accurate.

src/entity-manager/EntityManager.ts[822-834]
src/entity-manager/EntityManager.ts[1460-1472]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`EntityManager.normalizeAndValidateWhereCriteria`’s JSDoc is now stale: it says the helper is shared by `update/delete/softDelete/restore`, but this PR also routes `increment` and `decrement` through it.
### Issue Context
This is a documentation-only mismatch introduced by adding calls from `increment`/`decrement`.
### Fix Focus Areas
- src/entity-manager/EntityManager.ts[822-834]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Previous review results

Review updated until commit 25b4692

Results up to commit 7fd52a1


🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)


Informational
1. Outdated helper JSDoc 🐞 Bug ⚙ Maintainability
Description
normalizeAndValidateWhereCriteria is now used by increment/decrement, but its JSDoc still
claims it is shared only by update/delete/softDelete/restore. This can mislead maintainers about
which write APIs are guarded by the empty-criteria and invalidWhereValuesBehavior logic.
Code

src/entity-manager/EntityManager.ts[R1460-1472]

+        const { criteria: whereCriteria, isPrimitive } =
+            this.normalizeAndValidateWhereCriteria(conditions, "increment")
+
+        const qb = this.createQueryBuilder<Entity>(entityClass as any, "entity")
           .update(entityClass)
           .set(values)
-            .where(conditions)
-            .execute()
+        if (isPrimitive) {
+            qb.whereInIds(whereCriteria)
+        } else {
+            qb.where(whereCriteria)
+        }
+
+        return qb.execute()
Evidence
The helper’s comment explicitly lists only update/delete/softDelete/restore as callers, but the
modified increment (and similarly decrement) now calls normalizeAndValidateWhereCriteria, so
the comment is no longer accurate.

src/entity-manager/EntityManager.ts[822-834]
src/entity-manager/EntityManager.ts[1460-1472]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`EntityManager.normalizeAndValidateWhereCriteria`’s JSDoc is now stale: it says the helper is shared by `update/delete/softDelete/restore`, but this PR also routes `increment` and `decrement` through it.
### Issue Context
This is a documentation-only mismatch introduced by adding calls from `increment`/`decrement`.
### Fix Focus Areas
- src/entity-manager/EntityManager.ts[822-834]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Results up to commit 1783685


🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)


Informational
1. Outdated helper JSDoc 🐞 Bug ⚙ Maintainability
Description
normalizeAndValidateWhereCriteria is now used by increment/decrement, but its JSDoc still
claims it is shared only by update/delete/softDelete/restore. This can mislead maintainers about
which write APIs are guarded by the empty-criteria and invalidWhereValuesBehavior logic.
Code

src/entity-manager/EntityManager.ts[R1460-1472]

+        const { criteria: whereCriteria, isPrimitive } =
+            this.normalizeAndValidateWhereCriteria(conditions, "increment")
+
+        const qb = this.createQueryBuilder<Entity>(entityClass as any, "entity")
            .update(entityClass)
            .set(values)
-            .where(conditions)
-            .execute()
+        if (isPrimitive) {
+            qb.whereInIds(whereCriteria)
+        } else {
+            qb.where(whereCriteria)
+        }
+
+        return qb.execute()
Evidence
The helper’s comment explicitly lists only update/delete/softDelete/restore as callers, but the
modified increment (and similarly decrement) now calls normalizeAndValidateWhereCriteria, so
the comment is no longer accurate.

src/entity-manager/EntityManager.ts[822-834]
src/entity-manager/EntityManager.ts[1460-1472]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`EntityManager.normalizeAndValidateWhereCriteria`’s JSDoc is now stale: it says the helper is shared by `update/delete/softDelete/restore`, but this PR also routes `increment` and `decrement` through it.

### Issue Context
This is a documentation-only mismatch introduced by adding calls from `increment`/`decrement`.

### Fix Focus Areas
- src/entity-manager/EntityManager.ts[822-834]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Qodo Logo

@naorpeled
naorpeled force-pushed the fix/increment-decrement-validate-where branch from 1783685 to 7fd52a1 Compare July 10, 2026 21:50
increment() and decrement() built their UPDATE directly via
createQueryBuilder().update().set().where(conditions), bypassing
normalizeAndValidateWhereCriteria. An empty criteria therefore ran an
unfiltered UPDATE against every row, and a configured
invalidWhereValuesBehavior was ignored for null/undefined values — unlike
update/delete/softDelete/restore.

Route conditions through normalizeAndValidateWhereCriteria so increment and
decrement reject empty criteria and honor invalidWhereValuesBehavior,
consistent with the other write methods.
@naorpeled
naorpeled force-pushed the fix/increment-decrement-validate-where branch from 7fd52a1 to 25b4692 Compare July 10, 2026 22:16
@naorpeled
naorpeled requested review from alumni and smith-xyz July 10, 2026 22:16
Comment thread src/entity-manager/EntityManager.ts Outdated
.where(conditions)
.execute()
if (isPrimitive) {
qb.whereInIds(whereCriteria)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This line would never be reached.

However, I'm thinking we could call EntityManager.update which would already handle the validation part.

@alumni

alumni commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

@naorpeled I'd simply make increment/decrement call update, since in reality they're nothing more than a specialized update (one column is updated in a certain way).

This would ease maintaining them in general in the future, since we wouldn't have to think about them anymore.

@naorpeled

Copy link
Copy Markdown
Member Author

@copilot address the feedback by @alumni

Copilot AI commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

@copilot address the feedback by @alumni

Addressed in 61a2864: increment()/decrement() now delegate to EntityManager.update() after building the arithmetic partial update, so they reuse the existing criteria validation path instead of duplicating it.

@sonarqubecloud

Copy link
Copy Markdown

@alumni
alumni merged commit 8a51b75 into master Jul 13, 2026
48 checks passed
@alumni
alumni deleted the fix/increment-decrement-validate-where branch July 13, 2026 16:31
@github-actions github-actions Bot added this to the 1.0 milestone Jul 13, 2026
alumni added a commit that referenced this pull request Jul 13, 2026
…12692)

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
(cherry picked from commit 8a51b75)

Co-authored-by: Lucian Mocanu <[email protected]>
alumni added a commit that referenced this pull request Jul 13, 2026
…12692)

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
(cherry picked from commit 8a51b75)

Co-authored-by: Lucian Mocanu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants