Skip to content

getManyAndCount() returns incorrect count value for out-of-bounds skip #11640

@Drew-Kimberly

Description

@Drew-Kimberly

Issue description

getManyAndCount() returns incorrect count value for out-of-bounds skip

Expected Behavior

The following test case illustrates the regression introduced by #11524 :

// test/other-issues/lazy-count/lazy-count.ts

    it("should execute count query when skip is out-of-bounds", () =>
        Promise.all(
            connections.map(async function (connection) {
                await savePostEntities(connection, 5)

                const afterQuery = connection
                    .subscribers[0] as AfterQuerySubscriber
                afterQuery.clear()

                const [entities, count] = await connection.manager
                    .createQueryBuilder(Post, "post")
                    .take(10)
                    .skip(10) // Skip beyond the total number of records
                    .orderBy("post.id")
                    .getManyAndCount()

                expect(count).to.be.equal(5)
                expect(entities.length).to.be.equal(0)

                // The count query MUST be executed when skip is out-of-bounds
                expect(
                    afterQuery
                        .getCalledQueries()
                        .filter((query) => query.match(/(count|cnt)/i)),
                ).not.to.be.empty
            }),
        ))

Actual Behavior

The count query should be executed when skip is out-of-bounds so that the correct count value is returned.

Steps to reproduce

See unit test case

My Environment

Dependency Version
Operating System OSX Sequoia 15.5
Node.js version 20.19.1
TypeORM version 0.3.26

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions