@travetto/model-mongo 8.0.0 → 8.0.2

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.
package/README.md CHANGED
@@ -21,6 +21,7 @@ Supported features:
21
21
  * [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L10)
22
22
  * [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10)
23
23
  * [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L21)
24
+ * [Aggregate](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/aggregate.ts#L30)
24
25
  * [Query Crud](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/crud.ts#L11)
25
26
  * [Facet](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/facet.ts#L14)
26
27
  * [Suggest](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/suggest.ts#L12)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-mongo",
3
- "version": "8.0.0",
3
+ "version": "8.0.2",
4
4
  "description": "Mongo backing for the travetto model module.",
5
5
  "keywords": [
6
6
  "database",
@@ -29,14 +29,14 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@travetto/config": "^8.0.0",
33
- "@travetto/model": "^8.0.0",
34
- "@travetto/model-indexed": "^8.0.0",
35
- "@travetto/model-query": "^8.0.0",
36
- "mongodb": "^7.5.0"
32
+ "@travetto/config": "^8.0.2",
33
+ "@travetto/model": "^8.0.2",
34
+ "@travetto/model-indexed": "^8.0.2",
35
+ "@travetto/model-query": "^8.0.2",
36
+ "mongodb": "^7.6.0"
37
37
  },
38
38
  "peerDependencies": {
39
- "@travetto/cli": "^8.0.0"
39
+ "@travetto/cli": "^8.0.2"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@travetto/cli": {
package/src/service.ts CHANGED
@@ -51,7 +51,12 @@ import {
51
51
  type SortedIndexSelectionType
52
52
  } from '@travetto/model-indexed';
53
53
  import {
54
+ type AggregateNumericOperation,
55
+ type AggregateOperation,
56
+ type AggregateResultType,
54
57
  type ModelQuery,
58
+ type ModelQueryAggregateSupport,
59
+ ModelQueryAggregateUtil,
55
60
  type ModelQueryCrudSupport,
56
61
  ModelQueryCrudUtil,
57
62
  type ModelQueryFacet,
@@ -62,6 +67,8 @@ import {
62
67
  ModelQueryUtil,
63
68
  type PageableModelQuery,
64
69
  QueryVerifier,
70
+ type ValidComparableFields,
71
+ type ValidNumericFields,
65
72
  type ValidStringFields,
66
73
  type WhereClause
67
74
  } from '@travetto/model-query';
@@ -75,6 +82,7 @@ import {
75
82
  type Class,
76
83
  castTo,
77
84
  JSONUtil,
85
+ RuntimeError,
78
86
  ShutdownManager,
79
87
  TypedObject
80
88
  } from '@travetto/runtime';
@@ -112,6 +120,7 @@ export class MongoModelService
112
120
  ModelBlobSupport,
113
121
  ModelIndexedSupport,
114
122
  ModelQuerySupport,
123
+ ModelQueryAggregateSupport,
115
124
  ModelQueryCrudSupport,
116
125
  ModelQueryFacetSupport,
117
126
  ModelQuerySuggestSupport,
@@ -729,6 +738,53 @@ export class MongoModelService
729
738
  .toSorted((a, b) => b.count - a.count);
730
739
  }
731
740
 
741
+ static readonly AGGREGATE_OPERATION_MAPPING: Record<AggregateOperation, '$sum' | '$avg' | '$min' | '$max'> = {
742
+ sum: '$sum',
743
+ avg: '$avg',
744
+ min: '$min',
745
+ max: '$max'
746
+ };
747
+
748
+ // Aggregate
749
+ async aggregateFieldByQuery<
750
+ T extends ModelType,
751
+ Op extends AggregateOperation,
752
+ F extends (Op extends AggregateNumericOperation ? ValidNumericFields<T> : ValidComparableFields<T>)
753
+ >(modelClass: Class<T>, operation: Op, field: F, query?: ModelQuery<T>): Promise<AggregateResultType<T, F>> {
754
+ await QueryVerifier.verify(modelClass, query);
755
+
756
+ const collection = await this.getStore(modelClass);
757
+
758
+ const where = ModelQueryUtil.getWhereClause(modelClass, query?.where);
759
+ let queryObject: Record<string, unknown> = { [field]: { $exists: true, $ne: null } };
760
+
761
+ if (where) {
762
+ queryObject = { $and: [queryObject, MongoUtil.extractWhereFilter(modelClass, where)] };
763
+ }
764
+
765
+ const aggregateOperator = MongoModelService.AGGREGATE_OPERATION_MAPPING[operation];
766
+ if (!aggregateOperator) {
767
+ throw new RuntimeError(`Unsupported aggregate operation: ${operation}`);
768
+ }
769
+
770
+ const aggregations: object[] = [
771
+ { $match: queryObject },
772
+ {
773
+ $group: {
774
+ _id: null,
775
+ value: {
776
+ [aggregateOperator]: `$${String(field)}`
777
+ }
778
+ }
779
+ }
780
+ ];
781
+
782
+ const result = await collection.aggregate<{ _id: null; value: unknown }>(aggregations).toArray();
783
+ const rawValue = result.length ? result[0].value : undefined;
784
+
785
+ return ModelQueryAggregateUtil.resolveResult(modelClass, operation, field, rawValue);
786
+ }
787
+
732
788
  // Suggest
733
789
  async suggestValuesByQuery<T extends ModelType>(
734
790
  cls: Class<T>,