@travetto/model-mongo 8.0.0-alpha.15 → 8.0.0-alpha.17

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
@@ -20,7 +20,7 @@ Supported features:
20
20
  * [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10)
21
21
  * [Bulk](https://github.com/travetto/travetto/tree/main/module/model/src/types/bulk.ts#L64)
22
22
  * [Blob](https://github.com/travetto/travetto/tree/main/module/model/src/types/blob.ts#L8)
23
- * [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L15)
23
+ * [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L16)
24
24
  * [Query Crud](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/crud.ts#L11)
25
25
  * [Facet](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/facet.ts#L14)
26
26
  * [Query](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/query.ts#L10)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-mongo",
3
- "version": "8.0.0-alpha.15",
3
+ "version": "8.0.0-alpha.17",
4
4
  "type": "module",
5
5
  "description": "Mongo backing for the travetto model module.",
6
6
  "keywords": [
@@ -26,14 +26,14 @@
26
26
  "directory": "module/model-mongo"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/config": "^8.0.0-alpha.13",
30
- "@travetto/model": "^8.0.0-alpha.13",
31
- "@travetto/model-indexed": "^8.0.0-alpha.15",
32
- "@travetto/model-query": "^8.0.0-alpha.14",
29
+ "@travetto/config": "^8.0.0-alpha.15",
30
+ "@travetto/model": "^8.0.0-alpha.15",
31
+ "@travetto/model-indexed": "^8.0.0-alpha.17",
32
+ "@travetto/model-query": "^8.0.0-alpha.16",
33
33
  "mongodb": "^7.1.1"
34
34
  },
35
35
  "peerDependencies": {
36
- "@travetto/cli": "^8.0.0-alpha.18"
36
+ "@travetto/cli": "^8.0.0-alpha.20"
37
37
  },
38
38
  "peerDependenciesMeta": {
39
39
  "@travetto/cli": {
package/src/service.ts CHANGED
@@ -1,34 +1,28 @@
1
1
  import {
2
- type Binary,
3
- type Db, GridFSBucket, MongoClient, type GridFSFile, type Collection,
4
- type ObjectId, type RootFilterOperators, type Filter,
5
- type WithId as MongoWithId,
6
- type FindCursor,
2
+ type Binary, type Db, GridFSBucket, MongoClient, type GridFSFile, type Collection,
3
+ type ObjectId, type RootFilterOperators, type Filter, type WithId as MongoWithId, type FindCursor,
7
4
  MongoServerError,
8
5
  } from 'mongodb';
9
6
 
10
7
  import {
11
8
  ModelRegistryIndex, type ModelType, type OptionalId, type ModelCrudSupport, type ModelStorageSupport, type ModelExpirySupport,
12
9
  type ModelBulkSupport, type BulkOperation, type BulkResponse, NotFoundError, ExistsError, type ModelBlobSupport,
13
- ModelCrudUtil, ModelStorageUtil, ModelExpiryUtil, ModelBulkUtil,
14
- type ModelListOptions,
10
+ ModelCrudUtil, ModelStorageUtil, ModelExpiryUtil, ModelBulkUtil, type ModelListOptions,
15
11
  } from '@travetto/model';
16
12
  import {
17
13
  type ModelQuery, type ModelQueryCrudSupport, type ModelQueryFacetSupport, type ModelQuerySupport,
18
14
  type PageableModelQuery, type ValidStringFields, type WhereClause, type ModelQuerySuggestSupport,
19
- QueryVerifier, ModelQueryUtil, ModelQuerySuggestUtil, ModelQueryCrudUtil,
20
- type ModelQueryFacet,
15
+ QueryVerifier, ModelQueryUtil, ModelQuerySuggestUtil, ModelQueryCrudUtil, type ModelQueryFacet,
21
16
  } from '@travetto/model-query';
22
17
  import {
23
18
  type ModelIndexedSupport, type KeyedIndexSelection, type KeyedIndexBody, type ModelPageOptions, ModelIndexedUtil,
24
19
  type SingleItemIndex, type SortedIndexSelection, type ModelPageResult, type SortedIndex, type FullKeyedIndexBody,
25
- type FullKeyedIndexWithPartialBody, ModelIndexedComputedIndex,
20
+ type FullKeyedIndexWithPartialBody, ModelIndexedComputedIndex, type ModelIndexedSearchOptions, type SortedIndexSelectionType,
26
21
  } from '@travetto/model-indexed';
27
22
 
28
23
  import {
29
- ShutdownManager, type Class, TypedObject,
30
- castTo, asFull, type BinaryMetadata, type ByteRange, type BinaryType, BinaryUtil, BinaryMetadataUtil,
31
- JSONUtil,
24
+ ShutdownManager, type Class, TypedObject, castTo, asFull, type BinaryMetadata, type ByteRange, type BinaryType,
25
+ BinaryUtil, BinaryMetadataUtil, JSONUtil,
32
26
  } from '@travetto/runtime';
33
27
  import { Injectable, PostConstruct } from '@travetto/di';
34
28
 
@@ -87,20 +81,20 @@ export class MongoModelService implements
87
81
  }
88
82
  }
89
83
 
90
- async #buildIndexQuery<
91
- T extends ModelType,
92
- K extends KeyedIndexSelection<T>,
93
- S extends SortedIndexSelection<T>
94
- >(
84
+ async #buildIndexQuery<T extends ModelType>(
95
85
  cls: Class<T>,
96
- idx: SortedIndex<T, K, S>,
97
- body: KeyedIndexBody<T, K>
86
+ idx: SortedIndex<T>,
87
+ body: KeyedIndexBody<T>,
88
+ transformWhere?: (where: WhereClause<T>) => WhereClause<T>
98
89
  ): Promise<FindCursor> {
99
90
  const store = await this.getStore(cls);
100
91
  const computed = ModelIndexedComputedIndex.get(idx, body).validate();
92
+ let whereClause: WhereClause<T> = castTo(computed.project());
93
+ if (transformWhere) {
94
+ whereClause = transformWhere(whereClause);
95
+ }
101
96
 
102
- const where = this.getWhereFilter(cls, castTo(computed.project()));
103
-
97
+ const where = this.getWhereFilter(cls, whereClause);
104
98
  let q = store.find(where, { timeout: true })
105
99
  .batchSize(100);
106
100
 
@@ -549,6 +543,28 @@ export class MongoModelService implements
549
543
  yield* this.#iterateCursor(cls, cursor, options);
550
544
  }
551
545
 
546
+ async suggestByIndex<
547
+ T extends ModelType,
548
+ S extends SortedIndexSelection<T>,
549
+ K extends KeyedIndexSelection<T>,
550
+ B extends SortedIndexSelectionType<T, S> & string
551
+ >(
552
+ cls: Class<T>,
553
+ idx: SortedIndex<T, K, S>,
554
+ body: KeyedIndexBody<T, K>,
555
+ prefix: B,
556
+ options?: ModelIndexedSearchOptions
557
+ ): Promise<T[]> {
558
+ const cursor = (await this.#buildIndexQuery(cls, idx, body, (where) => castTo({
559
+ $and: [where, {
560
+ [idx.sortTemplate[0].path.join('.')]: ModelIndexedUtil.getSuggestRegex(prefix)
561
+ }]
562
+ })));
563
+ const batches = await Array.fromAsync(this.#iterateCursor(cls, cursor, { limit: 10, ...options }));
564
+
565
+ return batches.flat();
566
+ }
567
+
552
568
  // Query
553
569
  async query<T extends ModelType>(cls: Class<T>, query: PageableModelQuery<T>): Promise<T[]> {
554
570
  await QueryVerifier.verify(cls, query);
@@ -622,7 +638,7 @@ export class MongoModelService implements
622
638
  }
623
639
 
624
640
  // Facet
625
- async facet<T extends ModelType>(cls: Class<T>, field: ValidStringFields<T>, query?: ModelQuery<T>): Promise<ModelQueryFacet[]> {
641
+ async facetByQuery<T extends ModelType>(cls: Class<T>, field: ValidStringFields<T>, query?: ModelQuery<T>): Promise<ModelQueryFacet[]> {
626
642
  await QueryVerifier.verify(cls, query);
627
643
 
628
644
  const col = await this.getStore(cls);
@@ -659,14 +675,14 @@ export class MongoModelService implements
659
675
  }
660
676
 
661
677
  // Suggest
662
- async suggestValues<T extends ModelType>(cls: Class<T>, field: ValidStringFields<T>, prefix?: string, query?: PageableModelQuery<T>): Promise<string[]> {
678
+ async suggestValuesByQuery<T extends ModelType>(cls: Class<T>, field: ValidStringFields<T>, prefix?: string, query?: PageableModelQuery<T>): Promise<string[]> {
663
679
  await QueryVerifier.verify(cls, query);
664
680
  const resolvedQuery = ModelQuerySuggestUtil.getSuggestFieldQuery<T>(cls, field, prefix, query);
665
681
  const results = await this.query<T>(cls, resolvedQuery);
666
682
  return ModelQuerySuggestUtil.combineSuggestResults<T, string>(cls, field, prefix, results, (a) => a, query && query.limit);
667
683
  }
668
684
 
669
- async suggest<T extends ModelType>(cls: Class<T>, field: ValidStringFields<T>, prefix?: string, query?: PageableModelQuery<T>): Promise<T[]> {
685
+ async suggestByQuery<T extends ModelType>(cls: Class<T>, field: ValidStringFields<T>, prefix?: string, query?: PageableModelQuery<T>): Promise<T[]> {
670
686
  await QueryVerifier.verify(cls, query);
671
687
  const resolvedQuery = ModelQuerySuggestUtil.getSuggestQuery<T>(cls, field, prefix, query);
672
688
  const results = await this.query<T>(cls, resolvedQuery);