typeorm 0.3.26-dev.01dddfe → 0.3.26-dev.5904ac3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -588,6 +588,7 @@ export declare class SelectQueryBuilder<Entity extends ObjectLiteral> extends Qu
588
588
  * This method is useful to build pagination.
589
589
  */
590
590
  getManyAndCount(): Promise<[Entity[], number]>;
591
+ private lazyCount;
591
592
  /**
592
593
  * Executes built SQL query and returns raw data stream.
593
594
  */
@@ -846,11 +846,16 @@ export class SelectQueryBuilder extends QueryBuilder {
846
846
  this.expressionMap.queryEntity = true;
847
847
  const entitiesAndRaw = await this.executeEntitiesAndRawResults(queryRunner);
848
848
  this.expressionMap.queryEntity = false;
849
- const cacheId = this.expressionMap.cacheId;
850
- // Creates a new cacheId for the count query, or it will retreive the above query results
851
- // and count will return 0.
852
- this.expressionMap.cacheId = cacheId ? `${cacheId}-count` : cacheId;
853
- const count = await this.executeCountQuery(queryRunner);
849
+ let count = this.lazyCount(entitiesAndRaw);
850
+ if (count === undefined) {
851
+ const cacheId = this.expressionMap.cacheId;
852
+ // Creates a new cacheId for the count query, or it will retrieve the above query results
853
+ // and count will return 0.
854
+ if (cacheId) {
855
+ this.expressionMap.cacheId = `${cacheId}-count`;
856
+ }
857
+ count = await this.executeCountQuery(queryRunner);
858
+ }
854
859
  const results = [entitiesAndRaw.entities, count];
855
860
  // close transaction if we started it
856
861
  if (transactionStartedByUs) {
@@ -874,6 +879,39 @@ export class SelectQueryBuilder extends QueryBuilder {
874
879
  await queryRunner.release();
875
880
  }
876
881
  }
882
+ lazyCount(entitiesAndRaw) {
883
+ const hasLimit = this.expressionMap.limit !== undefined &&
884
+ this.expressionMap.limit !== null;
885
+ if (this.expressionMap.joinAttributes.length > 0 && hasLimit) {
886
+ return undefined;
887
+ }
888
+ const hasTake = this.expressionMap.take !== undefined &&
889
+ this.expressionMap.take !== null;
890
+ // limit overrides take when no join is defined
891
+ const maxResults = hasLimit
892
+ ? this.expressionMap.limit
893
+ : hasTake
894
+ ? this.expressionMap.take
895
+ : undefined;
896
+ if (maxResults !== undefined &&
897
+ entitiesAndRaw.entities.length === maxResults) {
898
+ // stop here when the result set contains the max number of rows; we need to execute a full count
899
+ return undefined;
900
+ }
901
+ const hasSkip = this.expressionMap.skip !== undefined &&
902
+ this.expressionMap.skip !== null &&
903
+ this.expressionMap.skip > 0;
904
+ const hasOffset = this.expressionMap.offset !== undefined &&
905
+ this.expressionMap.offset !== null &&
906
+ this.expressionMap.offset > 0;
907
+ // offset overrides skip when no join is defined
908
+ const previousResults = hasOffset
909
+ ? this.expressionMap.offset
910
+ : hasSkip
911
+ ? this.expressionMap.skip
912
+ : 0;
913
+ return entitiesAndRaw.entities.length + previousResults;
914
+ }
877
915
  /**
878
916
  * Executes built SQL query and returns raw data stream.
879
917
  */