1 | ;
|
2 | // Copyright 2023 Google LLC
|
3 | //
|
4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
5 | // you may not use this file except in compliance with the License.
|
6 | // You may obtain a copy of the License at
|
7 | //
|
8 | // https://www.apache.org/licenses/LICENSE-2.0
|
9 | //
|
10 | // Unless required by applicable law or agreed to in writing, software
|
11 | // distributed under the License is distributed on an "AS IS" BASIS,
|
12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | // See the License for the specific language governing permissions and
|
14 | // limitations under the License.
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.AGGREGATE_QUERY = exports.AggregateQuery = exports.AggregateField = void 0;
|
17 | const AGGREGATE_QUERY = Symbol('AGGREGATE_QUERY');
|
18 | exports.AGGREGATE_QUERY = AGGREGATE_QUERY;
|
19 | /**
|
20 | * An AggregateQuery is a class that can be used to obtain results from an
|
21 | * aggregate query request.
|
22 | *
|
23 | * @see {@link https://cloud.google.com/datastore/docs/aggregation-queries| Aggregation queries Reference}
|
24 | *
|
25 | * @class
|
26 | */
|
27 | class AggregateQuery {
|
28 | /**
|
29 | * Build an AggregateQuery object.
|
30 | *
|
31 | * @param {Query} query
|
32 | */
|
33 | constructor(query) {
|
34 | this.type = AGGREGATE_QUERY;
|
35 | this.query = query;
|
36 | this.aggregations = [];
|
37 | }
|
38 | /**
|
39 | * Add a `count` aggregate query to the list of aggregations.
|
40 | *
|
41 | * @param {string} alias
|
42 | * @returns {AggregateQuery}
|
43 | */
|
44 | count(alias) {
|
45 | this.aggregations.push(AggregateField.count().alias(alias));
|
46 | return this;
|
47 | }
|
48 | /**
|
49 | * Add a `sum` aggregate query to the list of aggregations.
|
50 | *
|
51 | * @param {string} property
|
52 | * @param {string} alias
|
53 | * @returns {AggregateQuery}
|
54 | */
|
55 | sum(property, alias) {
|
56 | this.aggregations.push(AggregateField.sum(property).alias(alias));
|
57 | return this;
|
58 | }
|
59 | /**
|
60 | * Add a `average` aggregate query to the list of aggregations.
|
61 | *
|
62 | * @param {string} property
|
63 | * @param {string} alias
|
64 | * @returns {AggregateQuery}
|
65 | */
|
66 | average(property, alias) {
|
67 | this.aggregations.push(AggregateField.average(property).alias(alias));
|
68 | return this;
|
69 | }
|
70 | /**
|
71 | * Add a custom aggregation to the list of aggregations.
|
72 | *
|
73 | * @param {AggregateField} aggregation
|
74 | * @returns {AggregateQuery}
|
75 | */
|
76 | addAggregation(aggregation) {
|
77 | this.aggregations.push(aggregation);
|
78 | return this;
|
79 | }
|
80 | /**
|
81 | * Add a list of custom aggregations to the list of aggregations.
|
82 | *
|
83 | * @param {AggregateField[]} aggregation
|
84 | * @returns {AggregateQuery}
|
85 | */
|
86 | addAggregations(aggregations) {
|
87 | for (const aggregation of aggregations) {
|
88 | this.aggregations.push(aggregation);
|
89 | }
|
90 | return this;
|
91 | }
|
92 | /**
|
93 | * Run the aggregation query and return the results.
|
94 | *
|
95 | * @param {RunQueryOptions | RequestCallback} [optionsOrCallback]
|
96 | * @param {function} cb The callback function.
|
97 | * @returns {void | Promise<RunQueryResponse>}
|
98 | */
|
99 | run(optionsOrCallback, cb) {
|
100 | const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
|
101 | const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
102 | const scope = this.query.scope;
|
103 | const runAggregationQuery = scope.runAggregationQuery.bind(scope);
|
104 | return runAggregationQuery(this, options, callback);
|
105 | }
|
106 | /**
|
107 | * Get the proto for the list of aggregations.
|
108 | *
|
109 | */
|
110 | toProto() {
|
111 | return this.aggregations.map(aggregation => aggregation.toProto());
|
112 | }
|
113 | }
|
114 | exports.AggregateQuery = AggregateQuery;
|
115 | /**
|
116 | * An AggregateField is a class that contains data that defines an aggregation.
|
117 | *
|
118 | */
|
119 | class AggregateField {
|
120 | /**
|
121 | * Gets a copy of the Count aggregate field.
|
122 | *
|
123 | * @returns {Count}
|
124 | */
|
125 | static count() {
|
126 | return new Count();
|
127 | }
|
128 | /**
|
129 | * Gets a copy of the Sum aggregate field.
|
130 | *
|
131 | * @returns {Sum}
|
132 | */
|
133 | static sum(property) {
|
134 | return new Sum(property);
|
135 | }
|
136 | /**
|
137 | * Gets a copy of the Average aggregate field.
|
138 | *
|
139 | * @returns {Average}
|
140 | */
|
141 | static average(property) {
|
142 | return new Average(property);
|
143 | }
|
144 | /**
|
145 | * Sets the alias on the aggregate field that should be used.
|
146 | *
|
147 | * @param {string} alias The label used in the results to describe this
|
148 | * aggregate field when a query is run.
|
149 | * @returns {AggregateField}
|
150 | */
|
151 | alias(alias) {
|
152 | if (alias) {
|
153 | this.alias_ = alias;
|
154 | }
|
155 | return this;
|
156 | }
|
157 | }
|
158 | exports.AggregateField = AggregateField;
|
159 | /**
|
160 | * A Count is a class that contains data that defines a Count aggregation.
|
161 | *
|
162 | */
|
163 | class Count extends AggregateField {
|
164 | /**
|
165 | * Gets the proto for the count aggregate field.
|
166 | *
|
167 | */
|
168 | toProto() {
|
169 | const count = Object.assign({});
|
170 | return Object.assign({ count }, this.alias_ ? { alias: this.alias_ } : null);
|
171 | }
|
172 | }
|
173 | /**
|
174 | * A PropertyAggregateField is a class that contains data that defines any
|
175 | * aggregation that is performed on a property.
|
176 | *
|
177 | */
|
178 | class PropertyAggregateField extends AggregateField {
|
179 | /**
|
180 | * Build a PropertyAggregateField object.
|
181 | *
|
182 | * @param {string} property
|
183 | */
|
184 | constructor(property_) {
|
185 | super();
|
186 | this.property_ = property_;
|
187 | }
|
188 | /**
|
189 | * Gets the proto for the property aggregate field.
|
190 | *
|
191 | */
|
192 | toProto() {
|
193 | const aggregation = this.property_
|
194 | ? { property: { name: this.property_ } }
|
195 | : {};
|
196 | return Object.assign({ operator: this.operator }, this.alias_ ? { alias: this.alias_ } : null, { [this.operator]: aggregation });
|
197 | }
|
198 | }
|
199 | /**
|
200 | * A Sum is a class that contains data that defines a Sum aggregation.
|
201 | *
|
202 | */
|
203 | class Sum extends PropertyAggregateField {
|
204 | constructor() {
|
205 | super(...arguments);
|
206 | this.operator = 'sum';
|
207 | }
|
208 | }
|
209 | /**
|
210 | * An Average is a class that contains data that defines an Average aggregation.
|
211 | *
|
212 | */
|
213 | class Average extends PropertyAggregateField {
|
214 | constructor() {
|
215 | super(...arguments);
|
216 | this.operator = 'avg';
|
217 | }
|
218 | }
|
219 | //# sourceMappingURL=aggregate.js.map |
\ | No newline at end of file |