@onchaindb/sdk 2.0.0 → 2.1.0

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.
Files changed (44) hide show
  1. package/.claude/settings.local.json +5 -1
  2. package/.gitignore +1 -0
  3. package/README.md +8 -9
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/client.js +0 -5
  6. package/dist/client.js.map +1 -1
  7. package/dist/database.d.ts +46 -8
  8. package/dist/database.d.ts.map +1 -1
  9. package/dist/database.js +32 -9
  10. package/dist/database.js.map +1 -1
  11. package/dist/index.d.ts +1 -2
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/query-sdk/NestedBuilders.d.ts +3 -2
  15. package/dist/query-sdk/NestedBuilders.d.ts.map +1 -1
  16. package/dist/query-sdk/NestedBuilders.js +7 -4
  17. package/dist/query-sdk/NestedBuilders.js.map +1 -1
  18. package/dist/query-sdk/QueryBuilder.d.ts +17 -15
  19. package/dist/query-sdk/QueryBuilder.d.ts.map +1 -1
  20. package/dist/query-sdk/QueryBuilder.js +126 -190
  21. package/dist/query-sdk/QueryBuilder.js.map +1 -1
  22. package/dist/query-sdk/index.d.ts +24 -1
  23. package/dist/query-sdk/index.d.ts.map +1 -1
  24. package/dist/query-sdk/index.js.map +1 -1
  25. package/dist/query-sdk/operators.d.ts +3 -2
  26. package/dist/query-sdk/operators.d.ts.map +1 -1
  27. package/dist/query-sdk/operators.js +7 -4
  28. package/dist/query-sdk/operators.js.map +1 -1
  29. package/dist/types.d.ts +17 -13
  30. package/dist/types.d.ts.map +1 -1
  31. package/dist/types.js.map +1 -1
  32. package/package.json +1 -1
  33. package/skills.md +5 -5
  34. package/src/client.ts +1 -7
  35. package/src/database.ts +159 -26
  36. package/src/index.ts +16 -10
  37. package/src/query-sdk/NestedBuilders.ts +14 -4
  38. package/src/query-sdk/QueryBuilder.ts +212 -235
  39. package/src/query-sdk/index.ts +19 -1
  40. package/src/query-sdk/operators.ts +15 -4
  41. package/src/query-sdk/tests/FieldConditionBuilder.test.ts +4 -2
  42. package/src/query-sdk/tests/NestedBuilders.test.ts +4 -3
  43. package/src/types.ts +26 -17
  44. package/.DS_Store +0 -0
@@ -2,7 +2,7 @@ import { QueryRequest, QueryResponse, SelectionMap, Val } from '../index';
2
2
  import { LogicalOperator } from './operators';
3
3
  import { SelectionBuilder } from './SelectionBuilder';
4
4
  import { ConditionBuilder } from './ConditionBuilder';
5
- import { FieldMap, HttpClient } from "./index";
5
+ import { FieldMap, HttpClient, AggregateSpec } from "./index";
6
6
  export interface ServerJoinConfig {
7
7
  alias: string;
8
8
  model: string;
@@ -12,19 +12,13 @@ export interface ServerJoinConfig {
12
12
  select?: SelectionMap;
13
13
  };
14
14
  }
15
- export interface JoinConfig {
16
- parentCollection: string;
17
- parentField: string;
18
- childField: string;
19
- alias: string;
20
- }
21
15
  export declare class QueryBuilder {
22
16
  private findConditions?;
23
17
  private selections?;
24
18
  private fieldMap?;
25
19
  private limitValue?;
26
20
  private offsetValue?;
27
- private sortBy?;
21
+ private _sortField?;
28
22
  private sortDirection?;
29
23
  private includeHistoryValue?;
30
24
  private httpClient?;
@@ -32,7 +26,8 @@ export declare class QueryBuilder {
32
26
  private collectionName;
33
27
  private app;
34
28
  private serverJoinConfigs;
35
- private joinConfigs;
29
+ private _groupBy?;
30
+ private _aggregate?;
36
31
  constructor(httpClient?: HttpClient, serverUrl?: string, app?: string);
37
32
  static new(httpClient?: HttpClient, serverUrl?: string, app?: string): QueryBuilder;
38
33
  withHttpClient(httpClient: HttpClient): QueryBuilder;
@@ -47,14 +42,16 @@ export declare class QueryBuilder {
47
42
  joinWith(alias: string, model: string): JoinBuilder;
48
43
  joinOne(alias: string, model: string): JoinBuilder;
49
44
  joinMany(alias: string, model: string): JoinBuilder;
50
- join(parentCollection: string, parentField: string, childField: string, alias: string): QueryBuilder;
51
45
  select(builderFn: (builder: SelectionBuilder) => SelectionBuilder): QueryBuilder;
52
46
  selectFields(fields: string[]): QueryBuilder;
53
47
  selectAll(): QueryBuilder;
54
48
  withFieldMap(fieldMap: FieldMap): QueryBuilder;
55
49
  limit(limit: number): QueryBuilder;
50
+ take(n: number): QueryBuilder;
56
51
  offset(offset: number): QueryBuilder;
57
- orderBy(fields: string, direction?: "ASC" | "DESC"): QueryBuilder;
52
+ skip(n: number): QueryBuilder;
53
+ sortBy(field: string, direction?: "asc" | "desc" | "ASC" | "DESC"): QueryBuilder;
54
+ orderBy(field: string, direction?: "ASC" | "DESC"): QueryBuilder;
58
55
  includeHistory(include?: boolean): QueryBuilder;
59
56
  execute<T extends Record<string, any>>(): Promise<QueryResponse<T>>;
60
57
  executeUnique<T extends Record<string, any>>(): Promise<T | null>;
@@ -65,6 +62,7 @@ export declare class QueryBuilder {
65
62
  minBy<T = any>(field: string): Promise<T | null>;
66
63
  distinctBy<T = any>(field: string): Promise<T[]>;
67
64
  countDistinct(field: string): Promise<number>;
65
+ runAggregate(spec: AggregateSpec): Promise<Record<string, any>>;
68
66
  groupBy(field: string): GroupByQueryBuilder;
69
67
  executeWithPayment<T extends Record<string, any>>(quoteId: string, paymentProof: string, network?: string): Promise<QueryResponse<T>>;
70
68
  getQueryRequest(): QueryRequest;
@@ -72,8 +70,10 @@ export declare class QueryBuilder {
72
70
  buildRawQuery(): QueryRequest;
73
71
  clone(): QueryBuilder;
74
72
  _addServerJoin(config: ServerJoinConfig): void;
73
+ _setSelections(select: SelectionMap): this;
74
+ _setGroupBy(fields: string[]): this;
75
+ _setAggregate(agg: AggregateSpec): this;
75
76
  private executeSimpleQuery;
76
- private executeWithJoins;
77
77
  private buildQueryValue;
78
78
  }
79
79
  export declare class WhereClause {
@@ -103,9 +103,10 @@ export declare class WhereClause {
103
103
  isLocalIp(): QueryBuilder;
104
104
  isExternalIp(): QueryBuilder;
105
105
  b64(value: string): QueryBuilder;
106
- inDataset(dataset: string): QueryBuilder;
106
+ inDataset(values: string[]): QueryBuilder;
107
107
  inCountry(countryCode: string): QueryBuilder;
108
- cidr(cidr: string): QueryBuilder;
108
+ cidr(ranges: string | string[]): QueryBuilder;
109
+ keywords(keywords: string[]): QueryBuilder;
109
110
  between(min: any, max: any): QueryBuilder;
110
111
  isTrue(): QueryBuilder;
111
112
  isFalse(): QueryBuilder;
@@ -152,6 +153,7 @@ export declare class GroupByQueryBuilder {
152
153
  avgBy(field: string): Promise<Record<string, number>>;
153
154
  maxBy<T = any>(field: string): Promise<Record<string, T>>;
154
155
  minBy<T = any>(field: string): Promise<Record<string, T>>;
155
- private getGroupKey;
156
+ run(spec: AggregateSpec): Promise<Record<string, any>[]>;
157
+ private buildGroupMap;
156
158
  }
157
159
  //# sourceMappingURL=QueryBuilder.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"QueryBuilder.d.ts","sourceRoot":"","sources":["../../src/query-sdk/QueryBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,GAAG,EAAuB,MAAM,UAAU,CAAC;AAE9F,OAAO,EAAwB,eAAe,EAAC,MAAM,aAAa,CAAC;AACnE,OAAO,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAC,QAAQ,EAAE,UAAU,EAAC,MAAM,SAAS,CAAC;AAS7C,MAAM,WAAW,gBAAgB;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,EAAE;QACL,IAAI,CAAC,EAAE,GAAG,CAAC;QACX,MAAM,CAAC,EAAE,YAAY,CAAC;KACzB,CAAC;CACL;AAGD,MAAM,WAAW,UAAU;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;CACjB;AAGD,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,CAAC,GAAG,GAAG,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAezD,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IA+BzD,GAAG,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;IAa9D,OAAO,CAAC,aAAa;CAQxB"}
@@ -9,7 +9,6 @@ const x402_1 = require("../x402");
9
9
  class QueryBuilder {
10
10
  constructor(httpClient, serverUrl, app) {
11
11
  this.serverJoinConfigs = [];
12
- this.joinConfigs = [];
13
12
  this.httpClient = httpClient;
14
13
  this.serverUrl = serverUrl;
15
14
  this.app = app;
@@ -55,16 +54,6 @@ class QueryBuilder {
55
54
  joinMany(alias, model) {
56
55
  return new JoinBuilder(this, alias, model, true);
57
56
  }
58
- join(parentCollection, parentField, childField, alias) {
59
- console.warn('QueryBuilder.join() is deprecated. Use serverJoin(), joinOne(), or joinMany() for server-side JOINs.');
60
- this.joinConfigs.push({
61
- parentCollection,
62
- parentField,
63
- childField,
64
- alias
65
- });
66
- return this;
67
- }
68
57
  select(builderFn) {
69
58
  const selectionBuilder = new SelectionBuilder_1.SelectionBuilder();
70
59
  builderFn(selectionBuilder);
@@ -89,24 +78,21 @@ class QueryBuilder {
89
78
  this.limitValue = limit;
90
79
  return this;
91
80
  }
81
+ take(n) { return this.limit(n); }
92
82
  offset(offset) {
93
83
  this.offsetValue = offset;
94
84
  return this;
95
85
  }
96
- orderBy(fields, direction) {
97
- this.sortBy = fields;
98
- switch (direction) {
99
- case "ASC":
100
- this.sortDirection = "ascending";
101
- break;
102
- case "DESC":
103
- this.sortDirection = "descending";
104
- break;
105
- default:
106
- this.sortDirection = "ascending";
107
- }
86
+ skip(n) { return this.offset(n); }
87
+ sortBy(field, direction) {
88
+ this._sortField = field;
89
+ const dir = direction?.toLowerCase();
90
+ this.sortDirection = dir === "desc" ? "descending" : "ascending";
108
91
  return this;
109
92
  }
93
+ orderBy(field, direction) {
94
+ return this.sortBy(field, direction);
95
+ }
110
96
  includeHistory(include = true) {
111
97
  this.includeHistoryValue = include;
112
98
  return this;
@@ -118,13 +104,6 @@ class QueryBuilder {
118
104
  if (!this.serverUrl) {
119
105
  throw new Error('Server URL is required for query execution');
120
106
  }
121
- if (this.serverJoinConfigs.length > 0) {
122
- return this.executeSimpleQuery();
123
- }
124
- if (this.joinConfigs.length > 0) {
125
- console.warn('Using deprecated client-side JOINs. Consider migrating to serverJoin() for better performance.');
126
- return this.executeWithJoins();
127
- }
128
107
  return this.executeSimpleQuery();
129
108
  }
130
109
  async executeUnique() {
@@ -150,49 +129,44 @@ class QueryBuilder {
150
129
  return sortedRecords[0];
151
130
  }
152
131
  async count() {
153
- const response = await this.execute();
154
- return response.records?.length || 0;
132
+ const clone = this.clone();
133
+ clone._setSelections({});
134
+ clone._setAggregate({ _count: { '$count': '*' } });
135
+ const response = await clone.execute();
136
+ const first = response.records?.[0];
137
+ return first?._count ?? 0;
155
138
  }
156
139
  async sumBy(field) {
157
- const response = await this.execute();
158
- if (!response.records)
159
- return 0;
160
- return response.records.reduce((sum, record) => {
161
- const value = record[field];
162
- return sum + (typeof value === 'number' ? value : 0);
163
- }, 0);
140
+ const clone = this.clone();
141
+ clone._setSelections({});
142
+ clone._setAggregate({ _result: { '$sum': field } });
143
+ const response = await clone.execute();
144
+ const first = response.records?.[0];
145
+ return first?._result ?? 0;
164
146
  }
165
147
  async avgBy(field) {
166
- const response = await this.execute();
167
- if (!response.records || response.records.length === 0)
168
- return 0;
169
- const sum = response.records.reduce((total, record) => {
170
- const value = record[field];
171
- return total + (typeof value === 'number' ? value : 0);
172
- }, 0);
173
- return sum / response.records.length;
148
+ const clone = this.clone();
149
+ clone._setSelections({});
150
+ clone._setAggregate({ _result: { '$avg': field } });
151
+ const response = await clone.execute();
152
+ const first = response.records?.[0];
153
+ return first?._result ?? 0;
174
154
  }
175
155
  async maxBy(field) {
176
- const response = await this.execute();
177
- if (!response.records || response.records.length === 0)
178
- return null;
179
- return response.records.reduce((max, record) => {
180
- const value = record[field];
181
- if (max === null)
182
- return value;
183
- return value > max ? value : max;
184
- }, null);
156
+ const clone = this.clone();
157
+ clone._setSelections({});
158
+ clone._setAggregate({ _result: { '$max': field } });
159
+ const response = await clone.execute();
160
+ const first = response.records?.[0];
161
+ return first?._result ?? null;
185
162
  }
186
163
  async minBy(field) {
187
- const response = await this.execute();
188
- if (!response.records || response.records.length === 0)
189
- return null;
190
- return response.records.reduce((min, record) => {
191
- const value = record[field];
192
- if (min === null)
193
- return value;
194
- return value < min ? value : min;
195
- }, null);
164
+ const clone = this.clone();
165
+ clone._setSelections({});
166
+ clone._setAggregate({ _result: { '$min': field } });
167
+ const response = await clone.execute();
168
+ const first = response.records?.[0];
169
+ return first?._result ?? null;
196
170
  }
197
171
  async distinctBy(field) {
198
172
  const response = await this.execute();
@@ -208,8 +182,19 @@ class QueryBuilder {
208
182
  return Array.from(uniqueValues);
209
183
  }
210
184
  async countDistinct(field) {
211
- const distinctValues = await this.distinctBy(field);
212
- return distinctValues.length;
185
+ const clone = this.clone();
186
+ clone._setSelections({});
187
+ clone._setAggregate({ _result: { '$countDistinct': field } });
188
+ const response = await clone.execute();
189
+ const first = response.records?.[0];
190
+ return first?._result ?? 0;
191
+ }
192
+ async runAggregate(spec) {
193
+ const clone = this.clone();
194
+ clone._setSelections({});
195
+ clone._setAggregate(spec);
196
+ const response = await clone.execute();
197
+ return response.records?.[0] ?? {};
213
198
  }
214
199
  groupBy(field) {
215
200
  return new GroupByQueryBuilder(this, field);
@@ -259,7 +244,7 @@ class QueryBuilder {
259
244
  ...queryValue,
260
245
  limit: this.limitValue,
261
246
  offset: this.offsetValue,
262
- sortBy: this.sortBy,
247
+ sortBy: this._sortField,
263
248
  sortDirection: this.sortDirection,
264
249
  root: `${this.app}::${this.collectionName}`
265
250
  };
@@ -272,7 +257,7 @@ class QueryBuilder {
272
257
  ...this.buildQueryValue(),
273
258
  limit: this.limitValue,
274
259
  offset: this.offsetValue,
275
- sortBy: this.sortBy,
260
+ sortBy: this._sortField,
276
261
  sortDirection: this.sortDirection,
277
262
  };
278
263
  }
@@ -283,17 +268,30 @@ class QueryBuilder {
283
268
  cloned.fieldMap = this.fieldMap ? { ...this.fieldMap } : undefined;
284
269
  cloned.limitValue = this.limitValue;
285
270
  cloned.offsetValue = this.offsetValue;
286
- cloned.sortBy = this.sortBy;
271
+ cloned._sortField = this._sortField;
287
272
  cloned.sortDirection = this.sortDirection;
288
273
  cloned.includeHistoryValue = this.includeHistoryValue;
289
274
  cloned.collectionName = this.collectionName;
290
275
  cloned.serverJoinConfigs = [...this.serverJoinConfigs];
291
- cloned.joinConfigs = [...this.joinConfigs];
276
+ cloned._groupBy = this._groupBy ? [...this._groupBy] : undefined;
277
+ cloned._aggregate = this._aggregate ? { ...this._aggregate } : undefined;
292
278
  return cloned;
293
279
  }
294
280
  _addServerJoin(config) {
295
281
  this.serverJoinConfigs.push(config);
296
282
  }
283
+ _setSelections(select) {
284
+ this.selections = select;
285
+ return this;
286
+ }
287
+ _setGroupBy(fields) {
288
+ this._groupBy = fields;
289
+ return this;
290
+ }
291
+ _setAggregate(agg) {
292
+ this._aggregate = agg;
293
+ return this;
294
+ }
297
295
  async executeSimpleQuery() {
298
296
  const request = this.getQueryRequest();
299
297
  try {
@@ -329,52 +327,6 @@ class QueryBuilder {
329
327
  throw new Error(`Query execution failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
330
328
  }
331
329
  }
332
- async executeWithJoins() {
333
- const mainQuery = this.clone();
334
- mainQuery.joinConfigs = [];
335
- const mainResults = await mainQuery.executeSimpleQuery();
336
- if (mainResults.records.length === 0) {
337
- return mainResults;
338
- }
339
- const joinResults = {};
340
- for (const joinConfig of this.joinConfigs) {
341
- const joinKeys = [...new Set(mainResults.records
342
- .map(record => record[joinConfig.childField])
343
- .filter(key => key !== null && key !== undefined))];
344
- if (joinKeys.length > 0) {
345
- const joinQuery = new QueryBuilder(this.httpClient, this.serverUrl, this.app);
346
- const joinResponse = await joinQuery
347
- .collection(joinConfig.parentCollection)
348
- .whereField(joinConfig.parentField).in(joinKeys)
349
- .selectAll()
350
- .executeSimpleQuery();
351
- joinResults[joinConfig.alias] = joinResponse.records;
352
- }
353
- else {
354
- joinResults[joinConfig.alias] = [];
355
- }
356
- }
357
- const mergedRecords = mainResults.records.map(mainRecord => {
358
- const merged = { ...mainRecord };
359
- for (const joinConfig of this.joinConfigs) {
360
- const joinKey = mainRecord[joinConfig.childField];
361
- const relatedRecords = joinResults[joinConfig.alias].filter(joinRecord => joinRecord[joinConfig.parentField] === joinKey);
362
- if (joinConfig.parentCollection === this.collectionName) {
363
- merged[joinConfig.alias] = relatedRecords;
364
- }
365
- else {
366
- merged[joinConfig.alias] = relatedRecords.length > 0 ? relatedRecords[0] : null;
367
- }
368
- }
369
- return merged;
370
- });
371
- return {
372
- records: mergedRecords,
373
- limit: mainResults.limit,
374
- page: mainResults.page,
375
- total: mainResults.total
376
- };
377
- }
378
330
  buildQueryValue() {
379
331
  let find = this.findConditions ? this.findConditions.toComposable() : {};
380
332
  const select = this.selections || SelectionBuilder_1.SelectionBuilder.all();
@@ -397,6 +349,15 @@ class QueryBuilder {
397
349
  if (this.includeHistoryValue !== undefined) {
398
350
  queryValue.include_history = this.includeHistoryValue;
399
351
  }
352
+ if (this.fieldMap && Object.keys(this.fieldMap).length > 0) {
353
+ queryValue.field_map = this.fieldMap;
354
+ }
355
+ if (this._groupBy && this._groupBy.length > 0) {
356
+ queryValue.group_by = this._groupBy;
357
+ }
358
+ if (this._aggregate && Object.keys(this._aggregate).length > 0) {
359
+ queryValue.aggregate = this._aggregate;
360
+ }
400
361
  return queryValue;
401
362
  }
402
363
  }
@@ -432,9 +393,10 @@ class WhereClause {
432
393
  isLocalIp() { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).isLocalIp()); }
433
394
  isExternalIp() { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).isExternalIp()); }
434
395
  b64(value) { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).b64(value)); }
435
- inDataset(dataset) { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).inDataset(dataset)); }
396
+ inDataset(values) { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).inDataset(values)); }
436
397
  inCountry(countryCode) { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).inCountry(countryCode)); }
437
- cidr(cidr) { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).cidr(cidr)); }
398
+ cidr(ranges) { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).cidr(ranges)); }
399
+ keywords(keywords) { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).keywords(keywords)); }
438
400
  between(min, max) { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).between(min, max)); }
439
401
  isTrue() { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).isTrue()); }
440
402
  isFalse() { return this.set(new operators_1.FieldConditionBuilder(this.fieldName).isFalse()); }
@@ -541,86 +503,60 @@ class GroupByQueryBuilder {
541
503
  this.groupByField = groupByField;
542
504
  }
543
505
  async count() {
544
- const response = await this.queryBuilder.execute();
545
- if (!response.records)
546
- return {};
547
- const groups = {};
548
- for (const record of response.records) {
549
- const key = this.getGroupKey(record);
550
- groups[key] = (groups[key] || 0) + 1;
551
- }
552
- return groups;
506
+ const clone = this.queryBuilder.clone();
507
+ clone._setGroupBy([this.groupByField]);
508
+ clone._setSelections({});
509
+ clone._setAggregate({ _count: { '$count': '*' } });
510
+ const response = await clone.execute();
511
+ return this.buildGroupMap(response.records, '_count');
553
512
  }
554
513
  async sumBy(field) {
555
- const response = await this.queryBuilder.execute();
556
- if (!response.records)
557
- return {};
558
- const groups = {};
559
- for (const record of response.records) {
560
- const key = this.getGroupKey(record);
561
- const value = record[field];
562
- groups[key] = (groups[key] || 0) + (typeof value === 'number' ? value : 0);
563
- }
564
- return groups;
514
+ const clone = this.queryBuilder.clone();
515
+ clone._setGroupBy([this.groupByField]);
516
+ clone._setSelections({});
517
+ clone._setAggregate({ _result: { '$sum': field } });
518
+ const response = await clone.execute();
519
+ return this.buildGroupMap(response.records, '_result');
565
520
  }
566
521
  async avgBy(field) {
567
- const response = await this.queryBuilder.execute();
568
- if (!response.records)
569
- return {};
570
- const groups = {};
571
- for (const record of response.records) {
572
- const key = this.getGroupKey(record);
573
- const value = record[field];
574
- if (!groups[key]) {
575
- groups[key] = { sum: 0, count: 0 };
576
- }
577
- groups[key].sum += typeof value === 'number' ? value : 0;
578
- groups[key].count += 1;
579
- }
580
- const result = {};
581
- for (const [key, { sum, count }] of Object.entries(groups)) {
582
- result[key] = count > 0 ? sum / count : 0;
583
- }
584
- return result;
522
+ const clone = this.queryBuilder.clone();
523
+ clone._setGroupBy([this.groupByField]);
524
+ clone._setSelections({});
525
+ clone._setAggregate({ _result: { '$avg': field } });
526
+ const response = await clone.execute();
527
+ return this.buildGroupMap(response.records, '_result');
585
528
  }
586
529
  async maxBy(field) {
587
- const response = await this.queryBuilder.execute();
588
- if (!response.records)
589
- return {};
590
- const groups = {};
591
- for (const record of response.records) {
592
- const key = this.getGroupKey(record);
593
- const value = record[field];
594
- if (!(key in groups) || value > groups[key]) {
595
- groups[key] = value;
596
- }
597
- }
598
- return groups;
530
+ const clone = this.queryBuilder.clone();
531
+ clone._setGroupBy([this.groupByField]);
532
+ clone._setSelections({});
533
+ clone._setAggregate({ _result: { '$max': field } });
534
+ const response = await clone.execute();
535
+ return this.buildGroupMap(response.records, '_result');
599
536
  }
600
537
  async minBy(field) {
601
- const response = await this.queryBuilder.execute();
602
- if (!response.records)
603
- return {};
604
- const groups = {};
605
- for (const record of response.records) {
606
- const key = this.getGroupKey(record);
607
- const value = record[field];
608
- if (!(key in groups) || value < groups[key]) {
609
- groups[key] = value;
610
- }
611
- }
612
- return groups;
613
- }
614
- getGroupKey(record) {
615
- if (this.groupByField.includes('.')) {
616
- const parts = this.groupByField.split('.');
617
- let current = record;
618
- for (const part of parts) {
619
- current = current?.[part];
620
- }
621
- return String(current ?? 'null');
538
+ const clone = this.queryBuilder.clone();
539
+ clone._setGroupBy([this.groupByField]);
540
+ clone._setSelections({});
541
+ clone._setAggregate({ _result: { '$min': field } });
542
+ const response = await clone.execute();
543
+ return this.buildGroupMap(response.records, '_result');
544
+ }
545
+ async run(spec) {
546
+ const clone = this.queryBuilder.clone();
547
+ clone._setGroupBy([this.groupByField]);
548
+ clone._setSelections({});
549
+ clone._setAggregate(spec);
550
+ const response = await clone.execute();
551
+ return response.records ?? [];
552
+ }
553
+ buildGroupMap(records, aggKey) {
554
+ const result = {};
555
+ for (const record of records ?? []) {
556
+ const key = String(record[this.groupByField] ?? 'null');
557
+ result[key] = record[aggKey];
622
558
  }
623
- return String(record[this.groupByField] ?? 'null');
559
+ return result;
624
560
  }
625
561
  }
626
562
  exports.GroupByQueryBuilder = GroupByQueryBuilder;