Skip to content

fix: copy cordova query rows affected into query result - #10873

Merged
gioboa merged 3 commits into
typeorm:masterfrom
jacobg:fix
Jan 4, 2026
Merged

gioboa merged 3 commits into
typeorm:masterfrom
jacobg:fix

Conversation

@jacobg

@jacobg jacobg commented May 2, 2024 •

Copy link
Copy Markdown
Contributor

Close #7374

Cordova driver needs to copy query rows affected into query result

Summary by CodeRabbit

  • Bug Fixes
    • Query results for non-insert database operations now include the count of affected rows in addition to returned records and raw data, providing more complete operation feedback for callers and improving accuracy of result reporting.

@coderabbitai

coderabbitai Bot commented Oct 28, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

CordovaQueryRunner now assigns result.affected = raw.rowsAffected for non-insert queries, adding the affected-row count to the structured query result while keeping existing result fields and control flow unchanged.

Changes

Cohort / File(s) Summary
CordovaQueryRunner update
src/driver/cordova/CordovaQueryRunner.ts
For non-insert query results, assign result.affected = raw.rowsAffected in addition to populating result.records and result.raw, exposing the number of affected rows on the returned result object.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant CordovaQueryRunner
    participant CordovaDriver as Driver

    Caller->>CordovaQueryRunner: execute SQL (non-insert)
    CordovaQueryRunner->>Driver: runRawQuery(...)
    Driver-->>CordovaQueryRunner: raw { rows, rowsAffected, ... }
    alt previously
        CordovaQueryRunner->>Caller: result { records, raw }
    else now
        CordovaQueryRunner->>Caller: result { records, raw, affected: raw.rowsAffected }
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Single-file, small-value assignment change.
  • Areas to inspect:
    • raw.rowsAffected availability/format for all non-insert drivers and edge cases.
    • Consumers expecting result.affected to be undefined; ensure no downstream assumptions break.

Poem

🐇 I hopped through code all snug and bright,

Found rows that changed and counted right.
Now every query sings, "I did!" —
Affected hops and joy amid.
✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "copy cordova query rows affected into query result" is concise, clear, and directly summarizes the main change. It accurately reflects the implementation in CordovaQueryRunner.ts where result.affected is now assigned from raw.rowsAffected for non-insert queries. The title is specific enough that a teammate reviewing the commit history would understand the primary change without additional context.
Linked Issues Check ✅ Passed The code change directly addresses the objective of linked issue #7374, which requires that UpdateResult.affected return the exact number of affected rows instead of undefined when using SQLite-based drivers like Cordova. The implementation assigns result.affected = raw.rowsAffected for non-insert queries, which fulfills the requirement to populate the affected field in the query result so application code can rely on it for success/failure logic and tests.
Out of Scope Changes Check ✅ Passed The pull request contains only the necessary changes to address issue #7374. The modification is narrowly focused on CordovaQueryRunner.ts where a single assignment (result.affected = raw.rowsAffected) is added for non-insert query results. No additional files are modified, and the change is directly aligned with the stated objective of enabling the affected row count to be properly returned by the Cordova driver.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2025 •

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

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

commit: 5630c33

@gioboa gioboa left a comment

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.

Thanks for your help @jacobg
Is this PR related to an issue?

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/driver/cordova/CordovaQueryRunner.ts (1)

103-104: Consider setting result.affected for INSERT queries too.

For consistency with the non-INSERT branch and standard SQL driver behavior, INSERT queries should also propagate the affected row count.

Apply this diff to set result.affected in the INSERT branch:

 if (query.substr(0, 11) === "INSERT INTO") {
     result.raw = raw.insertId
+    result.affected = raw.rowsAffected
 } else {
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d7867eb and cb28e13.

📒 Files selected for processing (1)
  • src/driver/cordova/CordovaQueryRunner.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: tests-linux (18) / postgres (14)
  • GitHub Check: tests-linux (18) / mysql_mariadb
  • GitHub Check: tests-linux (18) / sqlite
  • GitHub Check: tests-linux (18) / mssql
  • GitHub Check: tests-linux (20) / sqlite
  • GitHub Check: tests-linux (20) / better-sqlite3
  • GitHub Check: tests-linux (20) / oracle
  • GitHub Check: tests-linux (20) / sap
  • GitHub Check: tests-linux (20) / postgres (17)
  • GitHub Check: tests-linux (20) / postgres (14)
  • GitHub Check: tests-linux (20) / sqljs
  • GitHub Check: tests-linux (20) / mssql
  • GitHub Check: tests-linux (20) / mongodb
  • GitHub Check: tests-linux (20) / mysql_mariadb
  • GitHub Check: tests-windows / sqlite
  • GitHub Check: tests-linux (20) / mysql_mariadb_latest
  • GitHub Check: tests-linux (20) / cockroachdb
  • GitHub Check: tests-windows / sqljs
  • GitHub Check: tests-windows / better-sqlite3
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (1)
src/driver/cordova/CordovaQueryRunner.ts (1)

113-113: Good addition for capturing affected rows.

This correctly propagates the rowsAffected from the Cordova SQLite result to the QueryResult object, which is essential for UPDATE and DELETE operations.

@coveralls

coveralls commented Oct 28, 2025 •

Copy link
Copy Markdown

Coverage Status

coverage: 80.905% (-0.01%) from 80.916%
when pulling 5630c33 on jacobg:fix
into 7de850e on typeorm:master.

@jacobg

jacobg commented Oct 29, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for your help @jacobg Is this PR related to an issue?

@gioboa Thanks, yes it's related to issue #7374

@gioboa gioboa left a comment

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.

Can you create a test to double check the fix please?

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cb28e13 and b194fd9.

📒 Files selected for processing (1)
  • src/driver/cordova/CordovaQueryRunner.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: tests-linux (20) / postgres (17)
  • GitHub Check: tests-linux (18) / oracle
  • GitHub Check: tests-linux (18) / sap
  • GitHub Check: tests-linux (20) / mssql
  • GitHub Check: tests-linux (20) / cockroachdb
  • GitHub Check: tests-linux (20) / mongodb
  • GitHub Check: tests-linux (18) / sqljs
  • GitHub Check: tests-linux (18) / mysql_mariadb
  • GitHub Check: tests-linux (18) / mssql
  • GitHub Check: tests-linux (18) / mongodb
  • GitHub Check: tests-linux (18) / postgres (17)
  • GitHub Check: tests-linux (18) / sqlite
  • GitHub Check: tests-linux (18) / postgres (14)
  • GitHub Check: tests-linux (18) / better-sqlite3
  • GitHub Check: tests-linux (18) / mysql_mariadb_latest
  • GitHub Check: tests-windows / better-sqlite3
  • GitHub Check: tests-windows / sqljs
  • GitHub Check: tests-windows / sqlite
  • GitHub Check: Analyze (javascript-typescript)


result.records = resultSet
result.raw = resultSet
result.affected = raw.rowsAffected

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.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Good fix for non-INSERT queries; verify INSERT and cross-driver consistency.

The assignment correctly populates result.affected with the row count for UPDATE, DELETE, and other non-INSERT queries, directly addressing issue #7374.

However, consider two points:

  1. INSERT queries (lines 103-104) don't set result.affected. INSERT statements typically affect exactly one row (or more in batch inserts), so result.affected = raw.rowsAffected should probably be set there as well for consistency.

  2. Other SQLite drivers – verify that sibling drivers (e.g., SqliteQueryRunner, BetterSqlite3QueryRunner, or others extending AbstractSqliteQueryRunner) also populate result.affected consistently. If they don't, this fix should be applied across all SQLite-based drivers.

Run the following script to check how other SQLite drivers handle result.affected:


🏁 Script executed:

#!/bin/bash
# Description: Check if other SQLite-based query runners populate result.affected

# Find all files that extend AbstractSqliteQueryRunner or contain similar query logic
fd -e ts -e js . src/driver | xargs rg -n -C5 'result\.affected'

Length of output: 14850


Set result.affected for INSERT queries as well to match other SQLite drivers.

The fix correctly addresses the reported issue for UPDATE, DELETE, and other non-INSERT queries. However, verification shows an inconsistency:

  • INSERT queries (lines 103–104) don't populate result.affected, while all other SQLite drivers in the codebase (SqliteQueryRunner, BetterSqlite3QueryRunner, ExpoQueryRunner, ReactNativeQueryRunner, CapacitorQueryRunner, SqljsQueryRunner) set it for all query types.

Add the same assignment to the INSERT branch (after line 104):

result.affected = raw.rowsAffected

This ensures Cordova's behavior aligns with other SQLite-based drivers and completes the fix for issue #7374 across all query types.

🤖 Prompt for AI Agents
In src/driver/cordova/CordovaQueryRunner.ts around line 113, the INSERT branch
currently does not set result.affected while other SQLite drivers do; add a line
after the INSERT handling (after line 104/around 113) to assign result.affected
= raw.rowsAffected so INSERT queries populate affected rows just like
UPDATE/DELETE and match behavior of other SQLite-based drivers.

@jacobg

jacobg commented Oct 31, 2025

Copy link
Copy Markdown
Contributor Author

Can you create a test to double check the fix please?

@gioboa It doesn't seem there are any cordova tests or test infrastructure? What would you suggest?

@gioboa gioboa left a comment

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.

It doesn't seem there are any cordova tests or test infrastructure? What would you suggest?

I would like to heard @alumni's opinion on this.

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

qodo-free-for-open-source-projects Bot commented Dec 1, 2025 •

Copy link
Copy Markdown

PR Code Suggestions ✨

Latest suggestions up to 5630c33

CategorySuggestion                                                                                                                                    Impact
General
Set affected rows for INSERT queries

Set the result.affected property for INSERT queries in addition to other query
types to ensure consistent query result objects. This can be done by adding the
assignment to the if block or moving it outside the conditional.

src/driver/cordova/CordovaQueryRunner.ts [103-114]

 if (query.substr(0, 11) === "INSERT INTO") {
     result.raw = raw.insertId
+    result.affected = raw.rowsAffected
 } else {
     const resultSet = []
     for (let i = 0; i < raw.rows.length; i++) {
         resultSet.push(raw.rows.item(i))
     }
 
     result.records = resultSet
     result.raw = resultSet
     result.affected = raw.rowsAffected
 }

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 7

__

Why: The suggestion correctly points out that the PR's change to set result.affected was incomplete, as it missed the case for INSERT queries. This leads to inconsistent behavior, which the suggestion fixes.

Medium
  • More

Previous suggestions

Suggestions up to commit afd0577
CategorySuggestion                                                                                                                                    Impact
General
Set affected rows for INSERT queries

Set the result.affected property for INSERT queries, not just for other query
types, to ensure rowsAffected is always captured. This can be done by adding the
assignment to the if block or moving it outside the conditional.

src/driver/cordova/CordovaQueryRunner.ts [103-114]

 if (query.substr(0, 11) === "INSERT INTO") {
     result.raw = raw.insertId
+    result.affected = raw.rowsAffected
 } else {
     const resultSet = []
     for (let i = 0; i < raw.rows.length; i++) {
         resultSet.push(raw.rows.item(i))
     }
 
     result.records = resultSet
     result.raw = resultSet
     result.affected = raw.rowsAffected
 }
Suggestion importance[1-10]: 8

__

Why: The suggestion correctly points out that the PR fails to set result.affected for INSERT queries, which is an oversight that leads to inconsistent and incorrect behavior for the query result object.

Medium

@alumni

alumni commented Jan 4, 2026

Copy link
Copy Markdown
Collaborator

We don't have tests for all drivers. In case of Cordova, we rely on community support mostly.

If anyone is interested to setup testing for drivers we don't have tests yet, please get in touch with us on our discord channel.

@gioboa gioboa left a comment

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.

Thanks @jacobg

@gioboa gioboa changed the title copy cordova query rows affected into query result fix: copy cordova query rows affected into query result Jan 4, 2026
@gioboa
gioboa merged commit ad22c10 into typeorm:master Jan 4, 2026
Cprakhar pushed a commit to Cprakhar/typeorm that referenced this pull request Jan 7, 2026
mgohin pushed a commit to mgohin/typeorm that referenced this pull request Jan 15, 2026
kranners pushed a commit to kranners/typeorm that referenced this pull request Mar 1, 2026
@pkuczynski pkuczynski added this to the 1.0 milestone Mar 8, 2026
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.

UpdateResult#affected always returning undefined in SQLite

5 participants