@eventvisor/sdk 0.0.2
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/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/attributesManager.d.ts +36 -0
- package/dist/bucketer.d.ts +30 -0
- package/dist/compareVersions.d.ts +4 -0
- package/dist/conditions.d.ts +20 -0
- package/dist/datafileReader.d.ts +29 -0
- package/dist/effectsManager.d.ts +33 -0
- package/dist/emitter.d.ts +11 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.gz +0 -0
- package/dist/index.mjs.map +1 -0
- package/dist/instance.d.ts +67 -0
- package/dist/logger.d.ts +26 -0
- package/dist/modulesManager.d.ts +67 -0
- package/dist/murmurhash.d.ts +1 -0
- package/dist/persister.d.ts +40 -0
- package/dist/sourceResolver.d.ts +31 -0
- package/dist/transformer.d.ts +21 -0
- package/dist/validator.d.ts +28 -0
- package/jest.config.js +6 -0
- package/lib/attributesManager.d.ts +36 -0
- package/lib/bucketer.d.ts +30 -0
- package/lib/compareVersions.d.ts +4 -0
- package/lib/conditions.d.ts +20 -0
- package/lib/datafileReader.d.ts +29 -0
- package/lib/effectsManager.d.ts +33 -0
- package/lib/emitter.d.ts +11 -0
- package/lib/index.d.ts +12 -0
- package/lib/instance.d.ts +67 -0
- package/lib/logger.d.ts +26 -0
- package/lib/modulesManager.d.ts +67 -0
- package/lib/murmurhash.d.ts +1 -0
- package/lib/persister.d.ts +40 -0
- package/lib/sourceResolver.d.ts +31 -0
- package/lib/transformer.d.ts +21 -0
- package/lib/validator.d.ts +28 -0
- package/package.json +45 -0
- package/src/attributesManager.ts +181 -0
- package/src/bucketer.spec.ts +156 -0
- package/src/bucketer.ts +152 -0
- package/src/compareVersions.ts +93 -0
- package/src/conditions.ts +224 -0
- package/src/datafileReader.ts +133 -0
- package/src/effectsManager.ts +214 -0
- package/src/emitter.ts +64 -0
- package/src/index.spec.ts +5 -0
- package/src/index.ts +14 -0
- package/src/instance.spec.ts +184 -0
- package/src/instance.ts +608 -0
- package/src/logger.ts +90 -0
- package/src/modulesManager.ts +276 -0
- package/src/murmurhash.ts +71 -0
- package/src/persister.ts +162 -0
- package/src/sourceResolver.spec.ts +253 -0
- package/src/sourceResolver.ts +213 -0
- package/src/transformer.ts +316 -0
- package/src/transformer_static.spec.ts +377 -0
- package/src/transformer_types.spec.ts +820 -0
- package/src/validator.spec.ts +579 -0
- package/src/validator.ts +366 -0
- package/tsconfig.cjs.json +8 -0
- package/tsconfig.esm.json +8 -0
- package/webpack.config.js +80 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Value,
|
|
3
|
+
Effect,
|
|
4
|
+
Step,
|
|
5
|
+
EventName,
|
|
6
|
+
DestinationName,
|
|
7
|
+
EffectName,
|
|
8
|
+
EventLevel,
|
|
9
|
+
} from "@eventvisor/types";
|
|
10
|
+
|
|
11
|
+
import type { Logger } from "./logger";
|
|
12
|
+
import type { DatafileReader } from "./datafileReader";
|
|
13
|
+
import type { SourceResolver } from "./sourceResolver";
|
|
14
|
+
|
|
15
|
+
export type ModuleName = string;
|
|
16
|
+
|
|
17
|
+
export interface LookupOptions {
|
|
18
|
+
key: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// export interface TransformOptions {
|
|
22
|
+
// key: string;
|
|
23
|
+
// value: Value;
|
|
24
|
+
// }
|
|
25
|
+
|
|
26
|
+
export interface HandleOptions {
|
|
27
|
+
effectName: EffectName;
|
|
28
|
+
effect: Effect;
|
|
29
|
+
step: Step;
|
|
30
|
+
payload: Value;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface TransportOptions {
|
|
34
|
+
destinationName: DestinationName;
|
|
35
|
+
eventName: EventName;
|
|
36
|
+
eventLevel?: EventLevel;
|
|
37
|
+
payload: Value; // @TODO: rename to body?
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ReadFromStorageOptions {
|
|
41
|
+
key: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface WriteToStorageOptions {
|
|
45
|
+
key: string;
|
|
46
|
+
value: Value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface RemoveFromStorageOptions {
|
|
50
|
+
key: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ModuleDependencies {
|
|
54
|
+
datafileReader: DatafileReader;
|
|
55
|
+
logger: Logger;
|
|
56
|
+
sourceResolver: SourceResolver;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface Module {
|
|
60
|
+
name: ModuleName;
|
|
61
|
+
|
|
62
|
+
// initialize?
|
|
63
|
+
|
|
64
|
+
lookup?: (options: LookupOptions, deps: ModuleDependencies) => Promise<Value>;
|
|
65
|
+
|
|
66
|
+
// transform?: (options: TransformOptions, deps: ModuleDependencies) => Promise<Value>;
|
|
67
|
+
|
|
68
|
+
handle?: (options: HandleOptions, deps: ModuleDependencies) => Promise<void>;
|
|
69
|
+
|
|
70
|
+
transport?: (options: TransportOptions, deps: ModuleDependencies) => Promise<void>;
|
|
71
|
+
|
|
72
|
+
readFromStorage?: (options: ReadFromStorageOptions, deps: ModuleDependencies) => Promise<Value>;
|
|
73
|
+
writeToStorage?: (options: WriteToStorageOptions, deps: ModuleDependencies) => Promise<void>;
|
|
74
|
+
removeFromStorage?: (
|
|
75
|
+
options: RemoveFromStorageOptions,
|
|
76
|
+
deps: ModuleDependencies,
|
|
77
|
+
) => Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface ModulesManagerOptions {
|
|
81
|
+
logger: Logger;
|
|
82
|
+
getDatafileReader: () => DatafileReader;
|
|
83
|
+
getSourceResolver: () => SourceResolver;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class ModulesManager {
|
|
87
|
+
private logger: Logger;
|
|
88
|
+
private getDatafileReader: () => DatafileReader;
|
|
89
|
+
private getSourceResolver: () => SourceResolver;
|
|
90
|
+
|
|
91
|
+
// @TODO: can be optimized further by keeping only array of names, but keeping actual modules in an object
|
|
92
|
+
private modules: Module[];
|
|
93
|
+
|
|
94
|
+
constructor(options: ModulesManagerOptions) {
|
|
95
|
+
const { logger, getDatafileReader, getSourceResolver } = options;
|
|
96
|
+
|
|
97
|
+
this.logger = logger;
|
|
98
|
+
this.getDatafileReader = getDatafileReader;
|
|
99
|
+
this.getSourceResolver = getSourceResolver;
|
|
100
|
+
this.modules = [];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
registerModule(module: Module) {
|
|
104
|
+
if (this.modules.find((m) => m.name === module.name)) {
|
|
105
|
+
this.logger.error(`Module ${module.name} already registered`);
|
|
106
|
+
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
this.modules.push(module);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getModule(name: string) {
|
|
114
|
+
return this.modules.find((module) => module.name === name);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
removeModule(name: string) {
|
|
118
|
+
const module = this.getModule(name);
|
|
119
|
+
|
|
120
|
+
if (!module) {
|
|
121
|
+
this.logger.error(`Module ${name} not found`);
|
|
122
|
+
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.modules = this.modules.filter((module) => module.name !== name);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
getModuleDependencies(): ModuleDependencies {
|
|
130
|
+
return {
|
|
131
|
+
datafileReader: this.getDatafileReader(),
|
|
132
|
+
logger: this.logger,
|
|
133
|
+
sourceResolver: this.getSourceResolver(),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async lookup(fullKey: string): Promise<Value> {
|
|
138
|
+
const [moduleName, ...keyParts] = fullKey.split(".");
|
|
139
|
+
const key = keyParts.join(".");
|
|
140
|
+
|
|
141
|
+
const moduleInstance = this.getModule(moduleName);
|
|
142
|
+
|
|
143
|
+
if (moduleInstance && moduleInstance.lookup) {
|
|
144
|
+
try {
|
|
145
|
+
return await moduleInstance.lookup({ key }, this.getModuleDependencies());
|
|
146
|
+
} catch (error) {
|
|
147
|
+
this.logger.error(`Error in lookup`, { moduleName, key, error });
|
|
148
|
+
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this.logger.error(`Module "${moduleName}" not found with "lookup" function`);
|
|
154
|
+
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async handle(
|
|
159
|
+
fullKey: string,
|
|
160
|
+
effectName: EffectName,
|
|
161
|
+
effect: Effect,
|
|
162
|
+
step: Step,
|
|
163
|
+
payload: Value,
|
|
164
|
+
): Promise<void> {
|
|
165
|
+
const [moduleName, key] = fullKey.split("."); // eslint-disable-line
|
|
166
|
+
|
|
167
|
+
const moduleInstance = this.getModule(moduleName);
|
|
168
|
+
|
|
169
|
+
if (moduleInstance && moduleInstance.handle) {
|
|
170
|
+
try {
|
|
171
|
+
return await moduleInstance.handle(
|
|
172
|
+
{ effectName, effect, step, payload },
|
|
173
|
+
this.getModuleDependencies(),
|
|
174
|
+
);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
this.logger.error(`Error in handle`, { moduleName, effectName, error });
|
|
177
|
+
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
this.logger.error(`Module "${moduleName}" not found with "handle" function`);
|
|
183
|
+
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
transportExists(fullKey: string): boolean {
|
|
188
|
+
const [moduleName, key] = fullKey.split("."); // eslint-disable-line
|
|
189
|
+
|
|
190
|
+
const moduleInstance = this.getModule(moduleName);
|
|
191
|
+
|
|
192
|
+
return !!(moduleInstance && moduleInstance.transport);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// @TODO: change multiple args to single options object
|
|
196
|
+
async transport(
|
|
197
|
+
fullKey: string,
|
|
198
|
+
destinationName: DestinationName,
|
|
199
|
+
eventName: EventName,
|
|
200
|
+
payload: Value,
|
|
201
|
+
eventLevel?: EventLevel,
|
|
202
|
+
): Promise<void> {
|
|
203
|
+
const [moduleName, key] = fullKey.split("."); // eslint-disable-line
|
|
204
|
+
|
|
205
|
+
const moduleInstance = this.getModule(moduleName);
|
|
206
|
+
|
|
207
|
+
if (moduleInstance && moduleInstance.transport) {
|
|
208
|
+
try {
|
|
209
|
+
return await moduleInstance.transport(
|
|
210
|
+
{ destinationName, eventName, eventLevel, payload },
|
|
211
|
+
this.getModuleDependencies(),
|
|
212
|
+
);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
this.logger.error(`Error in transport`, { moduleName, destinationName, eventName, error });
|
|
215
|
+
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
this.logger.error(`Module "${moduleName}" not found with "transport" function`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async readFromStorage(moduleName: ModuleName, key: string): Promise<Value> {
|
|
224
|
+
const moduleInstance = this.getModule(moduleName);
|
|
225
|
+
|
|
226
|
+
if (moduleInstance && moduleInstance.readFromStorage) {
|
|
227
|
+
try {
|
|
228
|
+
return await moduleInstance.readFromStorage({ key }, this.getModuleDependencies());
|
|
229
|
+
} catch (error) {
|
|
230
|
+
this.logger.error(`Error in readFromStorage`, { moduleName, key, error });
|
|
231
|
+
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
this.logger.error(`Module "${moduleName}" not found with "readFromStorage" function`);
|
|
237
|
+
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async writeToStorage(moduleName: ModuleName, key: string, value: Value): Promise<void> {
|
|
242
|
+
const moduleInstance = this.getModule(moduleName);
|
|
243
|
+
|
|
244
|
+
if (moduleInstance && moduleInstance.writeToStorage) {
|
|
245
|
+
try {
|
|
246
|
+
return await moduleInstance.writeToStorage({ key, value }, this.getModuleDependencies());
|
|
247
|
+
} catch (error) {
|
|
248
|
+
this.logger.error(`Error in writeToStorage`, { moduleName, key, value, error });
|
|
249
|
+
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
this.logger.error(`Module "${moduleName}" not found with "writeToStorage" function`);
|
|
255
|
+
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async removeFromStorage(moduleName: ModuleName, key: string): Promise<void> {
|
|
260
|
+
const moduleInstance = this.getModule(moduleName);
|
|
261
|
+
|
|
262
|
+
if (moduleInstance && moduleInstance.removeFromStorage) {
|
|
263
|
+
try {
|
|
264
|
+
return await moduleInstance.removeFromStorage({ key }, this.getModuleDependencies());
|
|
265
|
+
} catch (error) {
|
|
266
|
+
this.logger.error(`Error in removeFromStorage`, { moduleName, key, error });
|
|
267
|
+
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
this.logger.error(`Module "${moduleName}" not found with "removeFromStorage" function`);
|
|
273
|
+
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// v3 function taken from: https://github.com/perezd/node-murmurhash
|
|
2
|
+
// this has been done to avoid loading v2 function which is not used in the SDK
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Copyright (c) 2020 Gary Court, Derek Perez
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
8
|
+
*
|
|
9
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
10
|
+
*
|
|
11
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const createBuffer = (val) => new TextEncoder().encode(val);
|
|
15
|
+
|
|
16
|
+
export function MurmurHashV3(key, seed) {
|
|
17
|
+
if (typeof key === "string") key = createBuffer(key);
|
|
18
|
+
|
|
19
|
+
let remainder, bytes, h1, h1b, c1, c2, k1, i;
|
|
20
|
+
|
|
21
|
+
remainder = key.length & 3; // key.length % 4
|
|
22
|
+
bytes = key.length - remainder;
|
|
23
|
+
h1 = seed;
|
|
24
|
+
c1 = 0xcc9e2d51;
|
|
25
|
+
c2 = 0x1b873593;
|
|
26
|
+
i = 0;
|
|
27
|
+
|
|
28
|
+
while (i < bytes) {
|
|
29
|
+
k1 =
|
|
30
|
+
(key[i] & 0xff) |
|
|
31
|
+
((key[++i] & 0xff) << 8) |
|
|
32
|
+
((key[++i] & 0xff) << 16) |
|
|
33
|
+
((key[++i] & 0xff) << 24);
|
|
34
|
+
++i;
|
|
35
|
+
|
|
36
|
+
k1 = ((k1 & 0xffff) * c1 + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
|
|
37
|
+
k1 = (k1 << 15) | (k1 >>> 17);
|
|
38
|
+
k1 = ((k1 & 0xffff) * c2 + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
|
|
39
|
+
|
|
40
|
+
h1 ^= k1;
|
|
41
|
+
h1 = (h1 << 13) | (h1 >>> 19);
|
|
42
|
+
h1b = ((h1 & 0xffff) * 5 + ((((h1 >>> 16) * 5) & 0xffff) << 16)) & 0xffffffff;
|
|
43
|
+
h1 = (h1b & 0xffff) + 0x6b64 + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
k1 = 0;
|
|
47
|
+
|
|
48
|
+
switch (remainder) {
|
|
49
|
+
case 3:
|
|
50
|
+
k1 ^= (key[i + 2] & 0xff) << 16;
|
|
51
|
+
case 2:
|
|
52
|
+
k1 ^= (key[i + 1] & 0xff) << 8;
|
|
53
|
+
case 1:
|
|
54
|
+
k1 ^= key[i] & 0xff;
|
|
55
|
+
|
|
56
|
+
k1 = ((k1 & 0xffff) * c1 + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
|
|
57
|
+
k1 = (k1 << 15) | (k1 >>> 17);
|
|
58
|
+
k1 = ((k1 & 0xffff) * c2 + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
|
|
59
|
+
h1 ^= k1;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
h1 ^= key.length;
|
|
63
|
+
|
|
64
|
+
h1 ^= h1 >>> 16;
|
|
65
|
+
h1 = ((h1 & 0xffff) * 0x85ebca6b + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
|
|
66
|
+
h1 ^= h1 >>> 13;
|
|
67
|
+
h1 = ((h1 & 0xffff) * 0xc2b2ae35 + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16)) & 0xffffffff;
|
|
68
|
+
h1 ^= h1 >>> 16;
|
|
69
|
+
|
|
70
|
+
return h1 >>> 0;
|
|
71
|
+
}
|
package/src/persister.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { Attribute, Effect, Value, ComplexPersist } from "@eventvisor/types";
|
|
2
|
+
import type { DatafileReader } from "./datafileReader";
|
|
3
|
+
import type { ConditionsChecker } from "./conditions";
|
|
4
|
+
import type { ModulesManager } from "./modulesManager";
|
|
5
|
+
|
|
6
|
+
export type EntityMap = Record<string, Value>;
|
|
7
|
+
|
|
8
|
+
export interface InitializeFromStorageOptions {
|
|
9
|
+
datafileReader: DatafileReader;
|
|
10
|
+
conditionsChecker: ConditionsChecker;
|
|
11
|
+
modulesManager: ModulesManager;
|
|
12
|
+
storageKeyPrefix: string;
|
|
13
|
+
getEntityNames: () => string[];
|
|
14
|
+
getEntity: (entityName: string) => Attribute | Effect | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface FindPersistOptions {
|
|
18
|
+
persists: ComplexPersist[];
|
|
19
|
+
entityName: string;
|
|
20
|
+
conditionsChecker: ConditionsChecker;
|
|
21
|
+
payload: Value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function findPersist(
|
|
25
|
+
options: FindPersistOptions,
|
|
26
|
+
): Promise<ComplexPersist | undefined> {
|
|
27
|
+
const { persists, entityName, conditionsChecker, payload } = options;
|
|
28
|
+
|
|
29
|
+
for (const persist of persists) {
|
|
30
|
+
if (!persist.conditions) {
|
|
31
|
+
return persist;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const isMatched = await conditionsChecker.allAreMatched(persist.conditions, {
|
|
35
|
+
attributeName: entityName,
|
|
36
|
+
effectName: entityName,
|
|
37
|
+
payload,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
if (isMatched) {
|
|
41
|
+
return persist;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function initializeFromStorage({
|
|
47
|
+
datafileReader,
|
|
48
|
+
conditionsChecker,
|
|
49
|
+
modulesManager,
|
|
50
|
+
storageKeyPrefix,
|
|
51
|
+
getEntityNames,
|
|
52
|
+
getEntity,
|
|
53
|
+
}: InitializeFromStorageOptions): Promise<EntityMap> {
|
|
54
|
+
const entityNames = getEntityNames();
|
|
55
|
+
const entityMap: EntityMap = {};
|
|
56
|
+
|
|
57
|
+
for (const entityName of entityNames) {
|
|
58
|
+
const schema = getEntity(entityName);
|
|
59
|
+
|
|
60
|
+
if (!schema) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const persists = datafileReader.getPersists(schema);
|
|
65
|
+
|
|
66
|
+
if (!persists) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const persist = await findPersist({ persists, entityName, conditionsChecker, payload: {} });
|
|
71
|
+
|
|
72
|
+
if (!persist) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// read from storage
|
|
77
|
+
const value = await modulesManager.readFromStorage(
|
|
78
|
+
persist.storage,
|
|
79
|
+
`${storageKeyPrefix}${entityName}`,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
if (value !== null && value !== undefined) {
|
|
83
|
+
entityMap[entityName] = value;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return entityMap;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface PersistEntityOptions {
|
|
91
|
+
datafileReader: DatafileReader;
|
|
92
|
+
conditionsChecker: ConditionsChecker;
|
|
93
|
+
modulesManager: ModulesManager;
|
|
94
|
+
storageKeyPrefix: string;
|
|
95
|
+
entityName: string;
|
|
96
|
+
entity: Attribute | Effect | undefined;
|
|
97
|
+
value: Value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function persistEntity({
|
|
101
|
+
datafileReader,
|
|
102
|
+
conditionsChecker,
|
|
103
|
+
modulesManager,
|
|
104
|
+
storageKeyPrefix,
|
|
105
|
+
entityName,
|
|
106
|
+
entity,
|
|
107
|
+
value,
|
|
108
|
+
}: PersistEntityOptions) {
|
|
109
|
+
if (!entity) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const persists = datafileReader.getPersists(entity);
|
|
114
|
+
|
|
115
|
+
if (!persists) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const persist = await findPersist({ persists, entityName, conditionsChecker, payload: value });
|
|
120
|
+
|
|
121
|
+
if (!persist) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
await modulesManager.writeToStorage(persist.storage, `${storageKeyPrefix}${entityName}`, value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface RemoveEntityOptions {
|
|
129
|
+
datafileReader: DatafileReader;
|
|
130
|
+
conditionsChecker: ConditionsChecker;
|
|
131
|
+
modulesManager: ModulesManager;
|
|
132
|
+
storageKeyPrefix: string;
|
|
133
|
+
entityName: string;
|
|
134
|
+
entity: Attribute | Effect | undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function removeEntity({
|
|
138
|
+
datafileReader,
|
|
139
|
+
conditionsChecker,
|
|
140
|
+
modulesManager,
|
|
141
|
+
storageKeyPrefix,
|
|
142
|
+
entityName,
|
|
143
|
+
entity,
|
|
144
|
+
}: RemoveEntityOptions) {
|
|
145
|
+
if (!entity) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const persists = datafileReader.getPersists(entity);
|
|
150
|
+
|
|
151
|
+
if (!persists) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const persist = await findPersist({ persists, entityName, conditionsChecker, payload: {} });
|
|
156
|
+
|
|
157
|
+
if (!persist) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
await modulesManager.removeFromStorage(persist.storage, `${storageKeyPrefix}${entityName}`);
|
|
162
|
+
}
|