electrodb 3.4.2 → 3.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/index.d.ts CHANGED
@@ -1236,10 +1236,10 @@ export interface RecordsActionOptions<
1236
1236
  F extends string,
1237
1237
  C extends string,
1238
1238
  S extends Schema<A, F, C>,
1239
- Items,
1239
+ ResponseItem,
1240
1240
  IndexCompositeAttributes,
1241
1241
  > {
1242
- go: QueryRecordsGo<Items>;
1242
+ go: QueryRecordsGo<ResponseItem>;
1243
1243
  params: ParamRecord;
1244
1244
  where: WhereClause<
1245
1245
  A,
@@ -1247,7 +1247,7 @@ export interface RecordsActionOptions<
1247
1247
  C,
1248
1248
  S,
1249
1249
  Item<A, F, C, S, S["attributes"]>,
1250
- RecordsActionOptions<A, F, C, S, Items, IndexCompositeAttributes>
1250
+ RecordsActionOptions<A, F, C, S, ResponseItem, IndexCompositeAttributes>
1251
1251
  >;
1252
1252
  }
1253
1253
 
@@ -2854,11 +2854,16 @@ export type ServiceQueryRecordsGo<
2854
2854
  options?: Options,
2855
2855
  ) => Promise<{ data: T; cursor: string | null }>;
2856
2856
 
2857
- export type QueryRecordsGo<ResponseType, Options = QueryOptions> = <
2858
- T = ResponseType,
2859
- >(
2857
+ export type QueryRecordsGo<Item> = <Options extends GoQueryTerminalOptions<keyof Item>>(
2860
2858
  options?: Options,
2861
- ) => Promise<{ data: T; cursor: string | null }>;
2859
+ ) => Options extends GoQueryTerminalOptions<infer Attr>
2860
+ ? Promise<{
2861
+ data: Array<{
2862
+ [Name in keyof Item as Name extends Attr ? Name : never]: Item[Name];
2863
+ }>;
2864
+ cursor: string | null;
2865
+ }>
2866
+ : Promise<{ data: Array<Item>; cursor: string | null }>;
2862
2867
 
2863
2868
  export type UpdateRecordGo<ResponseType, Keys> = <
2864
2869
  T = ResponseType,
@@ -5254,7 +5259,7 @@ export class Entity<
5254
5259
  F,
5255
5260
  C,
5256
5261
  S,
5257
- ResponseItem<A, F, C, S>[],
5262
+ ResponseItem<A, F, C, S>,
5258
5263
  AllTableIndexCompositeAttributes<A, F, C, S>
5259
5264
  >;
5260
5265
 
@@ -5265,7 +5270,7 @@ export class Entity<
5265
5270
  F,
5266
5271
  C,
5267
5272
  S,
5268
- ResponseItem<A, F, C, S>[],
5273
+ ResponseItem<A, F, C, S>,
5269
5274
  AllTableIndexCompositeAttributes<A, F, C, S>
5270
5275
  >;
5271
5276
 
@@ -5274,7 +5279,7 @@ export class Entity<
5274
5279
  F,
5275
5280
  C,
5276
5281
  S,
5277
- ResponseItem<A, F, C, S>[],
5282
+ ResponseItem<A, F, C, S>,
5278
5283
  TableIndexCompositeAttributes<A, F, C, S>
5279
5284
  >;
5280
5285
  query: Queries<A, F, C, S>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electrodb",
3
- "version": "3.4.2",
3
+ "version": "3.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/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 = "" } = {}) {