@travetto/model-memory 7.0.0-rc.1 → 7.0.0-rc.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/package.json +7 -7
- package/src/service.ts +23 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-memory",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.3",
|
|
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": "^7.0.0-rc.
|
|
29
|
-
"@travetto/di": "^7.0.0-rc.
|
|
30
|
-
"@travetto/model": "^7.0.0-rc.
|
|
31
|
-
"@travetto/schema": "^7.0.0-rc.
|
|
28
|
+
"@travetto/config": "^7.0.0-rc.3",
|
|
29
|
+
"@travetto/di": "^7.0.0-rc.3",
|
|
30
|
+
"@travetto/model": "^7.0.0-rc.3",
|
|
31
|
+
"@travetto/schema": "^7.0.0-rc.3"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@travetto/cli": "^7.0.0-rc.
|
|
35
|
-
"@travetto/test": "^7.0.0-rc.
|
|
34
|
+
"@travetto/cli": "^7.0.0-rc.3",
|
|
35
|
+
"@travetto/test": "^7.0.0-rc.3"
|
|
36
36
|
},
|
|
37
37
|
"peerDependenciesMeta": {
|
|
38
38
|
"@travetto/cli": {
|
package/src/service.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
2
|
import { buffer as toBuffer } from 'node:stream/consumers';
|
|
3
3
|
|
|
4
|
-
import { Class, TimeSpan, DeepPartial, castTo, BlobMeta, ByteRange, BinaryInput, BinaryUtil } from '@travetto/runtime';
|
|
4
|
+
import { Class, TimeSpan, DeepPartial, castTo, BlobMeta, ByteRange, BinaryInput, BinaryUtil, JSONUtil } from '@travetto/runtime';
|
|
5
5
|
import { Injectable } from '@travetto/di';
|
|
6
6
|
import { Config } from '@travetto/config';
|
|
7
7
|
import {
|
|
@@ -17,13 +17,13 @@ type StoreType = Map<string, Buffer>;
|
|
|
17
17
|
|
|
18
18
|
@Config('model.memory')
|
|
19
19
|
export class MemoryModelConfig {
|
|
20
|
-
|
|
20
|
+
modifyStorage?: boolean = true;
|
|
21
21
|
namespace?: string;
|
|
22
22
|
cullRate?: number | TimeSpan;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
function indexName<T extends ModelType>(cls: Class<T>, idx: IndexConfig<T> | string, suffix?: string): string {
|
|
26
|
-
return [cls.Ⲑid, typeof idx === 'string' ? idx : idx.name, suffix].filter(
|
|
26
|
+
return [cls.Ⲑid, typeof idx === 'string' ? idx : idx.name, suffix].filter(part => !!part).join(':');
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
function getFirstId(data: Map<string, unknown> | Set<string>, value?: string | number): string | undefined {
|
|
@@ -31,7 +31,7 @@ function getFirstId(data: Map<string, unknown> | Set<string>, value?: string | n
|
|
|
31
31
|
if (data instanceof Set) {
|
|
32
32
|
id = data.values().next().value;
|
|
33
33
|
} else {
|
|
34
|
-
id = [...data.entries()].find(([,
|
|
34
|
+
id = [...data.entries()].find(([, item]) => value === undefined || item === value)?.[0];
|
|
35
35
|
}
|
|
36
36
|
return id;
|
|
37
37
|
}
|
|
@@ -81,9 +81,9 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
81
81
|
const { key } = ModelIndexedUtil.computeIndexKey(cls, idx, castTo(item));
|
|
82
82
|
this.#indices[idx.type].get(idxName)?.get(key)?.delete(id);
|
|
83
83
|
}
|
|
84
|
-
} catch (
|
|
85
|
-
if (!(
|
|
86
|
-
throw
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (!(error instanceof NotFoundError)) {
|
|
86
|
+
throw error;
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
}
|
|
@@ -146,14 +146,14 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
async postConstruct(): Promise<void> {
|
|
149
|
-
await ModelStorageUtil.
|
|
149
|
+
await ModelStorageUtil.storageInitialization(this);
|
|
150
150
|
ModelExpiryUtil.registerCull(this);
|
|
151
151
|
|
|
152
|
-
for (const
|
|
153
|
-
for (const idx of ModelRegistryIndex.getConfig(
|
|
152
|
+
for (const cls of ModelRegistryIndex.getClasses()) {
|
|
153
|
+
for (const idx of ModelRegistryIndex.getConfig(cls).indices ?? []) {
|
|
154
154
|
switch (idx.type) {
|
|
155
155
|
case 'unique': {
|
|
156
|
-
console.error('Unique indices are not supported for', { cls:
|
|
156
|
+
console.error('Unique indices are not supported for', { cls: cls.Ⲑid, idx: idx.name });
|
|
157
157
|
break;
|
|
158
158
|
}
|
|
159
159
|
}
|
|
@@ -165,14 +165,14 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
165
165
|
async get<T extends ModelType>(cls: Class<T>, id: string): Promise<T> {
|
|
166
166
|
const store = this.#getStore(cls);
|
|
167
167
|
if (store.has(id)) {
|
|
168
|
-
const
|
|
169
|
-
if (
|
|
168
|
+
const result = await ModelCrudUtil.load(cls, store.get(id)!);
|
|
169
|
+
if (result) {
|
|
170
170
|
if (ModelRegistryIndex.getConfig(cls).expiresAt) {
|
|
171
|
-
if (!ModelExpiryUtil.getExpiryState(cls,
|
|
172
|
-
return
|
|
171
|
+
if (!ModelExpiryUtil.getExpiryState(cls, result).expired) {
|
|
172
|
+
return result;
|
|
173
173
|
}
|
|
174
174
|
} else {
|
|
175
|
-
return
|
|
175
|
+
return result;
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
}
|
|
@@ -221,9 +221,9 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
221
221
|
for (const id of this.#getStore(cls).keys()) {
|
|
222
222
|
try {
|
|
223
223
|
yield await this.get(cls, id);
|
|
224
|
-
} catch (
|
|
225
|
-
if (!(
|
|
226
|
-
throw
|
|
224
|
+
} catch (error) {
|
|
225
|
+
if (!(error instanceof NotFoundError)) {
|
|
226
|
+
throw error;
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
229
|
}
|
|
@@ -255,7 +255,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
255
255
|
|
|
256
256
|
async getBlobMeta(location: string): Promise<BlobMeta> {
|
|
257
257
|
const metaContent = this.#find(ModelBlobMetaNamespace, location, 'notfound');
|
|
258
|
-
const meta: BlobMeta =
|
|
258
|
+
const meta: BlobMeta = JSONUtil.parseSafe(metaContent.get(location)!);
|
|
259
259
|
return meta;
|
|
260
260
|
}
|
|
261
261
|
|
|
@@ -281,8 +281,8 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
281
281
|
let deleted = 0;
|
|
282
282
|
for (const key of [...store.keys()]) {
|
|
283
283
|
try {
|
|
284
|
-
const
|
|
285
|
-
if (ModelExpiryUtil.getExpiryState(cls,
|
|
284
|
+
const result = await ModelCrudUtil.load(cls, store.get(key)!);
|
|
285
|
+
if (ModelExpiryUtil.getExpiryState(cls, result).expired) {
|
|
286
286
|
store.delete(key);
|
|
287
287
|
deleted += 1;
|
|
288
288
|
}
|
|
@@ -301,7 +301,7 @@ export class MemoryModelService implements ModelCrudSupport, ModelBlobSupport, M
|
|
|
301
301
|
this.#indices.unsorted.clear();
|
|
302
302
|
}
|
|
303
303
|
|
|
304
|
-
async
|
|
304
|
+
async upsertModel<T extends ModelType>(cls: Class<T>): Promise<void> {
|
|
305
305
|
for (const idx of ModelRegistryIndex.getConfig(cls).indices ?? []) {
|
|
306
306
|
if (idx.type === 'sorted' || idx.type === 'unsorted') {
|
|
307
307
|
this.#indices[idx.type].set(indexName(cls, idx), new Map());
|