aggregate.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. /**
  3. * Copyright 2023 Google LLC. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.AggregateField = exports.Aggregate = void 0;
  19. const path_1 = require("./path");
  20. const assert = require("assert");
  21. /**
  22. * Concrete implementation of the Aggregate type.
  23. */
  24. class Aggregate {
  25. constructor(alias, aggregateType, fieldPath) {
  26. this.alias = alias;
  27. this.aggregateType = aggregateType;
  28. this.fieldPath = fieldPath;
  29. }
  30. /**
  31. * Converts this object to the proto representation of an Aggregate.
  32. * @internal
  33. */
  34. toProto() {
  35. const proto = {};
  36. if (this.aggregateType === 'count') {
  37. proto.count = {};
  38. }
  39. else if (this.aggregateType === 'sum') {
  40. assert(this.fieldPath !== undefined, 'Missing field path for sum aggregation.');
  41. proto.sum = {
  42. field: {
  43. fieldPath: path_1.FieldPath.fromArgument(this.fieldPath).formattedName,
  44. },
  45. };
  46. }
  47. else if (this.aggregateType === 'avg') {
  48. assert(this.fieldPath !== undefined, 'Missing field path for average aggregation.');
  49. proto.avg = {
  50. field: {
  51. fieldPath: path_1.FieldPath.fromArgument(this.fieldPath).formattedName,
  52. },
  53. };
  54. }
  55. else {
  56. throw new Error(`Aggregate type ${this.aggregateType} unimplemented.`);
  57. }
  58. proto.alias = this.alias;
  59. return proto;
  60. }
  61. }
  62. exports.Aggregate = Aggregate;
  63. /**
  64. * Represents an aggregation that can be performed by Firestore.
  65. */
  66. class AggregateField {
  67. /**
  68. * Create a new AggregateField<T>
  69. * @param aggregateType Specifies the type of aggregation operation to perform.
  70. * @param field Optionally specifies the field that is aggregated.
  71. * @internal
  72. */
  73. constructor(aggregateType, field) {
  74. this.aggregateType = aggregateType;
  75. /** A type string to uniquely identify instances of this class. */
  76. this.type = 'AggregateField';
  77. this._field = field;
  78. }
  79. /**
  80. * Compares this object with the given object for equality.
  81. *
  82. * This object is considered "equal" to the other object if and only if
  83. * `other` performs the same kind of aggregation on the same field (if any).
  84. *
  85. * @param other The object to compare to this object for equality.
  86. * @return `true` if this object is "equal" to the given object, as
  87. * defined above, or `false` otherwise.
  88. */
  89. isEqual(other) {
  90. return (other instanceof AggregateField &&
  91. this.aggregateType === other.aggregateType &&
  92. ((this._field === undefined && other._field === undefined) ||
  93. (this._field !== undefined &&
  94. other._field !== undefined &&
  95. path_1.FieldPath.fromArgument(this._field).isEqual(path_1.FieldPath.fromArgument(other._field)))));
  96. }
  97. /**
  98. * Create an AggregateField object that can be used to compute the count of
  99. * documents in the result set of a query.
  100. */
  101. static count() {
  102. return new AggregateField('count');
  103. }
  104. /**
  105. * Create an AggregateField object that can be used to compute the average of
  106. * a specified field over a range of documents in the result set of a query.
  107. * @param field Specifies the field to average across the result set.
  108. */
  109. static average(field) {
  110. return new AggregateField('avg', field);
  111. }
  112. /**
  113. * Create an AggregateField object that can be used to compute the sum of
  114. * a specified field over a range of documents in the result set of a query.
  115. * @param field Specifies the field to sum across the result set.
  116. */
  117. static sum(field) {
  118. return new AggregateField('sum', field);
  119. }
  120. }
  121. exports.AggregateField = AggregateField;
  122. //# sourceMappingURL=aggregate.js.map