@karmaniverous/entity-manager 2.1.1 → 2.1.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.
@@ -149,7 +149,7 @@ class EntityManager {
149
149
  if (!(0, _isFunction2.default)(decode)) throw new Error(`no decode function defined on ${entityToken} entity key '${component}'`);
150
150
  return {
151
151
  ...properties,
152
- ...decode(index[component])
152
+ ...(decode(index[component] ?? '')?.groups ?? [])
153
153
  };
154
154
  }, {});
155
155
  return (0, _sortedUniqBy2.default)((0, _sortBy2.default)((0, _entries2.default)(indexProperties), _ref3 => {
@@ -170,10 +170,7 @@ class PrivateEntityManager {
170
170
  set config(value) {
171
171
  // Validate config against schema.
172
172
  const validatorResult = (0, _jsonschema.validate)(value, configSchema);
173
- if (!validatorResult.valid) {
174
- validatorResult.errors.forEach(error => this.logger.error(error.message));
175
- throw new Error(validatorResult.errors);
176
- }
173
+ if (!validatorResult.valid) throw new Error(validatorResult.errors);
177
174
 
178
175
  // Conform config.
179
176
  const {
@@ -224,11 +221,7 @@ class PrivateEntityManager {
224
221
  // Validate entity index components are entity keys.
225
222
  (0, _some2.default)(indexes, (index, indexToken) => {
226
223
  const invalidIndexComponents = (0, _difference2.default)(index, (0, _keys2.default)(keys));
227
- if (invalidIndexComponents.length) {
228
- const message = `${entityToken} index '${indexToken}' components '${invalidIndexComponents}' are not entity keys.`;
229
- this.logger.error(message);
230
- throw new Error(message);
231
- } else return false;
224
+ if (invalidIndexComponents.length) throw new Error(`${entityToken} index '${indexToken}' components '${invalidIndexComponents}' are not entity keys.`);else return false;
232
225
  });
233
226
 
234
227
  // Conform & validate entity keys.
@@ -239,67 +232,36 @@ class PrivateEntityManager {
239
232
 
240
233
  // Validate key against schema.
241
234
  const validatorResult = (0, _jsonschema.validate)(keys[key], keySchema);
242
- if (!validatorResult.valid) {
243
- validatorResult.errors.forEach(error => this.logger.error(error.message));
244
- throw new Error(validatorResult.errors);
245
- }
235
+ if (!validatorResult.valid) throw new Error(validatorResult.errors);
246
236
  const {
247
237
  decode
248
238
  } = keys[key];
249
- if (!(0, _isUndefined2.default)(decode) && !(0, _isFunction2.default)(decode)) {
250
- const message = `entity ${entityKey} key '${key}' decode must be a function or undefined.`;
251
- this.logger.error(message);
252
- throw new Error(message);
253
- }
239
+ if (!(0, _isUndefined2.default)(decode) && !(0, _isFunction2.default)(decode)) throw new Error(`entity ${entityKey} key '${key}' decode must be a function or undefined.`);
254
240
  });
255
241
 
256
242
  // Validate sharding bump values increase monotonically with keys.
257
243
  (0, _some2.default)((0, _toPairs2.default)(bumps), (_ref3, i, c) => {
258
244
  let [bump, value] = _ref3;
259
245
  const [lastBump, lastValue] = i ? c[i - 1] : [];
260
- if (value <= lastValue) {
261
- const message = `${entityToken} sharding bumps do not monotonically increase from '${lastBump}: ${lastValue}' to '${bump}: ${value}'.)`;
262
- this.logger.error(message);
263
- throw new Error(message);
264
- } else return false;
246
+ if (value <= lastValue) throw new Error(`${entityToken} sharding bumps do not monotonically increase from '${lastBump}: ${lastValue}' to '${bump}: ${value}'.)`);else return false;
265
247
  });
266
248
 
267
249
  // Validate sharding entityKey is a function or undefined.
268
- if (!(0, _isNil2.default)(entityKey) && !(0, _isFunction2.default)(entityKey)) {
269
- const message = `${entityToken} sharding entityKey must be a function or nil`;
270
- this.logger.error(message);
271
- throw new Error(message);
272
- }
250
+ if (!(0, _isNil2.default)(entityKey) && !(0, _isFunction2.default)(entityKey)) throw new Error(`${entityToken} sharding entityKey must be a function or nil`);
273
251
 
274
252
  // Validate sharding nibbles do not exceed 32 bits.
275
- if (nibbles * nibbleBits > 32) {
276
- const message = `${entityToken} nibbles (${nibbles} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`;
277
- this.logger.error(message);
278
- throw new Error(message);
279
- }
253
+ if (nibbles * nibbleBits > 32) throw new Error(`${entityToken} nibbles (${nibbles} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`);
280
254
 
281
255
  // Validate sharding nibbles are less than first bump value.
282
256
  const firstBumpValue = (0, _map2.default)(bumps)[0];
283
- if (nibbles >= firstBumpValue) {
284
- const message = `${entityToken} nibbles (${nibbles}) not less than minimum bump value (${firstBumpValue})`;
285
- this.logger.error(message);
286
- throw new Error(message);
287
- }
257
+ if (nibbles >= firstBumpValue) throw new Error(`${entityToken} nibbles (${nibbles}) not less than minimum bump value (${firstBumpValue})`);
288
258
 
289
259
  // Validate last bump value does not exceed 32 bits.
290
260
  const lastBumpValue = (0, _map2.default)(bumps).slice(-1);
291
- if (lastBumpValue * nibbleBits > 32) {
292
- const message = `${entityToken} maximum bump value (${lastBumpValue} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`;
293
- this.logger.error(message);
294
- throw new Error(message);
295
- }
261
+ if (lastBumpValue * nibbleBits > 32) throw new Error(`${entityToken} maximum bump value (${lastBumpValue} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`);
296
262
 
297
263
  // Validate sharding timestamp is a function or undefined.
298
- if (!(0, _isNil2.default)(timestamp) && !(0, _isFunction2.default)(timestamp)) {
299
- const message = `${entityToken} sharding timestamp must be a function or nil.`;
300
- this.logger.error(message);
301
- throw new Error(message);
302
- } else return false;
264
+ if (!(0, _isNil2.default)(timestamp) && !(0, _isFunction2.default)(timestamp)) throw new Error(`${entityToken} sharding timestamp must be a function or nil.`);else return false;
303
265
  });
304
266
  _classPrivateFieldSet(this, _config, conformedConfig);
305
267
  }
@@ -337,22 +299,14 @@ class PrivateEntityManager {
337
299
  const {
338
300
  indexes
339
301
  } = this.getEntityConfig(entityToken);
340
- if (!(0, _has2.default)(indexes, indexToken)) {
341
- const message = `Index '${indexToken}' does not exist for entity '${entityToken}'.`;
342
- this.logger.error(message);
343
- throw new Error(message);
344
- }
302
+ if (!(0, _has2.default)(indexes, indexToken)) throw new Error(`Index '${indexToken}' does not exist for entity '${entityToken}'.`);
345
303
  return indexes[indexToken];
346
304
  }
347
305
  getKey(entityToken, keyToken) {
348
306
  const {
349
307
  keys
350
308
  } = this.getEntityConfig(entityToken);
351
- if (!(0, _has2.default)(keys, keyToken)) {
352
- const message = `Key '${keyToken}' does not exist for entity '${entityToken}'.`;
353
- this.logger.error(message);
354
- throw new Error(message);
355
- }
309
+ if (!(0, _has2.default)(keys, keyToken)) throw new Error(`Key '${keyToken}' does not exist for entity '${entityToken}'.`);
356
310
  return keys[keyToken];
357
311
  }
358
312
 
@@ -432,11 +386,7 @@ class PrivateEntityManager {
432
386
  * @returns {boolean} true if entityToken is valid.
433
387
  */
434
388
  validateEntityToken(entityToken) {
435
- if (!this.config.entities[entityToken]) {
436
- const message = `Invalid entityToken: ${entityToken}`;
437
- this.logger.error(message);
438
- throw new Error(message);
439
- } else return true;
389
+ if (!this.config.entities[entityToken]) throw new Error(`Invalid entityToken: ${entityToken}`);else return true;
440
390
  }
441
391
 
442
392
  /**
@@ -446,11 +396,7 @@ class PrivateEntityManager {
446
396
  * @returns {boolean} true if entityToken is valid.
447
397
  */
448
398
  validateItem(item) {
449
- if (!(0, _isPlainObject2.default)(item)) {
450
- const message = `Invalid item: ${item}`;
451
- this.logger.error(message);
452
- throw new Error(message);
453
- } else return true;
399
+ if (!(0, _isPlainObject2.default)(item)) throw new Error(`Invalid item: ${item}`);else return true;
454
400
  }
455
401
 
456
402
  /**
@@ -462,11 +408,7 @@ class PrivateEntityManager {
462
408
  */
463
409
  validateKeyToken(entityToken, keyToken) {
464
410
  this.validateEntityToken(entityToken);
465
- if (!this.config.entities[entityToken].keys[keyToken]) {
466
- const message = `Invalid entity ${entityToken} keyToken: ${keyToken}`;
467
- this.logger.error(message);
468
- throw new Error(message);
469
- } else return true;
411
+ if (!this.config.entities[entityToken].keys[keyToken]) throw new Error(`Invalid entity ${entityToken} keyToken: ${keyToken}`);else return true;
470
412
  }
471
413
 
472
414
  /**
@@ -117,7 +117,10 @@ export class EntityManager {
117
117
  `no decode function defined on ${entityToken} entity key '${component}'`
118
118
  );
119
119
 
120
- return { ...properties, ...decode(index[component]) };
120
+ return {
121
+ ...properties,
122
+ ...(decode(index[component] ?? '')?.groups ?? []),
123
+ };
121
124
  }, {});
122
125
 
123
126
  return _.sortedUniqBy(
@@ -124,12 +124,7 @@ export class PrivateEntityManager {
124
124
  set config(value) {
125
125
  // Validate config against schema.
126
126
  const validatorResult = validate(value, configSchema);
127
- if (!validatorResult.valid) {
128
- validatorResult.errors.forEach((error) =>
129
- this.logger.error(error.message)
130
- );
131
- throw new Error(validatorResult.errors);
132
- }
127
+ if (!validatorResult.valid) throw new Error(validatorResult.errors);
133
128
 
134
129
  // Conform config.
135
130
  const { entities = {}, shardKeyToken = 'shardId' } = value;
@@ -175,11 +170,11 @@ export class PrivateEntityManager {
175
170
  // Validate entity index components are entity keys.
176
171
  _.some(indexes, (index, indexToken) => {
177
172
  const invalidIndexComponents = _.difference(index, _.keys(keys));
178
- if (invalidIndexComponents.length) {
179
- const message = `${entityToken} index '${indexToken}' components '${invalidIndexComponents}' are not entity keys.`;
180
- this.logger.error(message);
181
- throw new Error(message);
182
- } else return false;
173
+ if (invalidIndexComponents.length)
174
+ throw new Error(
175
+ `${entityToken} index '${indexToken}' components '${invalidIndexComponents}' are not entity keys.`
176
+ );
177
+ else return false;
183
178
  });
184
179
 
185
180
  // Conform & validate entity keys.
@@ -188,67 +183,57 @@ export class PrivateEntityManager {
188
183
 
189
184
  // Validate key against schema.
190
185
  const validatorResult = validate(keys[key], keySchema);
191
- if (!validatorResult.valid) {
192
- validatorResult.errors.forEach((error) =>
193
- this.logger.error(error.message)
194
- );
195
- throw new Error(validatorResult.errors);
196
- }
186
+ if (!validatorResult.valid) throw new Error(validatorResult.errors);
197
187
 
198
188
  const { decode } = keys[key];
199
- if (!_.isUndefined(decode) && !_.isFunction(decode)) {
200
- const message = `entity ${entityKey} key '${key}' decode must be a function or undefined.`;
201
- this.logger.error(message);
202
- throw new Error(message);
203
- }
189
+ if (!_.isUndefined(decode) && !_.isFunction(decode))
190
+ throw new Error(
191
+ `entity ${entityKey} key '${key}' decode must be a function or undefined.`
192
+ );
204
193
  });
205
194
 
206
195
  // Validate sharding bump values increase monotonically with keys.
207
196
  _.some(_.toPairs(bumps), ([bump, value], i, c) => {
208
197
  const [lastBump, lastValue] = i ? c[i - 1] : [];
209
- if (value <= lastValue) {
210
- const message = `${entityToken} sharding bumps do not monotonically increase from '${lastBump}: ${lastValue}' to '${bump}: ${value}'.)`;
211
- this.logger.error(message);
212
- throw new Error(message);
213
- } else return false;
198
+ if (value <= lastValue)
199
+ throw new Error(
200
+ `${entityToken} sharding bumps do not monotonically increase from '${lastBump}: ${lastValue}' to '${bump}: ${value}'.)`
201
+ );
202
+ else return false;
214
203
  });
215
204
 
216
205
  // Validate sharding entityKey is a function or undefined.
217
- if (!_.isNil(entityKey) && !_.isFunction(entityKey)) {
218
- const message = `${entityToken} sharding entityKey must be a function or nil`;
219
- this.logger.error(message);
220
- throw new Error(message);
221
- }
206
+ if (!_.isNil(entityKey) && !_.isFunction(entityKey))
207
+ throw new Error(
208
+ `${entityToken} sharding entityKey must be a function or nil`
209
+ );
222
210
 
223
211
  // Validate sharding nibbles do not exceed 32 bits.
224
- if (nibbles * nibbleBits > 32) {
225
- const message = `${entityToken} nibbles (${nibbles} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`;
226
- this.logger.error(message);
227
- throw new Error(message);
228
- }
212
+ if (nibbles * nibbleBits > 32)
213
+ throw new Error(
214
+ `${entityToken} nibbles (${nibbles} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`
215
+ );
229
216
 
230
217
  // Validate sharding nibbles are less than first bump value.
231
218
  const firstBumpValue = _.map(bumps)[0];
232
- if (nibbles >= firstBumpValue) {
233
- const message = `${entityToken} nibbles (${nibbles}) not less than minimum bump value (${firstBumpValue})`;
234
- this.logger.error(message);
235
- throw new Error(message);
236
- }
219
+ if (nibbles >= firstBumpValue)
220
+ throw new Error(
221
+ `${entityToken} nibbles (${nibbles}) not less than minimum bump value (${firstBumpValue})`
222
+ );
237
223
 
238
224
  // Validate last bump value does not exceed 32 bits.
239
225
  const lastBumpValue = _.map(bumps).slice(-1);
240
- if (lastBumpValue * nibbleBits > 32) {
241
- const message = `${entityToken} maximum bump value (${lastBumpValue} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`;
242
- this.logger.error(message);
243
- throw new Error(message);
244
- }
226
+ if (lastBumpValue * nibbleBits > 32)
227
+ throw new Error(
228
+ `${entityToken} maximum bump value (${lastBumpValue} nibbles at ${nibbleBits} nibbleBits) exceed 32 bits`
229
+ );
245
230
 
246
231
  // Validate sharding timestamp is a function or undefined.
247
- if (!_.isNil(timestamp) && !_.isFunction(timestamp)) {
248
- const message = `${entityToken} sharding timestamp must be a function or nil.`;
249
- this.logger.error(message);
250
- throw new Error(message);
251
- } else return false;
232
+ if (!_.isNil(timestamp) && !_.isFunction(timestamp))
233
+ throw new Error(
234
+ `${entityToken} sharding timestamp must be a function or nil.`
235
+ );
236
+ else return false;
252
237
  }
253
238
  );
254
239
 
@@ -288,11 +273,10 @@ export class PrivateEntityManager {
288
273
  getIndexComponents(entityToken, indexToken) {
289
274
  const { indexes } = this.getEntityConfig(entityToken);
290
275
 
291
- if (!_.has(indexes, indexToken)) {
292
- const message = `Index '${indexToken}' does not exist for entity '${entityToken}'.`;
293
- this.logger.error(message);
294
- throw new Error(message);
295
- }
276
+ if (!_.has(indexes, indexToken))
277
+ throw new Error(
278
+ `Index '${indexToken}' does not exist for entity '${entityToken}'.`
279
+ );
296
280
 
297
281
  return indexes[indexToken];
298
282
  }
@@ -300,11 +284,10 @@ export class PrivateEntityManager {
300
284
  getKey(entityToken, keyToken) {
301
285
  const { keys } = this.getEntityConfig(entityToken);
302
286
 
303
- if (!_.has(keys, keyToken)) {
304
- const message = `Key '${keyToken}' does not exist for entity '${entityToken}'.`;
305
- this.logger.error(message);
306
- throw new Error(message);
307
- }
287
+ if (!_.has(keys, keyToken))
288
+ throw new Error(
289
+ `Key '${keyToken}' does not exist for entity '${entityToken}'.`
290
+ );
308
291
 
309
292
  return keys[keyToken];
310
293
  }
@@ -398,11 +381,9 @@ export class PrivateEntityManager {
398
381
  * @returns {boolean} true if entityToken is valid.
399
382
  */
400
383
  validateEntityToken(entityToken) {
401
- if (!this.config.entities[entityToken]) {
402
- const message = `Invalid entityToken: ${entityToken}`;
403
- this.logger.error(message);
404
- throw new Error(message);
405
- } else return true;
384
+ if (!this.config.entities[entityToken])
385
+ throw new Error(`Invalid entityToken: ${entityToken}`);
386
+ else return true;
406
387
  }
407
388
 
408
389
  /**
@@ -412,11 +393,8 @@ export class PrivateEntityManager {
412
393
  * @returns {boolean} true if entityToken is valid.
413
394
  */
414
395
  validateItem(item) {
415
- if (!_.isPlainObject(item)) {
416
- const message = `Invalid item: ${item}`;
417
- this.logger.error(message);
418
- throw new Error(message);
419
- } else return true;
396
+ if (!_.isPlainObject(item)) throw new Error(`Invalid item: ${item}`);
397
+ else return true;
420
398
  }
421
399
 
422
400
  /**
@@ -429,11 +407,9 @@ export class PrivateEntityManager {
429
407
  validateKeyToken(entityToken, keyToken) {
430
408
  this.validateEntityToken(entityToken);
431
409
 
432
- if (!this.config.entities[entityToken].keys[keyToken]) {
433
- const message = `Invalid entity ${entityToken} keyToken: ${keyToken}`;
434
- this.logger.error(message);
435
- throw new Error(message);
436
- } else return true;
410
+ if (!this.config.entities[entityToken].keys[keyToken])
411
+ throw new Error(`Invalid entity ${entityToken} keyToken: ${keyToken}`);
412
+ else return true;
437
413
  }
438
414
 
439
415
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karmaniverous/entity-manager",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },