@travetto/model-memory 5.0.14 → 5.0.16

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 (2) hide show
  1. package/package.json +7 -7
  2. package/src/service.ts +14 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-memory",
3
- "version": "5.0.14",
3
+ "version": "5.0.16",
4
4
  "description": "Memory backing for the travetto model module.",
5
5
  "keywords": [
6
6
  "datastore",
@@ -25,14 +25,14 @@
25
25
  "directory": "module/model-memory"
26
26
  },
27
27
  "dependencies": {
28
- "@travetto/config": "^5.0.13",
29
- "@travetto/di": "^5.0.13",
30
- "@travetto/model": "^5.0.14",
31
- "@travetto/schema": "^5.0.13"
28
+ "@travetto/config": "^5.0.15",
29
+ "@travetto/di": "^5.0.15",
30
+ "@travetto/model": "^5.0.16",
31
+ "@travetto/schema": "^5.0.15"
32
32
  },
33
33
  "peerDependencies": {
34
- "@travetto/cli": "^5.0.16",
35
- "@travetto/test": "^5.0.15"
34
+ "@travetto/cli": "^5.0.18",
35
+ "@travetto/test": "^5.0.17"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "@travetto/cli": {
package/src/service.ts CHANGED
@@ -65,7 +65,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
65
65
  return this.#store.get(key)!;
66
66
  }
67
67
 
68
- #find<T extends ModelType>(cls: Class<T> | string, id?: string, errorState?: 'data' | 'notfound'): StoreType {
68
+ #find<T extends ModelType>(cls: Class<T> | string, id?: string, errorState?: 'exists' | 'notfound'): StoreType {
69
69
  const store = this.#getStore(cls);
70
70
 
71
71
  if (id && errorState && (errorState === 'notfound' ? !store.has(id) : store.has(id))) {
@@ -185,7 +185,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
185
185
  if (!item.id) {
186
186
  item.id = this.idSource.create();
187
187
  }
188
- this.#find(cls, item.id, 'data');
188
+ this.#find(cls, item.id, 'exists');
189
189
  return await this.upsert(cls, item);
190
190
  }
191
191
 
@@ -279,7 +279,18 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
279
279
 
280
280
  // Expiry
281
281
  async deleteExpired<T extends ModelType>(cls: Class<T>): Promise<number> {
282
- return ModelExpiryUtil.naiveDeleteExpired(this, cls);
282
+ const store = this.#getStore(cls);
283
+ let deleted = 0;
284
+ for (const key of [...store.keys()]) {
285
+ try {
286
+ const res = await ModelCrudUtil.load(cls, store.get(key)!);
287
+ if (ModelExpiryUtil.getExpiryState(cls, res).expired) {
288
+ store.delete(key);
289
+ deleted += 1;
290
+ }
291
+ } catch { } // Do not let a single error stop the process
292
+ }
293
+ return deleted;
283
294
  }
284
295
 
285
296
  // Storage Support
@@ -292,7 +303,6 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
292
303
  this.#indices.unsorted.clear();
293
304
  }
294
305
 
295
-
296
306
  async createModel<T extends ModelType>(cls: Class<T>): Promise<void> {
297
307
  for (const idx of ModelRegistry.get(cls).indices ?? []) {
298
308
  if (idx.type === 'sorted' || idx.type === 'unsorted') {