electrodb 1.7.1 → 1.8.1

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/CHANGELOG.md CHANGED
@@ -154,4 +154,20 @@ All notable changes to this project will be documented in this file. Breaking ch
154
154
 
155
155
  ## [1.7.1] - 2022-03-19
156
156
  ### Added
157
- - Adding support for the v3 DyanmoDBClient. This change also brings in a new ElectroDB dependency [@aws-sdk/lib-dynamodb](https://www.npmjs.com/package/@aws-sdk/client-dynamodb). [[read more](./README.md#aws-dynamodb-client)]
157
+ - Adding support for the v3 DyanmoDBClient. This change also brings in a new ElectroDB dependency [@aws-sdk/lib-dynamodb](https://www.npmjs.com/package/@aws-sdk/client-dynamodb). [[read more](./README.md#aws-dynamodb-client)]
158
+
159
+ ## [1.7.2] - 2022-03-27
160
+ ### Fixed
161
+ - Fixed issue#111, `update` method specific query option typing no longer lost when using a `where` method in a query chain
162
+ - Fixing incorrect typing for exposed `UpdateEntityItem` type. Exported type was missing composite key attributes
163
+
164
+ ## [1.8.0] - 2022-03-28
165
+ ### Added
166
+ - Expected typings for the injected v2 client now include methods for `transactWrite` and `transactGet`
167
+ ### Changed
168
+ - Map attributes will now always resolve to least an empty object on a `create` and `put` methods (instead of just the root map)
169
+ - In the past, default values for property attributes on maps only resolves when a user provided an object to place the values on. Now default values within maps attributes will now always resolve onto the object on `create` and `put` methods.
170
+
171
+ ## [1.8.1] - 2022-03-29
172
+ ### Fixed
173
+ - Solidifying default application methodology: default values for nested properties will be applied up until an undefined default occurs or default callback returns undefined
package/README.md CHANGED
@@ -459,7 +459,7 @@ const EmployeesModel = {
459
459
  attributes: {
460
460
  employee: {
461
461
  type: "string",
462
- default: () => uuidv4(),
462
+ default: () => uuid(),
463
463
  },
464
464
  firstName: {
465
465
  type: "string",
@@ -686,9 +686,9 @@ attributes: {
686
686
  Property | Type | Required | Types | Description
687
687
  ------------ | :--------------------------------------------------------: | :------: | :-------: | -----------
688
688
  `type` | `string`, `ReadonlyArray<string>`, `string[]` | yes | all | Accepts the values: `"string"`, `"number"` `"boolean"`, `"map"`, `"list"`, `"set"`, an array of strings representing a finite list of acceptable values: `["option1", "option2", "option3"]`, or `"any"` which disables value type checking on that attribute.
689
- `required` | `boolean` | no | all | Flag an attribute as required to be present when creating a record. This attribute also acts as a type of `NOT NULL` flag, preventing it from being removed directly.
689
+ `required` | `boolean` | no | all | Flag an attribute as required to be present when creating a record. This attribute also acts as a type of `NOT NULL` flag, preventing it from being removed directly. When applied to nested properties, be mindful that default map values can cause required child attributes to fail validation.
690
690
  `hidden` | `boolean` | no | all | Flag an attribute as hidden to remove the property from results before they are returned.
691
- `default` | `value`, `() => value` | no | all | Either the default value itself or a synchronous function that returns the desired value. Applied before `set` and before `required` check.
691
+ `default` | `value`, `() => value` | no | all | Either the default value itself or a synchronous function that returns the desired value. Applied before `set` and before `required` check. In the case of nested attributes, default values will apply defaults to children attributes until an undefined value is reached
692
692
  `validate` | `RegExp`, `(value: any) => void`, `(value: any) => string` | no | all | Either regex or a synchronous callback to return an error string (will result in exception using the string as the error's message), or thrown exception in the event of an error.
693
693
  `field` | `string` | no | all | The name of the attribute as it exists in DynamoDB, if named differently in the schema attributes. Defaults to the `AttributeName` as defined in the schema.
694
694
  `readOnly` | `boolean` | no | all | Prevents an attribute from being updated after the record has been created. Attributes used in the composition of the table's primary Partition Key and Sort Key are read-only by default. The one exception to `readOnly` is for properties that also use the `watch` property, read [attribute watching](#attribute-watching) for more detail.
package/index.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ type Flatten<T> = T extends any[]
2
+ ? T
3
+ : T extends object
4
+ ? {[Key in keyof T]: Flatten<T[Key]>}
5
+ : T;
6
+
1
7
  declare const WhereSymbol: unique symbol;
2
8
  declare const UpdateDataSymbol: unique symbol;
3
9
 
@@ -987,6 +993,13 @@ interface UpdateQueryOptions extends QueryOptions {
987
993
  response?: "default" | "none" | 'all_old' | 'updated_old' | 'all_new' | 'updated_new';
988
994
  }
989
995
 
996
+ interface UpdateQueryParams {
997
+ response?: "default" | "none" | 'all_old' | 'updated_old' | 'all_new' | 'updated_new';
998
+ table?: string;
999
+ params?: object;
1000
+ originalErr?: boolean;
1001
+ }
1002
+
990
1003
  interface DeleteQueryOptions extends QueryOptions {
991
1004
  response?: "default" | "none" | 'all_old';
992
1005
  }
@@ -1042,7 +1055,13 @@ type SingleRecordOperationOptions<A extends string, F extends A, C extends strin
1042
1055
  type PutRecordOperationOptions<A extends string, F extends A, C extends string, S extends Schema<A,F,C>, ResponseType> = {
1043
1056
  go: GoRecord<ResponseType, PutQueryOptions>;
1044
1057
  params: ParamRecord<PutQueryOptions>;
1045
- where: WhereClause<A,F,C,S,Item<A,F,C,S,S["attributes"]>,SingleRecordOperationOptions<A,F,C,S,ResponseType>>;
1058
+ where: WhereClause<A,F,C,S,Item<A,F,C,S,S["attributes"]>,PutRecordOperationOptions<A,F,C,S,ResponseType>>;
1059
+ };
1060
+
1061
+ type UpdateRecordOperationOptions<A extends string, F extends A, C extends string, S extends Schema<A,F,C>, ResponseType> = {
1062
+ go: GoRecord<ResponseType, UpdateQueryOptions>;
1063
+ params: ParamRecord<UpdateQueryParams>;
1064
+ where: WhereClause<A,F,C,S,Item<A,F,C,S,S["attributes"]>,PutRecordOperationOptions<A,F,C,S,ResponseType>>;
1046
1065
  };
1047
1066
 
1048
1067
  type DeleteRecordOperationOptions<A extends string, F extends A, C extends string, S extends Schema<A,F,C>, ResponseType> = {
@@ -1058,7 +1077,7 @@ type BulkRecordOperationOptions<A extends string, F extends A, C extends string,
1058
1077
 
1059
1078
  type SetRecordActionOptions<A extends string, F extends A, C extends string, S extends Schema<A,F,C>, SetAttr,IndexCompositeAttributes,TableItem> = {
1060
1079
  go: GoRecord<Partial<TableItem>, UpdateQueryOptions>;
1061
- params: ParamRecord<UpdateQueryOptions>;
1080
+ params: ParamRecord<UpdateQueryParams>;
1062
1081
  set: SetRecord<A,F,C,S, SetItem<A,F,C,S>,IndexCompositeAttributes,TableItem>;
1063
1082
  remove: SetRecord<A,F,C,S, Array<keyof SetItem<A,F,C,S>>,IndexCompositeAttributes,TableItem>;
1064
1083
  add: SetRecord<A,F,C,S, AddItem<A,F,C,S>,IndexCompositeAttributes,TableItem>;
@@ -1066,7 +1085,7 @@ type SetRecordActionOptions<A extends string, F extends A, C extends string, S e
1066
1085
  append: SetRecord<A,F,C,S, AppendItem<A,F,C,S>,IndexCompositeAttributes,TableItem>;
1067
1086
  delete: SetRecord<A,F,C,S, DeleteItem<A,F,C,S>,IndexCompositeAttributes,TableItem>;
1068
1087
  data: DataUpdateMethodRecord<A,F,C,S, Item<A,F,C,S,S["attributes"]>,IndexCompositeAttributes,TableItem>;
1069
- where: WhereClause<A,F,C,S, Item<A,F,C,S,S["attributes"]>,RecordsActionOptions<A,F,C,S,TableItem,IndexCompositeAttributes>>;
1088
+ where: WhereClause<A,F,C,S, Item<A,F,C,S,S["attributes"]>,SetRecordActionOptions<A,F,C,S,SetAttr,IndexCompositeAttributes,TableItem>>;
1070
1089
  }
1071
1090
 
1072
1091
  type SetRecord<A extends string, F extends A, C extends string, S extends Schema<A,F,C>, SetAttr, IndexCompositeAttributes, TableItem> = (properties: SetAttr) => SetRecordActionOptions<A,F,C,S, SetAttr, IndexCompositeAttributes, TableItem>;
@@ -1120,6 +1139,8 @@ type DocumentClient = {
1120
1139
  batchWrite: DocumentClientMethod;
1121
1140
  batchGet: DocumentClientMethod;
1122
1141
  scan: DocumentClientMethod;
1142
+ transactGet: DocumentClientMethod;
1143
+ transactWrite: DocumentClientMethod;
1123
1144
  } | {
1124
1145
  send: (command: any) => Promise<any>;
1125
1146
  }
@@ -1482,7 +1503,7 @@ export type CreateEntityItem<E extends Entity<any, any, any, any>> =
1482
1503
 
1483
1504
  export type UpdateEntityItem<E extends Entity<any, any, any, any>> =
1484
1505
  E extends Entity<infer A, infer F, infer C, infer S>
1485
- ? SetItem<A, F, C, S>
1506
+ ? Partial<ResponseItem<A,F,C,S>>
1486
1507
  : never;
1487
1508
 
1488
1509
  export type UpdateAddEntityItem<E extends Entity<any, any, any, any>> =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electrodb",
3
- "version": "1.7.1",
3
+ "version": "1.8.1",
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/client.js CHANGED
@@ -9,7 +9,7 @@ const DocumentClientVersions = {
9
9
  };
10
10
 
11
11
  const v3Methods = ['send'];
12
- const v2Methods = ['get', 'put', 'update', 'delete', 'batchWrite', 'batchGet', 'scan', 'query', 'createSet'];
12
+ const v2Methods = ['get', 'put', 'update', 'delete', 'batchWrite', 'batchGet', 'scan', 'query', 'createSet', 'transactWrite', 'transactGet'];
13
13
  const supportedClientVersions = {
14
14
  [DocumentClientVersions.v2]: v2Methods,
15
15
  [DocumentClientVersions.v3]: v3Methods,
@@ -81,6 +81,18 @@ class DocumentClientV3Wrapper {
81
81
  return this.client.send(command);
82
82
  });
83
83
  }
84
+ transactWrite(params) {
85
+ return this.promiseWrap(async () => {
86
+ const command = new this.lib.TransactWriteCommand(params);
87
+ return this.client.send(command);
88
+ });
89
+ }
90
+ transactGet(params) {
91
+ return this.promiseWrap(async () => {
92
+ const command = new this.lib.TransactGetCommand(params);
93
+ return this.client.send(command);
94
+ });
95
+ }
84
96
  createSet(value) {
85
97
  if (Array.isArray(value)) {
86
98
  return new Set(value);
package/src/schema.js CHANGED
@@ -575,30 +575,30 @@ class MapAttribute extends Attribute {
575
575
  }
576
576
  return v;
577
577
  }
578
- const valueType = getValueType(value);
579
- if (value === undefined) {
580
- return getValue(value);
581
- } else if (value && valueType !== "object" && Object.keys(value).length === 0) {
582
- return getValue(value);
578
+
579
+ let data = value === undefined
580
+ ? getValue(value)
581
+ : value;
582
+
583
+ const valueType = getValueType(data);
584
+
585
+ if (data === undefined) {
586
+ return data;
583
587
  } else if (valueType !== "object") {
584
588
  throw new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Expected value to be an object to fulfill attribute type "${this.type}"`);
585
589
  }
586
590
 
587
- const data = {};
591
+ const response = {};
588
592
 
589
593
  for (const name of Object.keys(this.properties.attributes)) {
590
594
  const attribute = this.properties.attributes[name];
591
- const results = attribute.val(value[attribute.name]);
595
+ const results = attribute.val(data[attribute.name]);
592
596
  if (results !== undefined) {
593
- data[name] = results;
597
+ response[name] = results;
594
598
  }
595
599
  }
596
600
 
597
- if (Object.keys(data).length > 0) {
598
- return getValue(data);
599
- } else {
600
- return getValue();
601
- }
601
+ return response;
602
602
  }
603
603
  }
604
604
 
@@ -726,28 +726,25 @@ class ListAttribute extends Attribute {
726
726
  return v;
727
727
  }
728
728
 
729
- if (value === undefined) {
730
- return this.default();
731
- } else if (Array.isArray(value) && value.length === 0) {
732
- return value;
733
- } else if (!Array.isArray(value)) {
729
+ const data = value === undefined
730
+ ? getValue(value)
731
+ : value;
732
+
733
+ if (data === undefined) {
734
+ return data;
735
+ } else if (!Array.isArray(data)) {
734
736
  throw new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path "${this.path}. Received value of type "${getValueType(value)}", expected value of type "array"`);
735
737
  }
736
738
 
737
- const data = [];
738
-
739
- for (const v of value) {
740
- const results = this.items.val(v);
739
+ const response = [];
740
+ for (const d of data) {
741
+ const results = this.items.val(d);
741
742
  if (results !== undefined) {
742
- data.push(results);
743
+ response.push(results);
743
744
  }
744
745
  }
745
746
 
746
- if (data.filter(value => value !== undefined).length > 0) {
747
- return getValue(data);
748
- } else {
749
- return getValue();
750
- }
747
+ return response;
751
748
  }
752
749
  }
753
750