Repository: typeorm/typeorm
File: src/query-builder/SelectQueryBuilder.ts (TypeScript)

Code before:
        let transactionStartedByUs: boolean = false
        try {
            // start transaction if it was enabled
            if (
                this.expressionMap.useTransaction === true &&
                queryRunner.isTransactionActive === false
            ) {
                await queryRunner.startTransaction()
                transactionStartedByUs = true
            }

            this.expressionMap.queryEntity = true
            const entitiesAndRaw = await this.executeEntitiesAndRawResults(
                queryRunner,
            )
            this.expressionMap.queryEntity = false

            let count
            if (
                ((this.expressionMap.take !== undefined &&
                    entitiesAndRaw.entities.length < this.expressionMap.take) ||
                    (this.expressionMap.limit !== undefined &&
                        entitiesAndRaw.entities.length <
                            this.expressionMap.limit)) &&
                this.expressionMap.skip === undefined &&
                this.expressionMap.offset === undefined
            ) {
                // the limit was not reached, we already know the count
                count = entitiesAndRaw.entities.length
            } else {
                const cacheId = this.expressionMap.cacheId
                // Creates a new cacheId for the count query, or it will retrieve the above query results
                // and count will return 0.
                if (cacheId) {
                    this.expressionMap.cacheId = `${cacheId}-count`
                }
                count = await this.executeCountQuery(queryRunner)
            }
            const results: [Entity[], number] = [entitiesAndRaw.entities, count]

            // close transaction if we started it
            if (transactionStartedByUs) {
                await queryRunner.commitTransaction()
            }

            return results
        } catch (error) {
            // rollback transaction if we started it
            if (transactionStartedByUs) {
                try {
                    await queryRunner.rollbackTransaction()

Review comment (bug):
There are some edge cases for `take`/`limit`/`skip`/`offset`: `0`, `null`, `undefined`.

Code after:
        let transactionStartedByUs: boolean = false
        try {
            // start transaction if it was enabled
            if (
                this.expressionMap.useTransaction === true &&
                queryRunner.isTransactionActive === false
            ) {
                await queryRunner.startTransaction()
                transactionStartedByUs = true
            }

            this.expressionMap.queryEntity = true
            const entitiesAndRaw = await this.executeEntitiesAndRawResults(
                queryRunner,
            )
            this.expressionMap.queryEntity = false

            let count: number | undefined = this.lazyCount(entitiesAndRaw)
            if (count === undefined) {
                const cacheId = this.expressionMap.cacheId
                // Creates a new cacheId for the count query, or it will retrieve the above query results
                // and count will return 0.
                if (cacheId) {
                    this.expressionMap.cacheId = `${cacheId}-count`
                }
                count = await this.executeCountQuery(queryRunner)
            }
            const results: [Entity[], number] = [entitiesAndRaw.entities, count]

            // close transaction if we started it
            if (transactionStartedByUs) {
                await queryRunner.commitTransaction()
            }

            return results
        } catch (error) {
            // rollback transaction if we started it
            if (transactionStartedByUs) {
                try {
                    await queryRunner.rollbackTransaction()
                } catch (rollbackError) {}
            }
            throw error
        } finally {
            if (queryRunner !== this.queryRunner)
                // means we created our own query runner
                await queryRunner.release()
        }
    }

    private lazyCount(entitiesAndRaw: {