dynamo-bao 0.1.5 → 0.1.7

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/src/model.js CHANGED
@@ -25,6 +25,12 @@ const {
25
25
 
26
26
  const GID_SEPARATOR = "##__SK__##";
27
27
  const { UNIQUE_CONSTRAINT_KEY, SYSTEM_FIELDS } = require("./constants");
28
+ const {
29
+ ConfigurationError,
30
+ ValidationError,
31
+ QueryError,
32
+ DataFormatError,
33
+ } = require("./exceptions");
28
34
 
29
35
  /**
30
36
  * @description
@@ -83,7 +89,10 @@ class BaoModel {
83
89
  }
84
90
 
85
91
  if (!fieldDef) {
86
- throw new Error(`Field ${fieldName} not found in ${this.name} fields`);
92
+ throw new ConfigurationError(
93
+ `Field ${fieldName} not found in ${this.name} fields`,
94
+ this.name,
95
+ );
87
96
  }
88
97
 
89
98
  return fieldDef;
@@ -91,7 +100,9 @@ class BaoModel {
91
100
 
92
101
  static _getPkValue(data) {
93
102
  if (!data) {
94
- throw new Error("Data object is required for static _getPkValue call");
103
+ throw new ValidationError(
104
+ "Data object is required for static _getPkValue call",
105
+ );
95
106
  }
96
107
 
97
108
  const pkValue =
@@ -106,7 +117,9 @@ class BaoModel {
106
117
 
107
118
  static _getSkValue(data) {
108
119
  if (!data) {
109
- throw new Error("Data object is required for static _getSkValue call");
120
+ throw new ValidationError(
121
+ "Data object is required for static _getSkValue call",
122
+ );
110
123
  }
111
124
 
112
125
  if (this.primaryKey.sk === "modelPrefix") {
@@ -228,18 +241,26 @@ class BaoModel {
228
241
  // dynamo string keys. No test prefix or model prefix is applied.
229
242
  static _getPrimaryKeyValues(data) {
230
243
  if (!data) {
231
- throw new Error("Data object is required for _getPrimaryKeyValues call");
244
+ throw new ValidationError(
245
+ "Data object is required for _getPrimaryKeyValues call",
246
+ );
232
247
  }
233
248
 
234
249
  const pkField = this._getField(this.primaryKey.pk);
235
250
  const skField = this._getField(this.primaryKey.sk);
236
251
 
237
252
  if (skField === undefined && this.primaryKey.sk !== "modelPrefix") {
238
- throw new Error(`SK field is required for getPkSk call`);
253
+ throw new ConfigurationError(
254
+ `SK field is required for getPkSk call`,
255
+ this.name,
256
+ );
239
257
  }
240
258
 
241
259
  if (pkField === undefined && this.primaryKey.pk !== "modelPrefix") {
242
- throw new Error(`PK field is required for getPkSk call`);
260
+ throw new ConfigurationError(
261
+ `PK field is required for getPkSk call`,
262
+ this.name,
263
+ );
243
264
  }
244
265
 
245
266
  // If the field is set, use the GSI value, otherwise use the raw value
@@ -256,7 +277,7 @@ class BaoModel {
256
277
  pkValue === null ||
257
278
  skValue === null
258
279
  ) {
259
- throw new Error(`PK and SK must be defined to get a PkSk`);
280
+ throw new ValidationError(`PK and SK must be defined to get a PkSk`);
260
281
  }
261
282
 
262
283
  let key = {
@@ -269,6 +290,23 @@ class BaoModel {
269
290
  return key;
270
291
  }
271
292
 
293
+ /**
294
+ * @description
295
+ * Make a primary ID from a pk and sk.
296
+ * @param {string} pk - The partition key.
297
+ * @param {string} sk - The sort key.
298
+ * @returns {string} The primary ID.
299
+ */
300
+ static makePrimaryId(pk, sk) {
301
+ if (this.primaryKey.pk === "modelPrefix") {
302
+ return sk;
303
+ } else if (this.primaryKey.sk === "modelPrefix") {
304
+ return pk;
305
+ } else {
306
+ return pk + GID_SEPARATOR + sk;
307
+ }
308
+ }
309
+
272
310
  /**
273
311
  * @description
274
312
  * Static version of {@link BaoModel#getPrimaryId}.
@@ -280,15 +318,7 @@ class BaoModel {
280
318
  const pkSk = this._getPrimaryKeyValues(data);
281
319
  logger.debug("getPrimaryId", pkSk);
282
320
 
283
- let primaryId;
284
- if (this.primaryKey.pk === "modelPrefix") {
285
- primaryId = pkSk.sk;
286
- } else if (this.primaryKey.sk === "modelPrefix") {
287
- primaryId = pkSk.pk;
288
- } else {
289
- primaryId = pkSk.pk + GID_SEPARATOR + pkSk.sk;
290
- }
291
-
321
+ let primaryId = this.makePrimaryId(pkSk.pk, pkSk.sk);
292
322
  return primaryId;
293
323
  }
294
324
 
@@ -306,7 +336,7 @@ class BaoModel {
306
336
 
307
337
  static _parsePrimaryId(primaryId) {
308
338
  if (!primaryId) {
309
- throw new Error("Primary ID is required to parse");
339
+ throw new DataFormatError("Primary ID is required to parse");
310
340
  }
311
341
 
312
342
  if (primaryId.indexOf(GID_SEPARATOR) !== -1) {
@@ -318,7 +348,7 @@ class BaoModel {
318
348
  } else if (this.primaryKey.sk === "modelPrefix") {
319
349
  return { pk: primaryId, sk: this.modelPrefix };
320
350
  } else {
321
- throw new Error(`Invalid primary ID: ${primaryId}`);
351
+ throw new DataFormatError(`Invalid primary ID: ${primaryId}`);
322
352
  }
323
353
  }
324
354
  }
@@ -440,7 +470,10 @@ class BaoModel {
440
470
 
441
471
  const field = this.constructor.fields[fieldName];
442
472
  if (!field || !field.modelName) {
443
- throw new Error(`Field ${fieldName} is not a valid relation field`);
473
+ throw new ConfigurationError(
474
+ `Field ${fieldName} is not a valid relation field`,
475
+ this.constructor.name,
476
+ );
444
477
  }
445
478
 
446
479
  const value = this[fieldName];
@@ -508,7 +541,10 @@ class BaoModel {
508
541
  getRelated(fieldName) {
509
542
  const field = this.constructor.fields[fieldName];
510
543
  if (!(field instanceof RelatedFieldClass)) {
511
- throw new Error(`Field ${fieldName} is not a RelatedField`);
544
+ throw new ConfigurationError(
545
+ `Field ${fieldName} is not a RelatedField`,
546
+ this.constructor.name,
547
+ );
512
548
  }
513
549
  return this._relatedObjects[fieldName];
514
550
  }
@@ -529,13 +565,17 @@ class BaoModel {
529
565
  ) {
530
566
  const constraint = this.uniqueConstraints[constraintName];
531
567
  if (!constraint) {
532
- throw new Error(
568
+ throw new ConfigurationError(
533
569
  `Unknown unique constraint '${constraintName}' in ${this.name}`,
570
+ this.name,
534
571
  );
535
572
  }
536
573
 
537
574
  if (!value) {
538
- throw new Error(`${constraint.field} value is required`);
575
+ throw new ValidationError(
576
+ `${constraint.field} value is required`,
577
+ constraint.field,
578
+ );
539
579
  }
540
580
 
541
581
  const key = this._formatUniqueConstraintKey(
@@ -561,7 +601,7 @@ class BaoModel {
561
601
  const item = await this.find(result.Item.relatedId, { loaderContext });
562
602
 
563
603
  if (item) {
564
- item._addConsumedCapacity(result.ConsumedCapacity);
604
+ item._addConsumedCapacity(result.ConsumedCapacity, "read");
565
605
  }
566
606
 
567
607
  return item;
@@ -584,30 +624,30 @@ class BaoModel {
584
624
  this._addConsumedCapacity(capacity, type, fromContext);
585
625
  }
586
626
 
587
- _addConsumedCapacity(capacity, type = "read", fromContext = false) {
588
- if (type !== "read" && type !== "write" && type !== "total") {
589
- throw new Error(`Invalid consumed capacity type: ${type}`);
627
+ _addConsumedCapacity(consumedCapacity, type, isRelated = false) {
628
+ if (!["read", "write"].includes(type)) {
629
+ throw new ValidationError(`Invalid consumed capacity type: ${type}`);
590
630
  }
591
631
 
592
- if (!capacity) {
632
+ if (!consumedCapacity) {
593
633
  return;
594
634
  }
595
635
 
596
- if (Array.isArray(capacity)) {
597
- capacity.forEach((item) =>
598
- this._addConsumedCapacity(item, type, fromContext),
636
+ if (Array.isArray(consumedCapacity)) {
637
+ consumedCapacity.forEach((item) =>
638
+ this._addConsumedCapacity(item, type, isRelated),
599
639
  );
600
640
  } else {
601
- if (capacity.consumedCapacity) {
641
+ if (consumedCapacity.consumedCapacity) {
602
642
  this._consumedCapacity.push({
603
- consumedCapacity: capacity.consumedCapacity,
604
- fromContext: capacity.fromContext || fromContext,
605
- type: capacity.type || type,
643
+ consumedCapacity: consumedCapacity.consumedCapacity,
644
+ fromContext: consumedCapacity.fromContext || isRelated,
645
+ type: consumedCapacity.type || type,
606
646
  });
607
647
  } else {
608
648
  this._consumedCapacity.push({
609
- consumedCapacity: capacity,
610
- fromContext: fromContext,
649
+ consumedCapacity: consumedCapacity,
650
+ fromContext: isRelated,
611
651
  type: type,
612
652
  });
613
653
  }
@@ -624,8 +664,8 @@ class BaoModel {
624
664
  * @returns {number} The numeric consumed capacity.
625
665
  */
626
666
  getNumericConsumedCapacity(type, includeRelated = false) {
627
- if (type !== "read" && type !== "write" && type !== "total") {
628
- throw new Error(`Invalid consumed capacitytype: ${type}`);
667
+ if (!["read", "write", "total"].includes(type)) {
668
+ throw new ValidationError(`Invalid consumed capacity type: ${type}`);
629
669
  }
630
670
 
631
671
  let consumedCapacity = this._consumedCapacity;