@travetto/model-mongo 8.0.1 → 8.0.3

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.1",
3
+ "version": "8.0.3",
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.1",
33
- "@travetto/model": "^8.0.1",
34
- "@travetto/model-indexed": "^8.0.1",
35
- "@travetto/model-query": "^8.0.1",
32
+ "@travetto/config": "^8.0.3",
33
+ "@travetto/model": "^8.0.3",
34
+ "@travetto/model-indexed": "^8.0.3",
35
+ "@travetto/model-query": "^8.0.3",
36
36
  "mongodb": "^7.6.0"
37
37
  },
38
38
  "peerDependencies": {
39
- "@travetto/cli": "^8.0.1"
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,
@@ -632,14 +641,6 @@ export class MongoModelService
632
641
  return await Promise.all(items.map(item => this.postLoad(cls, item)));
633
642
  }
634
643
 
635
- async queryCount<T extends ModelType>(cls: Class<T>, query: ModelQuery<T>): Promise<number> {
636
- await QueryVerifier.verify(cls, query);
637
-
638
- const col = await this.getStore(cls);
639
- const filter = MongoUtil.extractWhereFilter(cls, query.where);
640
- return col.countDocuments(filter);
641
- }
642
-
643
644
  async queryOne<T extends ModelType>(cls: Class<T>, query: ModelQuery<T>, failOnMany = true): Promise<T> {
644
645
  const results = await this.query<T>(cls, { ...query, limit: failOnMany ? 2 : 1 });
645
646
  return ModelQueryUtil.verifyGetSingleCounts<T>(cls, failOnMany, results, query.where);
@@ -729,6 +730,61 @@ export class MongoModelService
729
730
  .toSorted((a, b) => b.count - a.count);
730
731
  }
731
732
 
733
+ static readonly AGGREGATE_OPERATION_MAPPING: Record<AggregateOperation, '$sum' | '$avg' | '$min' | '$max'> = {
734
+ sum: '$sum',
735
+ avg: '$avg',
736
+ min: '$min',
737
+ max: '$max'
738
+ };
739
+
740
+ // Aggregate
741
+ async aggregateFieldByQuery<
742
+ T extends ModelType,
743
+ Op extends AggregateOperation,
744
+ F extends (Op extends AggregateNumericOperation ? ValidNumericFields<T> : ValidComparableFields<T>)
745
+ >(modelClass: Class<T>, operation: Op, field: F, query?: ModelQuery<T>): Promise<AggregateResultType<T, F>> {
746
+ await QueryVerifier.verify(modelClass, query);
747
+
748
+ const collection = await this.getStore(modelClass);
749
+
750
+ const where = ModelQueryUtil.getWhereClause(modelClass, query?.where);
751
+ let queryObject: Record<string, unknown> = { [field]: { $exists: true, $ne: null } };
752
+
753
+ if (where) {
754
+ queryObject = { $and: [queryObject, MongoUtil.extractWhereFilter(modelClass, where)] };
755
+ }
756
+
757
+ const aggregateOperator = MongoModelService.AGGREGATE_OPERATION_MAPPING[operation];
758
+ if (!aggregateOperator) {
759
+ throw new RuntimeError(`Unsupported aggregate operation: ${operation}`);
760
+ }
761
+
762
+ const aggregations: object[] = [
763
+ { $match: queryObject },
764
+ {
765
+ $group: {
766
+ _id: null,
767
+ value: {
768
+ [aggregateOperator]: `$${String(field)}`
769
+ }
770
+ }
771
+ }
772
+ ];
773
+
774
+ const result = await collection.aggregate<{ _id: null; value: unknown }>(aggregations).toArray();
775
+ const rawValue = result.length ? result[0].value : undefined;
776
+
777
+ return ModelQueryAggregateUtil.resolveResult(modelClass, operation, field, rawValue);
778
+ }
779
+
780
+ async countByQuery<T extends ModelType>(cls: Class<T>, query: ModelQuery<T>): Promise<number> {
781
+ await QueryVerifier.verify(cls, query);
782
+
783
+ const col = await this.getStore(cls);
784
+ const filter = MongoUtil.extractWhereFilter(cls, query.where);
785
+ return col.countDocuments(filter);
786
+ }
787
+
732
788
  // Suggest
733
789
  async suggestValuesByQuery<T extends ModelType>(
734
790
  cls: Class<T>,