electrodb 1.4.0 → 1.4.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electrodb",
3
- "version": "1.4.0",
3
+ "version": "1.4.4",
4
4
  "description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/clauses.js CHANGED
@@ -38,7 +38,6 @@ let clauses = {
38
38
  return state;
39
39
  }
40
40
  try {
41
- entity._expectFacets(facets, Object.keys(facets), `"query collection" composite attributes`);
42
41
  const {pk} = state.getCompositeAttributes();
43
42
  return state
44
43
  .setType(QueryTypes.collection)
@@ -381,7 +380,6 @@ let clauses = {
381
380
  }
382
381
  try {
383
382
  const attributes = state.getCompositeAttributes();
384
- entity._expectFacets(facets, Object.keys(facets), `"query" composite attributes`);
385
383
  return state
386
384
  .setMethod(MethodTypes.query)
387
385
  .setType(QueryTypes.is)
@@ -404,16 +402,6 @@ let clauses = {
404
402
  }
405
403
  try {
406
404
  const attributes = state.getCompositeAttributes();
407
- entity._expectFacets(
408
- startingFacets,
409
- Object.keys(startingFacets),
410
- `"between" composite attributes`,
411
- );
412
- entity._expectFacets(
413
- endingFacets,
414
- Object.keys(endingFacets),
415
- `"and" composite attributes`,
416
- );
417
405
  return state
418
406
  .setType(QueryTypes.and)
419
407
  .setSK(entity._buildQueryFacets(endingFacets, attributes.sk))
@@ -433,8 +421,6 @@ let clauses = {
433
421
  return state;
434
422
  }
435
423
  try {
436
- entity._expectFacets(facets, Object.keys(facets), `"begins" composite attributes`);
437
-
438
424
  return state
439
425
  .setType(QueryTypes.begins)
440
426
  .ifSK(state => {
@@ -455,7 +441,6 @@ let clauses = {
455
441
  return state;
456
442
  }
457
443
  try {
458
- entity._expectFacets(facets, Object.keys(facets), `"gt" composite attributes`);
459
444
  return state
460
445
  .setType(QueryTypes.gt)
461
446
  .ifSK(state => {
@@ -476,7 +461,6 @@ let clauses = {
476
461
  return state;
477
462
  }
478
463
  try {
479
- entity._expectFacets(facets, Object.keys(facets), `"gte" composite attributes`);
480
464
  return state
481
465
  .setType(QueryTypes.gte)
482
466
  .ifSK(state => {
@@ -497,7 +481,6 @@ let clauses = {
497
481
  return state;
498
482
  }
499
483
  try {
500
- entity._expectFacets(facets, Object.keys(facets), `"lt" composite attributes`);
501
484
  return state.setType(QueryTypes.lt)
502
485
  .ifSK(state => {
503
486
  const attributes = state.getCompositeAttributes();
@@ -517,7 +500,6 @@ let clauses = {
517
500
  return state;
518
501
  }
519
502
  try {
520
- entity._expectFacets(facets, Object.keys(facets), `"lte" composite attributes`);
521
503
  return state.setType(QueryTypes.lte)
522
504
  .ifSK(state => {
523
505
  const attributes = state.getCompositeAttributes();
package/src/entity.js CHANGED
@@ -1003,6 +1003,7 @@ class Entity {
1003
1003
  }
1004
1004
 
1005
1005
  _makeUpdateParams(update = {}, pk = {}, sk = {}) {
1006
+ let primaryIndexAttributes = {...pk, ...sk};
1006
1007
  let modifiedAttributeValues = {};
1007
1008
  let modifiedAttributeNames = {};
1008
1009
  for (const path of Object.keys(update.paths)) {
@@ -1038,6 +1039,7 @@ class Entity {
1038
1039
  const wasNotAlreadyModified = modifiedAttributeNames[indexKey] === undefined;
1039
1040
  if (isNotTablePK && isNotTableSK && wasNotAlreadyModified) {
1040
1041
  update.set(indexKey, updatedKeys[indexKey]);
1042
+
1041
1043
  }
1042
1044
  }
1043
1045
 
@@ -1050,6 +1052,24 @@ class Entity {
1050
1052
  }
1051
1053
  }
1052
1054
 
1055
+ // This loop adds the composite attributes to the Primary Index. This is important
1056
+ // in the case an update results in an "upsert". We want to add the Primary Index
1057
+ // composite attributes to the update so they will be included on the item when it
1058
+ // is created. It is done after all of the above because it is not a true "update"
1059
+ // so it should not be subject to the above "rules".
1060
+ for (const primaryIndexAttribute of Object.keys(primaryIndexAttributes)) {
1061
+ // isNotTablePK and isNotTableSK is important to check in case these properties
1062
+ // are not also the name of the index (you cannot modify the PK or SK of an item
1063
+ // after its creation)
1064
+ const attribute = this.model.schema.attributes[primaryIndexAttribute];
1065
+ const isNotTablePK = !!(attribute && attribute.field !== this.model.indexes[accessPattern].pk.field);
1066
+ const isNotTableSK = !!(attribute && attribute.field !== this.model.indexes[accessPattern].sk.field);
1067
+ const wasNotAlreadyModified = modifiedAttributeNames[primaryIndexAttribute] === undefined;
1068
+ if (isNotTablePK && isNotTableSK && wasNotAlreadyModified) {
1069
+ update.set(primaryIndexAttribute, primaryIndexAttributes[primaryIndexAttribute]);
1070
+ }
1071
+ }
1072
+
1053
1073
  return {
1054
1074
  UpdateExpression: update.build(),
1055
1075
  ExpressionAttributeNames: update.getNames(),
@@ -1095,12 +1115,14 @@ class Entity {
1095
1115
  let restrict = ["pk"];
1096
1116
  let keys = { pk };
1097
1117
  sks = sks.filter(sk => sk !== undefined);
1118
+
1098
1119
  for (let i = 0; i < sks.length; i++) {
1099
1120
  let id = `sk${i + 1}`;
1100
1121
  keys[id] = sks[i];
1101
1122
  restrict.push(id);
1102
1123
  translate[id] = translate["sk"];
1103
1124
  }
1125
+
1104
1126
  let keyExpressions = this._expressionAttributeBuilder(keys, ItemOperations.set, {
1105
1127
  translate,
1106
1128
  restrict,
@@ -1427,7 +1449,7 @@ class Entity {
1427
1449
  { ...keyAttributes },
1428
1450
  );
1429
1451
  const removedKeyImpact = this._expectIndexFacets(
1430
- {...removed},
1452
+ { ...removed },
1431
1453
  {...keyAttributes}
1432
1454
  )
1433
1455
 
package/src/schema.js CHANGED
@@ -332,8 +332,11 @@ class Attribute {
332
332
  };
333
333
  } else if (definition instanceof RegExp) {
334
334
  return (val) => {
335
+ if (val === undefined) {
336
+ return [true, ""];
337
+ }
335
338
  let isValid = definition.test(val);
336
- let reason = isValid ? "" : `Invalid value for attribute "${this.path}": Failed user defined regex`;
339
+ let reason = isValid ? "" : `Invalid value for attribute "${this.path}": Failed model defined regex`;
337
340
  return [isValid, reason];
338
341
  };
339
342
  } else {