electrodb 3.4.3 → 3.4.5

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": "3.4.3",
3
+ "version": "3.4.5",
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/entity.js CHANGED
@@ -2012,7 +2012,10 @@ class Entity {
2012
2012
  );
2013
2013
  break;
2014
2014
  case MethodTypes.scan:
2015
- params = this._makeScanParam(filter[ExpressionTypes.FilterExpression]);
2015
+ params = this._makeScanParam(
2016
+ filter[ExpressionTypes.FilterExpression],
2017
+ config,
2018
+ );
2016
2019
  break;
2017
2020
  /* istanbul ignore next */
2018
2021
  default:
@@ -2228,7 +2231,7 @@ class Entity {
2228
2231
  }
2229
2232
 
2230
2233
  /* istanbul ignore next */
2231
- _makeScanParam(filter = {}) {
2234
+ _makeScanParam(filter = {}, options = {}) {
2232
2235
  let indexBase = TableIndex;
2233
2236
  let hasSortKey = this.model.lookup.indexHasSortKeys[indexBase];
2234
2237
  let accessPattern =
@@ -2291,7 +2294,10 @@ class Entity {
2291
2294
  params.FilterExpression = filterExpressions.join(" AND ");
2292
2295
  }
2293
2296
 
2294
- return params;
2297
+ return this._applyProjectionExpressions({
2298
+ parameters: params,
2299
+ config: options,
2300
+ });
2295
2301
  }
2296
2302
 
2297
2303
  _makeSimpleIndexParams(partition, sort) {
package/src/schema.js CHANGED
@@ -268,19 +268,23 @@ class Attribute {
268
268
 
269
269
  _makeGet(get) {
270
270
  this._checkGetSet(get, "get");
271
- const getter = get || ((attr) => attr);
272
- return (value, siblings) => {
271
+ const getter = get
272
+ ? (value, getSiblings) => get(value, getSiblings())
273
+ : (attr) => attr;
274
+ return (value, getSiblings) => {
273
275
  if (this.hidden) {
274
276
  return;
275
277
  }
276
278
  value = this.unformat(value);
277
- return getter(value, siblings);
279
+ return getter(value, getSiblings);
278
280
  };
279
281
  }
280
282
 
281
283
  _makeSet(set) {
282
284
  this._checkGetSet(set, "set");
283
- return set || ((attr) => attr);
285
+ return set
286
+ ? (value, getSiblings) => set(value, getSiblings())
287
+ : (attr) => attr;
284
288
  }
285
289
 
286
290
  _makeApplyFixings({
@@ -664,19 +668,19 @@ class MapAttribute extends Attribute {
664
668
 
665
669
  _makeGet(get, properties) {
666
670
  this._checkGetSet(get, "get");
667
- const getter =
668
- get ||
669
- ((val) => {
670
- const isEmpty = !val || Object.keys(val).length === 0;
671
- const isNotRequired = !this.required;
672
- const doesNotHaveDefault = this.default === undefined;
673
- const isRoot = this.isRoot;
674
- if (isEmpty && isRoot && isNotRequired && doesNotHaveDefault) {
675
- return undefined;
676
- }
677
- return val;
678
- });
679
- return (values, siblings) => {
671
+ const getter = get
672
+ ? (value, getSiblings) => get(value, getSiblings())
673
+ : (val) => {
674
+ const isEmpty = !val || Object.keys(val).length === 0;
675
+ const isNotRequired = !this.required;
676
+ const doesNotHaveDefault = this.default === undefined;
677
+ const isRoot = this.isRoot;
678
+ if (isEmpty && isRoot && isNotRequired && doesNotHaveDefault) {
679
+ return undefined;
680
+ }
681
+ return val;
682
+ };
683
+ return (values, getSiblings) => {
680
684
  const data = {};
681
685
 
682
686
  if (this.hidden) {
@@ -687,60 +691,62 @@ class MapAttribute extends Attribute {
687
691
  if (!get) {
688
692
  return undefined;
689
693
  }
690
- return getter(data, siblings);
694
+ return getter(data, getSiblings);
691
695
  }
692
696
 
693
697
  for (const name of Object.keys(properties.attributes)) {
694
698
  const attribute = properties.attributes[name];
695
699
  if (values[attribute.field] !== undefined) {
696
- let results = attribute.get(values[attribute.field], { ...values });
700
+ let results = attribute.get(values[attribute.field], () => ({
701
+ ...values,
702
+ }));
697
703
  if (results !== undefined) {
698
704
  data[name] = results;
699
705
  }
700
706
  }
701
707
  }
702
708
 
703
- return getter(data, siblings);
709
+ return getter(data, getSiblings);
704
710
  };
705
711
  }
706
712
 
707
713
  _makeSet(set, properties) {
708
714
  this._checkGetSet(set, "set");
709
- const setter =
710
- set ||
711
- ((val) => {
712
- const isEmpty = !val || Object.keys(val).length === 0;
713
- const isNotRequired = !this.required;
714
- const doesNotHaveDefault = this.default === undefined;
715
- const defaultIsValue = this.default === val;
716
- const isRoot = this.isRoot;
717
- if (defaultIsValue) {
718
- return val;
719
- } else if (isEmpty && isRoot && isNotRequired && doesNotHaveDefault) {
720
- return undefined;
721
- } else {
722
- return val;
723
- }
724
- });
715
+ const setter = set
716
+ ? (val, getSiblings) => set(val, getSiblings())
717
+ : (val) => {
718
+ const isEmpty = !val || Object.keys(val).length === 0;
719
+ const isNotRequired = !this.required;
720
+ const doesNotHaveDefault = this.default === undefined;
721
+ const defaultIsValue = this.default === val;
722
+ const isRoot = this.isRoot;
723
+ if (defaultIsValue) {
724
+ return val;
725
+ } else if (isEmpty && isRoot && isNotRequired && doesNotHaveDefault) {
726
+ return undefined;
727
+ } else {
728
+ return val;
729
+ }
730
+ };
725
731
 
726
- return (values, siblings) => {
732
+ return (values, getSiblings) => {
727
733
  const data = {};
728
734
  if (values === undefined) {
729
735
  if (!set) {
730
736
  return undefined;
731
737
  }
732
- return setter(values, siblings);
738
+ return setter(values, getSiblings);
733
739
  }
734
740
  for (const name of Object.keys(properties.attributes)) {
735
741
  const attribute = properties.attributes[name];
736
742
  if (values[name] !== undefined) {
737
- const results = attribute.set(values[name], { ...values });
743
+ const results = attribute.set(values[name], () => ({ ...values }));
738
744
  if (results !== undefined) {
739
745
  data[attribute.field] = results;
740
746
  }
741
747
  }
742
748
  }
743
- return setter(data, siblings);
749
+ return setter(data, getSiblings);
744
750
  };
745
751
  }
746
752
 
@@ -876,9 +882,11 @@ class ListAttribute extends Attribute {
876
882
  _makeGet(get, items) {
877
883
  this._checkGetSet(get, "get");
878
884
 
879
- const getter = get || ((attr) => attr);
885
+ const getter = get
886
+ ? (value, getSiblings) => get(value, getSiblings())
887
+ : (attr) => attr;
880
888
 
881
- return (values, siblings) => {
889
+ return (values, getSiblings) => {
882
890
  const data = [];
883
891
 
884
892
  if (this.hidden) {
@@ -886,38 +894,40 @@ class ListAttribute extends Attribute {
886
894
  }
887
895
 
888
896
  if (values === undefined) {
889
- return getter(data, siblings);
897
+ return getter(data, getSiblings);
890
898
  }
891
899
 
892
900
  for (let value of values) {
893
- const results = items.get(value, [...values]);
901
+ const results = items.get(value, () => [...values]);
894
902
  if (results !== undefined) {
895
903
  data.push(results);
896
904
  }
897
905
  }
898
906
 
899
- return getter(data, siblings);
907
+ return getter(data, getSiblings);
900
908
  };
901
909
  }
902
910
 
903
911
  _makeSet(set, items) {
904
912
  this._checkGetSet(set, "set");
905
- const setter = set || ((attr) => attr);
906
- return (values, siblings) => {
913
+ const setter = set
914
+ ? (value, getSiblings) => set(value, getSiblings())
915
+ : (attr) => attr;
916
+ return (values, getSiblings) => {
907
917
  const data = [];
908
918
 
909
919
  if (values === undefined) {
910
- return setter(values, siblings);
920
+ return setter(values, getSiblings);
911
921
  }
912
922
 
913
923
  for (const value of values) {
914
- const results = items.set(value, [...values]);
924
+ const results = items.set(value, () => [...values]);
915
925
  if (results !== undefined) {
916
926
  data.push(results);
917
927
  }
918
928
  }
919
929
 
920
- return setter(data, siblings);
930
+ return setter(data, getSiblings);
921
931
  };
922
932
  }
923
933
 
@@ -1111,14 +1121,16 @@ class SetAttribute extends Attribute {
1111
1121
 
1112
1122
  _makeGet(get, items) {
1113
1123
  this._checkGetSet(get, "get");
1114
- const getter = get || ((attr) => attr);
1115
- return (values, siblings) => {
1124
+ const getter = get
1125
+ ? (value, getSiblings) => get(value, getSiblings())
1126
+ : (attr) => attr;
1127
+ return (values, getSiblings) => {
1116
1128
  if (values !== undefined) {
1117
1129
  const data = this.fromDDBSet(values);
1118
- return getter(data, siblings);
1130
+ return getter(data, getSiblings);
1119
1131
  }
1120
1132
  const data = this.fromDDBSet(values);
1121
- const results = getter(data, siblings);
1133
+ const results = getter(data, getSiblings);
1122
1134
  if (results !== undefined) {
1123
1135
  // if not undefined, try to convert, else no need to return
1124
1136
  return this.fromDDBSet(results);
@@ -1128,9 +1140,11 @@ class SetAttribute extends Attribute {
1128
1140
 
1129
1141
  _makeSet(set, items) {
1130
1142
  this._checkGetSet(set, "set");
1131
- const setter = set || ((attr) => attr);
1132
- return (values, siblings) => {
1133
- const results = setter(this.fromDDBSet(values), siblings);
1143
+ const setter = set
1144
+ ? (value, getSiblings) => set(value, getSiblings())
1145
+ : (attr) => attr;
1146
+ return (values, getSiblings) => {
1147
+ const results = setter(this.fromDDBSet(values), getSiblings);
1134
1148
  if (results !== undefined) {
1135
1149
  return this.toDDBSet(results);
1136
1150
  }
@@ -1616,12 +1630,13 @@ class Schema {
1616
1630
 
1617
1631
  _applyAttributeMutation(method, include, avoid, payload) {
1618
1632
  let data = { ...payload };
1633
+ const getSiblings = () => ({ ...payload });
1619
1634
  for (let path of Object.keys(include)) {
1620
1635
  // this.attributes[attribute] !== undefined | Attribute exists as actual attribute. If `includeKeys` is turned on for example this will include values that do not have a presence in the model and therefore will not have a `.get()` method
1621
1636
  // avoid[attribute] === undefined | Attribute shouldn't be in the avoided
1622
1637
  const attribute = this.getAttribute(path);
1623
1638
  if (attribute !== undefined && avoid[path] === undefined) {
1624
- data[path] = attribute[method](payload[path], { ...payload });
1639
+ data[path] = attribute[method](payload[path], getSiblings);
1625
1640
  }
1626
1641
  }
1627
1642
  return data;
package/src/util.js CHANGED
@@ -209,19 +209,16 @@ const cursorFormatter = {
209
209
  };
210
210
 
211
211
  function removeFixings({ prefix = "", postfix = "", value = "" } = {}) {
212
- const start = value.toLowerCase().startsWith(prefix.toLowerCase())
213
- ? prefix.length
214
- : 0;
212
+ if (prefix === "" && postfix === "") return value;
213
+
214
+ const valueLower = value.toLowerCase();
215
+
216
+ const start = valueLower.startsWith(prefix.toLowerCase()) ? prefix.length : 0;
215
217
  const end =
216
218
  value.length -
217
- (value.toLowerCase().endsWith(postfix.toLowerCase()) ? postfix.length : 0);
218
-
219
- let formatted = "";
220
- for (let i = start; i < end; i++) {
221
- formatted += value[i];
222
- }
219
+ (valueLower.endsWith(postfix.toLowerCase()) ? postfix.length : 0);
223
220
 
224
- return formatted;
221
+ return value.slice(start, end);
225
222
  }
226
223
 
227
224
  function addPadding({ padding = {}, value = "" } = {}) {