@karmaniverous/entity-manager 5.0.9 → 6.0.0
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 +15 -438
- package/dist/cjs/_virtual/_commonjsHelpers.js +7 -0
- package/dist/cjs/_virtual/lz-string.js +5 -0
- package/dist/cjs/node_modules/@karmaniverous/entity-tools/dist/mjs/Nil.js +14 -0
- package/dist/cjs/node_modules/@karmaniverous/entity-tools/dist/mjs/defaultTranscodes.js +94 -0
- package/dist/cjs/node_modules/@karmaniverous/entity-tools/dist/mjs/sort.js +52 -0
- package/dist/cjs/node_modules/lz-string/libs/lz-string.js +517 -0
- package/dist/cjs/node_modules/radash/dist/esm/array.js +90 -0
- package/dist/cjs/node_modules/radash/dist/esm/async.js +62 -0
- package/dist/cjs/node_modules/radash/dist/esm/object.js +25 -0
- package/dist/cjs/node_modules/radash/dist/esm/typed.js +35 -0
- package/dist/cjs/node_modules/string-hash/index.js +23 -0
- package/dist/cjs/node_modules/tslib/tslib.es6.js +39 -0
- package/dist/cjs/node_modules/zod/lib/index.js +4342 -0
- package/dist/cjs/src/EntityManager.js +93 -0
- package/dist/cjs/src/ParsedConfig.js +227 -0
- package/dist/cjs/src/addKeys.js +47 -0
- package/dist/cjs/src/decodeEntityElement.js +37 -0
- package/dist/cjs/src/decodeGeneratedProperty.js +48 -0
- package/dist/cjs/src/dehydrateIndexItem.js +52 -0
- package/dist/cjs/src/dehydratePageKeyMap.js +75 -0
- package/dist/cjs/src/encodeEntityElement.js +36 -0
- package/dist/cjs/src/encodeGeneratedProperty.js +44 -0
- package/dist/cjs/src/getHashKeySpace.js +41 -0
- package/dist/cjs/src/getShardBump.js +24 -0
- package/dist/cjs/src/index.js +7 -0
- package/dist/cjs/src/query.js +104 -0
- package/dist/cjs/src/rehydrateIndexItem.js +48 -0
- package/dist/cjs/src/rehydratePageKeyMap.js +66 -0
- package/dist/cjs/src/removeKeys.js +33 -0
- package/dist/cjs/src/unwrapIndex.js +40 -0
- package/dist/cjs/src/updateItemHashKey.js +55 -0
- package/dist/cjs/src/updateItemRangeKey.js +45 -0
- package/dist/cjs/src/validateEntityGeneratedProperty.js +29 -0
- package/dist/cjs/src/validateEntityIndexToken.js +22 -0
- package/dist/cjs/src/validateEntityToken.js +17 -0
- package/dist/index.d.cts +826 -0
- package/dist/index.d.mts +826 -0
- package/dist/index.d.ts +826 -0
- package/dist/mjs/_virtual/_commonjsHelpers.js +5 -0
- package/dist/mjs/_virtual/lz-string.js +3 -0
- package/dist/mjs/node_modules/@karmaniverous/entity-tools/dist/mjs/Nil.js +12 -0
- package/dist/mjs/node_modules/@karmaniverous/entity-tools/dist/mjs/defaultTranscodes.js +92 -0
- package/dist/mjs/node_modules/@karmaniverous/entity-tools/dist/mjs/sort.js +50 -0
- package/dist/mjs/node_modules/lz-string/libs/lz-string.js +515 -0
- package/dist/mjs/node_modules/radash/dist/esm/array.js +80 -0
- package/dist/mjs/node_modules/radash/dist/esm/async.js +58 -0
- package/dist/mjs/node_modules/radash/dist/esm/object.js +22 -0
- package/dist/mjs/node_modules/radash/dist/esm/typed.js +28 -0
- package/dist/mjs/node_modules/string-hash/index.js +21 -0
- package/dist/mjs/node_modules/tslib/tslib.es6.js +36 -0
- package/dist/mjs/node_modules/zod/lib/index.js +4233 -0
- package/dist/mjs/src/EntityManager.js +91 -0
- package/dist/mjs/src/ParsedConfig.js +225 -0
- package/dist/mjs/src/addKeys.js +45 -0
- package/dist/mjs/src/decodeEntityElement.js +35 -0
- package/dist/mjs/src/decodeGeneratedProperty.js +46 -0
- package/dist/mjs/src/dehydrateIndexItem.js +50 -0
- package/dist/mjs/src/dehydratePageKeyMap.js +73 -0
- package/dist/mjs/src/encodeEntityElement.js +34 -0
- package/dist/mjs/src/encodeGeneratedProperty.js +42 -0
- package/dist/mjs/src/getHashKeySpace.js +39 -0
- package/dist/mjs/src/getShardBump.js +22 -0
- package/dist/mjs/src/index.js +1 -0
- package/dist/mjs/src/query.js +102 -0
- package/dist/mjs/src/rehydrateIndexItem.js +46 -0
- package/dist/mjs/src/rehydratePageKeyMap.js +64 -0
- package/dist/mjs/src/removeKeys.js +31 -0
- package/dist/mjs/src/unwrapIndex.js +38 -0
- package/dist/mjs/src/updateItemHashKey.js +53 -0
- package/dist/mjs/src/updateItemRangeKey.js +43 -0
- package/dist/mjs/src/validateEntityGeneratedProperty.js +27 -0
- package/dist/mjs/src/validateEntityIndexToken.js +20 -0
- package/dist/mjs/src/validateEntityToken.js +15 -0
- package/package.json +107 -58
- package/.env +0 -1
- package/.github/FUNDING.yml +0 -3
- package/dist/default/lib/EntityManager/EntityManager.js +0 -423
- package/dist/default/lib/EntityManager/PrivateEntityManager.js +0 -421
- package/dist/default/lib/index.js +0 -12
- package/dist/package.json +0 -3
- package/lib/EntityManager/EntityManager.js +0 -396
- package/lib/EntityManager/PrivateEntityManager.js +0 -436
- package/lib/index.js +0 -1
|
@@ -1,436 +0,0 @@
|
|
|
1
|
-
import { validate } from 'jsonschema';
|
|
2
|
-
import _ from 'lodash';
|
|
3
|
-
import stringHash from 'string-hash';
|
|
4
|
-
|
|
5
|
-
export const DEFAULT_LIMIT = 10;
|
|
6
|
-
export const DEFAULT_PAGE_SIZE = 10;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* EntityManager config validation schema.
|
|
10
|
-
*
|
|
11
|
-
* @private
|
|
12
|
-
*/
|
|
13
|
-
const configSchema = {
|
|
14
|
-
type: 'object',
|
|
15
|
-
properties: {
|
|
16
|
-
entities: {
|
|
17
|
-
type: 'object',
|
|
18
|
-
patternProperties: {
|
|
19
|
-
'^\\w+$': {
|
|
20
|
-
type: 'object',
|
|
21
|
-
properties: {
|
|
22
|
-
defaultLimit: {
|
|
23
|
-
type: 'integer',
|
|
24
|
-
},
|
|
25
|
-
defaultPageSize: {
|
|
26
|
-
type: 'integer',
|
|
27
|
-
},
|
|
28
|
-
indexes: {
|
|
29
|
-
type: 'object',
|
|
30
|
-
additionalProperties: {
|
|
31
|
-
type: 'array',
|
|
32
|
-
items: {
|
|
33
|
-
type: 'string',
|
|
34
|
-
},
|
|
35
|
-
minItems: 1,
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
keys: {
|
|
39
|
-
type: 'object',
|
|
40
|
-
},
|
|
41
|
-
sharding: {
|
|
42
|
-
type: 'object',
|
|
43
|
-
properties: {
|
|
44
|
-
bumps: {
|
|
45
|
-
type: 'object',
|
|
46
|
-
patternProperties: {
|
|
47
|
-
'^\\d+$': { type: 'integer', minimum: 0 },
|
|
48
|
-
},
|
|
49
|
-
additionalProperties: false,
|
|
50
|
-
},
|
|
51
|
-
entityKey: { type: 'function' },
|
|
52
|
-
nibbleBits: {
|
|
53
|
-
type: 'integer',
|
|
54
|
-
minimum: 1,
|
|
55
|
-
maximum: 5,
|
|
56
|
-
},
|
|
57
|
-
nibbles: {
|
|
58
|
-
type: 'integer',
|
|
59
|
-
minimum: 0,
|
|
60
|
-
},
|
|
61
|
-
timestamp: { type: 'function' },
|
|
62
|
-
},
|
|
63
|
-
additionalProperties: false,
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
additionalProperties: false,
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
additionalProperties: false,
|
|
70
|
-
},
|
|
71
|
-
shardKeyToken: {
|
|
72
|
-
type: 'string',
|
|
73
|
-
pattern: '^\\w+$',
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
additionalProperties: false,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const keySchema = {
|
|
80
|
-
type: 'object',
|
|
81
|
-
properties: {
|
|
82
|
-
encode: {},
|
|
83
|
-
decode: {},
|
|
84
|
-
retain: {
|
|
85
|
-
type: 'boolean',
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
additionalProperties: false,
|
|
89
|
-
required: ['encode'],
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Private EntityManager implementation.
|
|
94
|
-
*
|
|
95
|
-
* @private
|
|
96
|
-
*/
|
|
97
|
-
export class PrivateEntityManager {
|
|
98
|
-
#config;
|
|
99
|
-
#logger;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Create a PrivateEntityManager instance.
|
|
103
|
-
*
|
|
104
|
-
* @param {object} options - Options object.
|
|
105
|
-
* @param {object} [options.config] - EntityManager configuration object.
|
|
106
|
-
* @param {object} [options.logger] - Logger instance (defaults to console, must support error & debug methods).
|
|
107
|
-
* @throws {Error} If config is invalid.
|
|
108
|
-
* @throws {Error} If logger is invalid.
|
|
109
|
-
*/
|
|
110
|
-
constructor({ config = {}, logger = console } = {}) {
|
|
111
|
-
// Validate logger.
|
|
112
|
-
if (!logger.error || !logger.debug)
|
|
113
|
-
throw new Error('logger must implement error & debug methods.');
|
|
114
|
-
|
|
115
|
-
this.#logger = logger;
|
|
116
|
-
this.config = config;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Get the current config.
|
|
121
|
-
*
|
|
122
|
-
* @returns {object} Current config.
|
|
123
|
-
*/
|
|
124
|
-
get config() {
|
|
125
|
-
return this.#config;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Set the current config.
|
|
130
|
-
*
|
|
131
|
-
* @param {object} value - Config object.
|
|
132
|
-
* @throws {Error} If config is invalid.
|
|
133
|
-
* @throws {Error} If entity bumps do not monotonically increase.
|
|
134
|
-
* @throws {Error} If entity nibbles are greater than minimum bump value.
|
|
135
|
-
*/
|
|
136
|
-
set config(value) {
|
|
137
|
-
// Validate config against schema.
|
|
138
|
-
const validatorResult = validate(value, configSchema);
|
|
139
|
-
if (!validatorResult.valid) throw new Error(validatorResult.errors);
|
|
140
|
-
|
|
141
|
-
// Conform config.
|
|
142
|
-
const { entities = {}, shardKeyToken = 'shardId' } = value;
|
|
143
|
-
const conformedConfig = {
|
|
144
|
-
entities: _.mapValues(
|
|
145
|
-
entities,
|
|
146
|
-
({
|
|
147
|
-
defaultLimit = DEFAULT_LIMIT,
|
|
148
|
-
defaultPageSize = DEFAULT_PAGE_SIZE,
|
|
149
|
-
indexes = {},
|
|
150
|
-
keys = {},
|
|
151
|
-
sharding: {
|
|
152
|
-
bumps = {},
|
|
153
|
-
entityKey,
|
|
154
|
-
nibbleBits = 1,
|
|
155
|
-
nibbles = 0,
|
|
156
|
-
timestamp,
|
|
157
|
-
} = {},
|
|
158
|
-
}) => ({
|
|
159
|
-
defaultLimit,
|
|
160
|
-
defaultPageSize,
|
|
161
|
-
indexes,
|
|
162
|
-
keys,
|
|
163
|
-
sharding: {
|
|
164
|
-
bumps: _.fromPairs(_.sortBy(_.toPairs(bumps), 0)),
|
|
165
|
-
entityKey,
|
|
166
|
-
nibbleBits,
|
|
167
|
-
nibbles,
|
|
168
|
-
timestamp,
|
|
169
|
-
},
|
|
170
|
-
})
|
|
171
|
-
),
|
|
172
|
-
shardKeyToken,
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
// Conform & validate entity properties.
|
|
176
|
-
_.some(
|
|
177
|
-
conformedConfig.entities,
|
|
178
|
-
(
|
|
179
|
-
{
|
|
180
|
-
indexes,
|
|
181
|
-
keys,
|
|
182
|
-
sharding: { bumps, entityKey, nibbleBits, nibbles, timestamp },
|
|
183
|
-
},
|
|
184
|
-
entityToken
|
|
185
|
-
) => {
|
|
186
|
-
// Validate entity index components are entity keys.
|
|
187
|
-
_.some(indexes, (index, indexToken) => {
|
|
188
|
-
const invalidIndexComponents = _.difference(index, _.keys(keys));
|
|
189
|
-
if (invalidIndexComponents.length)
|
|
190
|
-
throw new Error(
|
|
191
|
-
`${entityToken} index '${indexToken}' components '${invalidIndexComponents}' are not entity keys.`
|
|
192
|
-
);
|
|
193
|
-
else return false;
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
// Conform & validate entity keys.
|
|
197
|
-
_.forEach(keys, (value, key) => {
|
|
198
|
-
keys[key] = _.isFunction(value) ? { encode: value } : value;
|
|
199
|
-
|
|
200
|
-
// Validate key against schema.
|
|
201
|
-
const validatorResult = validate(keys[key], keySchema);
|
|
202
|
-
if (!validatorResult.valid) throw new Error(validatorResult.errors);
|
|
203
|
-
|
|
204
|
-
const { decode } = keys[key];
|
|
205
|
-
if (!_.isUndefined(decode) && !_.isFunction(decode))
|
|
206
|
-
throw new Error(
|
|
207
|
-
`entity ${entityKey} key '${key}' decode must be a function or undefined.`
|
|
208
|
-
);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
// Validate sharding bump values increase monotonically with keys.
|
|
212
|
-
_.some(_.toPairs(bumps), ([bump, value], i, c) => {
|
|
213
|
-
const [lastBump, lastValue] = i ? c[i - 1] : [];
|
|
214
|
-
if (value <= lastValue)
|
|
215
|
-
throw new Error(
|
|
216
|
-
`${entityToken} sharding bumps do not monotonically increase from '${lastBump}: ${lastValue}' to '${bump}: ${value}'.)`
|
|
217
|
-
);
|
|
218
|
-
else return false;
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
// Validate sharding entityKey is a function or undefined.
|
|
222
|
-
if (!_.isNil(entityKey) && !_.isFunction(entityKey))
|
|
223
|
-
throw new Error(
|
|
224
|
-
`${entityToken} sharding entityKey must be a function or nil`
|
|
225
|
-
);
|
|
226
|
-
|
|
227
|
-
// Validate sharding nibbles do not exceed 32 bits.
|
|
228
|
-
if (nibbles * nibbleBits > 32)
|
|
229
|
-
throw new Error(
|
|
230
|
-
`${entityToken} nibbles (${nibbles} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`
|
|
231
|
-
);
|
|
232
|
-
|
|
233
|
-
// Validate sharding nibbles are less than first bump value.
|
|
234
|
-
const firstBumpValue = _.map(bumps)[0];
|
|
235
|
-
if (nibbles >= firstBumpValue)
|
|
236
|
-
throw new Error(
|
|
237
|
-
`${entityToken} nibbles (${nibbles}) not less than minimum bump value (${firstBumpValue})`
|
|
238
|
-
);
|
|
239
|
-
|
|
240
|
-
// Validate last bump value does not exceed 32 bits.
|
|
241
|
-
const lastBumpValue = _.map(bumps).slice(-1);
|
|
242
|
-
if (lastBumpValue * nibbleBits > 32)
|
|
243
|
-
throw new Error(
|
|
244
|
-
`${entityToken} maximum bump value (${lastBumpValue} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`
|
|
245
|
-
);
|
|
246
|
-
|
|
247
|
-
// Validate sharding timestamp is a function or undefined.
|
|
248
|
-
if (!_.isNil(timestamp) && !_.isFunction(timestamp))
|
|
249
|
-
throw new Error(
|
|
250
|
-
`${entityToken} sharding timestamp must be a function or nil.`
|
|
251
|
-
);
|
|
252
|
-
else return false;
|
|
253
|
-
}
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
this.#config = conformedConfig;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Get logger instance.
|
|
261
|
-
*
|
|
262
|
-
* @returns {object} Logger instance.
|
|
263
|
-
*/
|
|
264
|
-
get logger() {
|
|
265
|
-
return this.#logger;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Get shard key token.
|
|
270
|
-
*
|
|
271
|
-
* @returns {string} Shard key token.
|
|
272
|
-
*/
|
|
273
|
-
get shardKeyToken() {
|
|
274
|
-
return this.#config.shardKeyToken;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Get entity config.
|
|
279
|
-
*
|
|
280
|
-
* @param {string} entityToken - Entity token.
|
|
281
|
-
* @returns {object} Entity config.
|
|
282
|
-
* @throws {Error} If entityToken is invalid.
|
|
283
|
-
*/
|
|
284
|
-
getEntityConfig(entityToken) {
|
|
285
|
-
this.validateEntityToken(entityToken);
|
|
286
|
-
return this.config.entities[entityToken];
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
getIndexComponents(entityToken, indexToken) {
|
|
290
|
-
const { indexes } = this.getEntityConfig(entityToken);
|
|
291
|
-
|
|
292
|
-
if (!_.has(indexes, indexToken))
|
|
293
|
-
throw new Error(
|
|
294
|
-
`Index '${indexToken}' does not exist for entity '${entityToken}'.`
|
|
295
|
-
);
|
|
296
|
-
|
|
297
|
-
return indexes[indexToken];
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Get the number of nibbles & nibbleBits for a given entityToken at a given timestamp.
|
|
302
|
-
*
|
|
303
|
-
* @param {string} entityToken - Entity token.
|
|
304
|
-
* @param {number} [timestamp] - Timestamp in milliseconds (defaults to current time).
|
|
305
|
-
* @returns {{nibbleBits: number, nibbles: number}} Result object.
|
|
306
|
-
*/
|
|
307
|
-
getNibbles(entityToken, timestamp = Date.now()) {
|
|
308
|
-
const { nibbleBits, nibbles, bumps } =
|
|
309
|
-
this.getEntityConfig(entityToken).sharding;
|
|
310
|
-
|
|
311
|
-
this.validateTimestamp(timestamp);
|
|
312
|
-
return {
|
|
313
|
-
nibbleBits,
|
|
314
|
-
nibbles: _.findLast(bumps, (value, key) => key <= timestamp) ?? nibbles,
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Return a shard key for a given entity token, entity id & timestamp.
|
|
320
|
-
*
|
|
321
|
-
* @param {string} entityToken - Entity token.
|
|
322
|
-
* @param {string} entityKey - Entity id.
|
|
323
|
-
* @param {number} [timestamp] - Timestamp in milliseconds (defaults to current time).
|
|
324
|
-
* @returns {string} shard key.
|
|
325
|
-
*/
|
|
326
|
-
getShardKey(entityToken, entityKey, timestamp = Date.now()) {
|
|
327
|
-
// Get nibbles for entityToken at timestamp (validates entityToken)
|
|
328
|
-
const { nibbleBits, nibbles } = this.getNibbles(entityToken, timestamp);
|
|
329
|
-
|
|
330
|
-
// Calculate shardKey.
|
|
331
|
-
const radix = 2 ** nibbleBits;
|
|
332
|
-
const shardKey = nibbles
|
|
333
|
-
? (stringHash(entityKey) % (nibbles * radix))
|
|
334
|
-
.toString(radix)
|
|
335
|
-
.padStart(nibbles, '0')
|
|
336
|
-
: undefined;
|
|
337
|
-
|
|
338
|
-
if (_.isNil(shardKey))
|
|
339
|
-
this.logger.debug(
|
|
340
|
-
`no shard key generated for ${entityToken} id '${entityKey}' at timestamp ${timestamp}.`
|
|
341
|
-
);
|
|
342
|
-
else
|
|
343
|
-
this.logger.debug(
|
|
344
|
-
`generated shard key '${shardKey}' for ${entityToken} id '${entityKey}' at timestamp ${timestamp}.`
|
|
345
|
-
);
|
|
346
|
-
|
|
347
|
-
return shardKey;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Return an array of shard keys valid for a given entity token & timestamp.
|
|
352
|
-
*
|
|
353
|
-
* @param {string} entityToken - Entity token.
|
|
354
|
-
* @param {number} [timestamp] - Timestamp in milliseconds (defaults to current time).
|
|
355
|
-
* @returns {string[]} shard key space.
|
|
356
|
-
*/
|
|
357
|
-
getShardKeySpace(entityToken, timestamp = Date.now()) {
|
|
358
|
-
const { nibbleBits, nibbles, bumps } =
|
|
359
|
-
this.getEntityConfig(entityToken).sharding;
|
|
360
|
-
// if (!nibbles && !_.size(bumps)) return [''];
|
|
361
|
-
|
|
362
|
-
this.validateTimestamp(timestamp);
|
|
363
|
-
const nibbleSpace = [
|
|
364
|
-
nibbles,
|
|
365
|
-
..._.map(_.filter(bumps, (value, key) => key <= timestamp)),
|
|
366
|
-
];
|
|
367
|
-
|
|
368
|
-
const radix = 2 ** nibbleBits;
|
|
369
|
-
const shardKeySpace = _.flatten(
|
|
370
|
-
_.map(nibbleSpace, (nibbles) => {
|
|
371
|
-
return nibbles
|
|
372
|
-
? _.range(0, radix ** nibbles).map((nibble) =>
|
|
373
|
-
nibble.toString(radix).padStart(nibbles, '0')
|
|
374
|
-
)
|
|
375
|
-
: undefined;
|
|
376
|
-
})
|
|
377
|
-
);
|
|
378
|
-
|
|
379
|
-
return shardKeySpace;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* Tests whether an entityToken is valid.
|
|
384
|
-
*
|
|
385
|
-
* @param {string} entityToken - Entity token.
|
|
386
|
-
* @returns {boolean} true if entityToken is valid.
|
|
387
|
-
*/
|
|
388
|
-
validateEntityToken(entityToken) {
|
|
389
|
-
if (!this.config.entities[entityToken])
|
|
390
|
-
throw new Error(`Invalid entityToken: ${entityToken}`);
|
|
391
|
-
else return true;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Tests whether an item is valid.
|
|
396
|
-
*
|
|
397
|
-
* @param {string} item - Entity token.
|
|
398
|
-
* @returns {boolean} true if entityToken is valid.
|
|
399
|
-
*/
|
|
400
|
-
validateItem(item) {
|
|
401
|
-
if (!_.isPlainObject(item)) throw new Error(`Invalid item: ${item}`);
|
|
402
|
-
else return true;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* Tests whether a key token is valid.
|
|
407
|
-
*
|
|
408
|
-
* @param {string} entityToken - Entity token.
|
|
409
|
-
* @param {string} keyToken - Key token.
|
|
410
|
-
* @returns {boolean} true if keyToken is valid.
|
|
411
|
-
*/
|
|
412
|
-
validateKeyToken(entityToken, keyToken) {
|
|
413
|
-
this.validateEntityToken(entityToken);
|
|
414
|
-
|
|
415
|
-
if (!this.config.entities[entityToken].keys[keyToken])
|
|
416
|
-
throw new Error(`Invalid entity ${entityToken} keyToken: ${keyToken}`);
|
|
417
|
-
else return true;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
/**
|
|
421
|
-
* Tests whether a timestamp is valid.
|
|
422
|
-
*
|
|
423
|
-
* @param {number} timestamp - timestamp.
|
|
424
|
-
* @param {boolean} [future] - true if timestamp must be in the future.
|
|
425
|
-
* @returns {boolean} true if timestamp is valid.
|
|
426
|
-
*/
|
|
427
|
-
validateTimestamp(timestamp, future = false) {
|
|
428
|
-
if (!_.isInteger(timestamp) || (future && timestamp <= Date.now()))
|
|
429
|
-
throw new Error(
|
|
430
|
-
`invalid timestamp (must be an integer${
|
|
431
|
-
future ? ' in the future' : ''
|
|
432
|
-
})`
|
|
433
|
-
);
|
|
434
|
-
else return true;
|
|
435
|
-
}
|
|
436
|
-
}
|
package/lib/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { EntityManager } from './EntityManager/EntityManager.js';
|