GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — nodejs-datastore/src/aggregate.ts at main · googleapis/nodejs-datastore
This repository was archived by the owner on Feb 20, 2026. It is now read-only.
Expand file tree/ Copy pathaggregate.tsMore file actions 257 lines (234 loc) · 6.72 KB / Copy pathaggregate.tsFile metadata and controls257 lines (234 loc) · 6.72 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 // Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import {Query} from './index'; import {RunQueryOptions, RunQueryResponse} from './query'; import {RequestCallback} from './request'; const AGGREGATE_QUERY = Symbol('AGGREGATE_QUERY'); /** * An AggregateQuery is a class that can be used to obtain results from an * aggregate query request. * * @see {@link https://cloud.google.com/datastore/docs/aggregation-queries| Aggregation queries Reference} * * @class */ class AggregateQuery { type = AGGREGATE_QUERY; aggregations: Array<AggregateField>; query: Query | undefined; /** * Build an AggregateQuery object. * * @param {Query} query A Query object */ constructor(query: Query) { this.query = query; this.aggregations = []; } /** * Add a `count` aggregate query to the list of aggregations. * * @param {string} alias The label used in the results to describe this * aggregate field when a query is run. * @returns {AggregateQuery} */ count(alias?: string): AggregateQuery { this.aggregations.push(AggregateField.count().alias(alias)); return this; } /** * Add a `sum` aggregate query to the list of aggregations. * * @param {string} property The property to use for the sum calculation. * @param {string} alias The label used in the results to describe this * aggregate field when a query is run. * @returns {AggregateQuery} */ sum(property: string, alias?: string): AggregateQuery { this.aggregations.push(AggregateField.sum(property).alias(alias)); return this; } /** * Add a `average` aggregate query to the list of aggregations. * * @param {string} property The property to use for the average calculation. * @param {string} alias The label used in the results to describe this * aggregate field when a query is run. * @returns {AggregateQuery} */ average(property: string, alias?: string): AggregateQuery { this.aggregations.push(AggregateField.average(property).alias(alias)); return this; } /** * Add a custom aggregation to the list of aggregations. * * @param {AggregateField} aggregation The aggregate field to perform the * aggregation query over. * @returns {AggregateQuery} */ addAggregation(aggregation: AggregateField): AggregateQuery { this.aggregations.push(aggregation); return this; } /** * Add a list of custom aggregations to the list of aggregations. * * @param {AggregateField[]} aggregations The aggregate fields to perform the * aggregation query over. * @returns {AggregateQuery} */ addAggregations(aggregations: AggregateField[]): AggregateQuery { for (const aggregation of aggregations) { this.aggregations.push(aggregation); } return this; } /** * Run the aggregation query and return the results. * * @param {RunQueryOptions | RequestCallback} [optionsOrCallback] * @param {function} cb The callback function. * @returns {void | Promise<RunQueryResponse>} */ run( optionsOrCallback?: RunQueryOptions | RequestCallback, cb?: RequestCallback, ): void | Promise<RunQueryResponse> { const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb!; const scope = this.query!.scope; const runAggregationQuery = scope!.runAggregationQuery.bind(scope); return runAggregationQuery(this, options, callback); } /** * Get the proto for the list of aggregations. * */ toProto(): any { return this.aggregations.map(aggregation => aggregation.toProto()); } } /** * An AggregateField is a class that contains data that defines an aggregation. * */ abstract class AggregateField { alias_?: string; /** * Gets a copy of the Count aggregate field. * * @returns {Count} */ static count(): Count { return new Count(); } /** * Gets a copy of the Sum aggregate field. * * @param {string} property The property to use for the average calculation. * @returns {Sum} */ static sum(property: string): Sum { return new Sum(property); } /** * Gets a copy of the Average aggregate field. * * @param {string} property The property to use for the average calculation. * @returns {Average} */ static average(property: string): Average { return new Average(property); } /** * Sets the alias on the aggregate field that should be used. * * @param {string} alias The label used in the results to describe this * aggregate field when a query is run. * @returns {AggregateField} */ alias(alias?: string): AggregateField { if (alias) { this.alias_ = alias; } return this; } /** * Gets the proto for the aggregate field. * */ abstract toProto(): any; } /** * A Count is a class that contains data that defines a Count aggregation. * */ class Count extends AggregateField { /** * Gets the proto for the count aggregate field. * */ toProto(): any { const count = Object.assign({}); return Object.assign({count}, this.alias_ ? {alias: this.alias_} : null); } } /** * A PropertyAggregateField is a class that contains data that defines any * aggregation that is performed on a property. * */ abstract class PropertyAggregateField extends AggregateField { abstract operator: string; /** * Build a PropertyAggregateField object. * * @param {string} property The property to aggregate over. */ constructor(public property_: string) { super(); } /** * Gets the proto for the property aggregate field. * */ toProto(): any { const aggregation = this.property_ ? {property: {name: this.property_}} : {}; return Object.assign( {operator: this.operator}, this.alias_ ? {alias: this.alias_} : null, {[this.operator]: aggregation}, ); } } /** * A Sum is a class that contains data that defines a Sum aggregation. * */ class Sum extends PropertyAggregateField { operator = 'sum'; } /** * An Average is a class that contains data that defines an Average aggregation. * */ class Average extends PropertyAggregateField { operator = 'avg'; } export {AggregateField, AggregateQuery, AGGREGATE_QUERY}; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |