@travetto/model-memory 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
@@ -17,4 +17,4 @@ This module provides a memory-based implementation for the [Data Modeling Suppor
17
17
  * [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L11)
18
18
  * [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10)
19
19
  * [Blob](https://github.com/travetto/travetto/tree/main/module/model/src/types/blob.ts#L8)
20
- * [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L15)
20
+ * [Indexed](https://github.com/travetto/travetto/tree/main/module/model-indexed/src/types/service.ts#L16)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-memory",
3
- "version": "8.0.0-alpha.15",
3
+ "version": "8.0.0-alpha.17",
4
4
  "type": "module",
5
5
  "description": "Memory backing for the travetto model module.",
6
6
  "keywords": [
@@ -26,15 +26,15 @@
26
26
  "directory": "module/model-memory"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/config": "^8.0.0-alpha.13",
30
- "@travetto/di": "^8.0.0-alpha.12",
31
- "@travetto/model": "^8.0.0-alpha.13",
32
- "@travetto/model-indexed": "^8.0.0-alpha.15",
33
- "@travetto/schema": "^8.0.0-alpha.13"
29
+ "@travetto/config": "^8.0.0-alpha.15",
30
+ "@travetto/di": "^8.0.0-alpha.14",
31
+ "@travetto/model": "^8.0.0-alpha.15",
32
+ "@travetto/model-indexed": "^8.0.0-alpha.17",
33
+ "@travetto/schema": "^8.0.0-alpha.15"
34
34
  },
35
35
  "peerDependencies": {
36
- "@travetto/cli": "^8.0.0-alpha.18",
37
- "@travetto/test": "^8.0.0-alpha.12"
36
+ "@travetto/cli": "^8.0.0-alpha.20",
37
+ "@travetto/test": "^8.0.0-alpha.14"
38
38
  },
39
39
  "peerDependenciesMeta": {
40
40
  "@travetto/cli": {
package/src/service.ts CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  type ModelIndexedSupport, type KeyedIndexSelection, type KeyedIndexBody, type ModelPageOptions, ModelIndexedUtil,
15
15
  type SingleItemIndex, type SortedIndexSelection, type ModelPageResult, type SortedIndex,
16
16
  type AllIndexes, isModelIndexedIndex, type FullKeyedIndexBody, type FullKeyedIndexWithPartialBody, ModelIndexedComputedIndex,
17
+ type ModelIndexedSearchOptions, type SortedIndexSelectionType,
17
18
  } from '@travetto/model-indexed';
18
19
 
19
20
  const ModelBlobNamespace = '__blobs';
@@ -21,6 +22,10 @@ const ModelBlobMetaNamespace = `${ModelBlobNamespace}_meta`;
21
22
 
22
23
  type StoreType = Map<string, BinaryArray>;
23
24
 
25
+ const sortValue = (a: string | number, b: string | number): number =>
26
+ (typeof a === 'number' && typeof b === 'number') ?
27
+ a - b : (typeof a === 'string' && typeof b === 'string') ? a.localeCompare(b) : 0;
28
+
24
29
  @Config('model.memory')
25
30
  export class MemoryModelConfig {
26
31
  modifyStorage?: boolean = true;
@@ -53,7 +58,7 @@ export class MemoryModelService implements
53
58
 
54
59
  #store = new Map<string, StoreType>();
55
60
  #indices = {
56
- 'indexed:sorted': new Map<string, Map<string, Map<string, number>>>(),
61
+ 'indexed:sorted': new Map<string, Map<string, Map<string, number | string>>>(),
57
62
  'indexed:keyed': new Map<string, Map<string, Set<string>>>(),
58
63
  } as const;
59
64
 
@@ -171,10 +176,9 @@ export class MemoryModelService implements
171
176
  throw new NotFoundError(cls, computed.getKey({ sort: true }));
172
177
  }
173
178
 
174
- async * #iterateIds<T extends ModelType>(
179
+ async * #iterateIds(
175
180
  ids: string[],
176
- options?: ModelListOptions & ModelPageOptions<number>
177
-
181
+ options?: ModelListOptions & ModelPageOptions<number>,
178
182
  ): AsyncIterable<string[]> {
179
183
  let offset = options && 'offset' in options ? options.offset ?? 0 : 0;
180
184
  const batchSize = options?.batchSizeHint ?? 100;
@@ -196,13 +200,12 @@ export class MemoryModelService implements
196
200
 
197
201
  async * #getIndexIds<
198
202
  T extends ModelType,
199
- K extends KeyedIndexSelection<T>,
200
- S extends SortedIndexSelection<T>
201
203
  >(
202
204
  cls: Class<T>,
203
- idx: AllIndexes<T, K, S>,
204
- body: KeyedIndexBody<T, K>,
205
- options?: ModelListOptions & ModelPageOptions<number>
205
+ idx: AllIndexes<T>,
206
+ body: KeyedIndexBody<T>,
207
+ options?: ModelListOptions & ModelPageOptions<number>,
208
+ filterIndex?: (indexValue: string | number) => boolean
206
209
  ): AsyncIterable<string[]> {
207
210
  const computed = ModelIndexedComputedIndex.get(idx, body).validate();
208
211
  if (!isModelIndexedIndex(idx)) {
@@ -215,10 +218,14 @@ export class MemoryModelService implements
215
218
  if (!index) {
216
219
  ids = [];
217
220
  } else if (index instanceof Map) {
218
- ids = [...index.entries()].toSorted((a, b) => a[1] - b[1]).map(([id,]) => id);
221
+ ids = [...index.entries()]
222
+ .filter(([, sort]) => filterIndex ? filterIndex(sort) : true)
223
+ .sort((a, b) => sortValue(a[1], b[1]))
224
+ .map(([id,]) => id);
219
225
  } else {
220
226
  ids = [...index];
221
227
  }
228
+
222
229
  yield* this.#iterateIds(ids, options);
223
230
  }
224
231
 
@@ -468,4 +475,20 @@ export class MemoryModelService implements
468
475
  yield ModelCrudUtil.filterOutNotFound(batch.map(id => this.get(cls, id)));
469
476
  }
470
477
  }
478
+
479
+ async suggestByIndex<
480
+ T extends ModelType,
481
+ S extends SortedIndexSelection<T>,
482
+ K extends KeyedIndexSelection<T>,
483
+ B extends SortedIndexSelectionType<T, S> & string
484
+ >(cls: Class<T>, idx: SortedIndex<T, K, S>, body: KeyedIndexBody<T, K>, prefix: B, options?: ModelIndexedSearchOptions): Promise<T[]> {
485
+ const items: T[] = [];
486
+ for await (const batch of this.#getIndexIds(
487
+ cls, idx, body, options,
488
+ id => (typeof id === 'string' && id.startsWith(prefix))
489
+ )) {
490
+ items.push(...await ModelCrudUtil.filterOutNotFound(batch.map(id => this.get(cls, id))));
491
+ }
492
+ return items;
493
+ }
471
494
  }