dyna-record 0.0.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.
Files changed (239) hide show
  1. package/README.md +3 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +17 -0
  5. package/dist/src/DynaRecord.d.ts +216 -0
  6. package/dist/src/DynaRecord.d.ts.map +1 -0
  7. package/dist/src/DynaRecord.js +217 -0
  8. package/dist/src/Logger.d.ts +42 -0
  9. package/dist/src/Logger.d.ts.map +1 -0
  10. package/dist/src/Logger.js +57 -0
  11. package/dist/src/decorators/Entity.d.ts +23 -0
  12. package/dist/src/decorators/Entity.d.ts.map +1 -0
  13. package/dist/src/decorators/Entity.js +32 -0
  14. package/dist/src/decorators/Table.d.ts +22 -0
  15. package/dist/src/decorators/Table.d.ts.map +1 -0
  16. package/dist/src/decorators/Table.js +31 -0
  17. package/dist/src/decorators/attributes/Attribute.d.ts +26 -0
  18. package/dist/src/decorators/attributes/Attribute.d.ts.map +1 -0
  19. package/dist/src/decorators/attributes/Attribute.js +41 -0
  20. package/dist/src/decorators/attributes/DateAttribute.d.ts +25 -0
  21. package/dist/src/decorators/attributes/DateAttribute.d.ts.map +1 -0
  22. package/dist/src/decorators/attributes/DateAttribute.js +43 -0
  23. package/dist/src/decorators/attributes/DateNullableAttribute.d.ts +26 -0
  24. package/dist/src/decorators/attributes/DateNullableAttribute.d.ts.map +1 -0
  25. package/dist/src/decorators/attributes/DateNullableAttribute.js +43 -0
  26. package/dist/src/decorators/attributes/ForeignKeyAttribute.d.ts +25 -0
  27. package/dist/src/decorators/attributes/ForeignKeyAttribute.d.ts.map +1 -0
  28. package/dist/src/decorators/attributes/ForeignKeyAttribute.js +40 -0
  29. package/dist/src/decorators/attributes/NullableAttribute.d.ts +27 -0
  30. package/dist/src/decorators/attributes/NullableAttribute.d.ts.map +1 -0
  31. package/dist/src/decorators/attributes/NullableAttribute.js +41 -0
  32. package/dist/src/decorators/attributes/NullableForeignKeyAttribute.d.ts +25 -0
  33. package/dist/src/decorators/attributes/NullableForeignKeyAttribute.d.ts.map +1 -0
  34. package/dist/src/decorators/attributes/NullableForeignKeyAttribute.js +40 -0
  35. package/dist/src/decorators/attributes/PartitionKeyAttribute.d.ts +24 -0
  36. package/dist/src/decorators/attributes/PartitionKeyAttribute.d.ts.map +1 -0
  37. package/dist/src/decorators/attributes/PartitionKeyAttribute.js +38 -0
  38. package/dist/src/decorators/attributes/SortKeyAttribute.d.ts +24 -0
  39. package/dist/src/decorators/attributes/SortKeyAttribute.d.ts.map +1 -0
  40. package/dist/src/decorators/attributes/SortKeyAttribute.js +38 -0
  41. package/dist/src/decorators/attributes/index.d.ts +10 -0
  42. package/dist/src/decorators/attributes/index.d.ts.map +1 -0
  43. package/dist/src/decorators/attributes/index.js +37 -0
  44. package/dist/src/decorators/attributes/serializers.d.ts +13 -0
  45. package/dist/src/decorators/attributes/serializers.d.ts.map +1 -0
  46. package/dist/src/decorators/attributes/serializers.js +19 -0
  47. package/dist/src/decorators/index.d.ts +6 -0
  48. package/dist/src/decorators/index.d.ts.map +1 -0
  49. package/dist/src/decorators/index.js +27 -0
  50. package/dist/src/decorators/relationships/BelongsTo.d.ts +34 -0
  51. package/dist/src/decorators/relationships/BelongsTo.d.ts.map +1 -0
  52. package/dist/src/decorators/relationships/BelongsTo.js +50 -0
  53. package/dist/src/decorators/relationships/HasAndBelongsToMany.d.ts +80 -0
  54. package/dist/src/decorators/relationships/HasAndBelongsToMany.d.ts.map +1 -0
  55. package/dist/src/decorators/relationships/HasAndBelongsToMany.js +70 -0
  56. package/dist/src/decorators/relationships/HasMany.d.ts +35 -0
  57. package/dist/src/decorators/relationships/HasMany.d.ts.map +1 -0
  58. package/dist/src/decorators/relationships/HasMany.js +48 -0
  59. package/dist/src/decorators/relationships/HasOne.d.ts +35 -0
  60. package/dist/src/decorators/relationships/HasOne.d.ts.map +1 -0
  61. package/dist/src/decorators/relationships/HasOne.js +48 -0
  62. package/dist/src/decorators/relationships/index.d.ts +6 -0
  63. package/dist/src/decorators/relationships/index.d.ts.map +1 -0
  64. package/dist/src/decorators/relationships/index.js +29 -0
  65. package/dist/src/decorators/relationships/types.d.ts +11 -0
  66. package/dist/src/decorators/relationships/types.d.ts.map +1 -0
  67. package/dist/src/decorators/relationships/types.js +2 -0
  68. package/dist/src/decorators/types.d.ts +30 -0
  69. package/dist/src/decorators/types.d.ts.map +1 -0
  70. package/dist/src/decorators/types.js +2 -0
  71. package/dist/src/dynamo-utils/DynamoClient.d.ts +34 -0
  72. package/dist/src/dynamo-utils/DynamoClient.d.ts.map +1 -0
  73. package/dist/src/dynamo-utils/DynamoClient.js +57 -0
  74. package/dist/src/dynamo-utils/TransactGetBuilder.d.ts +18 -0
  75. package/dist/src/dynamo-utils/TransactGetBuilder.d.ts.map +1 -0
  76. package/dist/src/dynamo-utils/TransactGetBuilder.js +32 -0
  77. package/dist/src/dynamo-utils/TransactWriteBuilder.d.ts +46 -0
  78. package/dist/src/dynamo-utils/TransactWriteBuilder.d.ts.map +1 -0
  79. package/dist/src/dynamo-utils/TransactWriteBuilder.js +100 -0
  80. package/dist/src/dynamo-utils/errors.d.ts +13 -0
  81. package/dist/src/dynamo-utils/errors.d.ts.map +1 -0
  82. package/dist/src/dynamo-utils/errors.js +17 -0
  83. package/dist/src/dynamo-utils/index.d.ts +8 -0
  84. package/dist/src/dynamo-utils/index.d.ts.map +1 -0
  85. package/dist/src/dynamo-utils/index.js +30 -0
  86. package/dist/src/dynamo-utils/types.d.ts +52 -0
  87. package/dist/src/dynamo-utils/types.d.ts.map +1 -0
  88. package/dist/src/dynamo-utils/types.js +2 -0
  89. package/dist/src/errors.d.ts +13 -0
  90. package/dist/src/errors.d.ts.map +1 -0
  91. package/dist/src/errors.js +17 -0
  92. package/dist/src/index.d.ts +6 -0
  93. package/dist/src/index.d.ts.map +1 -0
  94. package/dist/src/index.js +24 -0
  95. package/dist/src/metadata/AttributeMetadata.d.ts +22 -0
  96. package/dist/src/metadata/AttributeMetadata.d.ts.map +1 -0
  97. package/dist/src/metadata/AttributeMetadata.js +27 -0
  98. package/dist/src/metadata/EntityMetadata.d.ts +42 -0
  99. package/dist/src/metadata/EntityMetadata.d.ts.map +1 -0
  100. package/dist/src/metadata/EntityMetadata.js +49 -0
  101. package/dist/src/metadata/JoinTableMetadata.d.ts +23 -0
  102. package/dist/src/metadata/JoinTableMetadata.d.ts.map +1 -0
  103. package/dist/src/metadata/JoinTableMetadata.js +22 -0
  104. package/dist/src/metadata/MetadataStorage.d.ts +104 -0
  105. package/dist/src/metadata/MetadataStorage.d.ts.map +1 -0
  106. package/dist/src/metadata/MetadataStorage.js +194 -0
  107. package/dist/src/metadata/TableMetadata.d.ts +53 -0
  108. package/dist/src/metadata/TableMetadata.d.ts.map +1 -0
  109. package/dist/src/metadata/TableMetadata.js +100 -0
  110. package/dist/src/metadata/index.d.ts +13 -0
  111. package/dist/src/metadata/index.d.ts.map +1 -0
  112. package/dist/src/metadata/index.js +34 -0
  113. package/dist/src/metadata/relationship-metadata/BelongsToRelationship.d.ts +18 -0
  114. package/dist/src/metadata/relationship-metadata/BelongsToRelationship.d.ts.map +1 -0
  115. package/dist/src/metadata/relationship-metadata/BelongsToRelationship.js +26 -0
  116. package/dist/src/metadata/relationship-metadata/HasAndBelongsToManyRelationship.d.ts +16 -0
  117. package/dist/src/metadata/relationship-metadata/HasAndBelongsToManyRelationship.d.ts.map +1 -0
  118. package/dist/src/metadata/relationship-metadata/HasAndBelongsToManyRelationship.js +25 -0
  119. package/dist/src/metadata/relationship-metadata/HasManyRelationship.d.ts +18 -0
  120. package/dist/src/metadata/relationship-metadata/HasManyRelationship.d.ts.map +1 -0
  121. package/dist/src/metadata/relationship-metadata/HasManyRelationship.js +26 -0
  122. package/dist/src/metadata/relationship-metadata/HasOneRelationship.d.ts +17 -0
  123. package/dist/src/metadata/relationship-metadata/HasOneRelationship.d.ts.map +1 -0
  124. package/dist/src/metadata/relationship-metadata/HasOneRelationship.js +25 -0
  125. package/dist/src/metadata/relationship-metadata/RelationshipMetadata.d.ts +21 -0
  126. package/dist/src/metadata/relationship-metadata/RelationshipMetadata.d.ts.map +1 -0
  127. package/dist/src/metadata/relationship-metadata/RelationshipMetadata.js +18 -0
  128. package/dist/src/metadata/relationship-metadata/index.d.ts +7 -0
  129. package/dist/src/metadata/relationship-metadata/index.d.ts.map +1 -0
  130. package/dist/src/metadata/relationship-metadata/index.js +29 -0
  131. package/dist/src/metadata/relationship-metadata/types.d.ts +16 -0
  132. package/dist/src/metadata/relationship-metadata/types.d.ts.map +1 -0
  133. package/dist/src/metadata/relationship-metadata/types.js +2 -0
  134. package/dist/src/metadata/relationship-metadata/utils.d.ts +3 -0
  135. package/dist/src/metadata/relationship-metadata/utils.d.ts.map +1 -0
  136. package/dist/src/metadata/relationship-metadata/utils.js +25 -0
  137. package/dist/src/metadata/types.d.ts +90 -0
  138. package/dist/src/metadata/types.d.ts.map +1 -0
  139. package/dist/src/metadata/types.js +2 -0
  140. package/dist/src/metadata/utils.d.ts +42 -0
  141. package/dist/src/metadata/utils.d.ts.map +1 -0
  142. package/dist/src/metadata/utils.js +67 -0
  143. package/dist/src/operations/Create/Create.d.ts +39 -0
  144. package/dist/src/operations/Create/Create.d.ts.map +1 -0
  145. package/dist/src/operations/Create/Create.js +84 -0
  146. package/dist/src/operations/Create/index.d.ts +3 -0
  147. package/dist/src/operations/Create/index.d.ts.map +1 -0
  148. package/dist/src/operations/Create/index.js +23 -0
  149. package/dist/src/operations/Create/types.d.ts +7 -0
  150. package/dist/src/operations/Create/types.d.ts.map +1 -0
  151. package/dist/src/operations/Create/types.js +2 -0
  152. package/dist/src/operations/Delete/Delete.d.ts +74 -0
  153. package/dist/src/operations/Delete/Delete.d.ts.map +1 -0
  154. package/dist/src/operations/Delete/Delete.js +207 -0
  155. package/dist/src/operations/Delete/index.d.ts +2 -0
  156. package/dist/src/operations/Delete/index.d.ts.map +1 -0
  157. package/dist/src/operations/Delete/index.js +8 -0
  158. package/dist/src/operations/Delete/types.d.ts +7 -0
  159. package/dist/src/operations/Delete/types.d.ts.map +1 -0
  160. package/dist/src/operations/Delete/types.js +2 -0
  161. package/dist/src/operations/FindById/FindById.d.ts +91 -0
  162. package/dist/src/operations/FindById/FindById.d.ts.map +1 -0
  163. package/dist/src/operations/FindById/FindById.js +244 -0
  164. package/dist/src/operations/FindById/index.d.ts +3 -0
  165. package/dist/src/operations/FindById/index.d.ts.map +1 -0
  166. package/dist/src/operations/FindById/index.js +23 -0
  167. package/dist/src/operations/FindById/types.d.ts +57 -0
  168. package/dist/src/operations/FindById/types.d.ts.map +1 -0
  169. package/dist/src/operations/FindById/types.js +2 -0
  170. package/dist/src/operations/OperationBase.d.ts +37 -0
  171. package/dist/src/operations/OperationBase.d.ts.map +1 -0
  172. package/dist/src/operations/OperationBase.js +44 -0
  173. package/dist/src/operations/Query/Query.d.ts +45 -0
  174. package/dist/src/operations/Query/Query.d.ts.map +1 -0
  175. package/dist/src/operations/Query/Query.js +84 -0
  176. package/dist/src/operations/Query/index.d.ts +3 -0
  177. package/dist/src/operations/Query/index.d.ts.map +1 -0
  178. package/dist/src/operations/Query/index.js +23 -0
  179. package/dist/src/operations/Query/types.d.ts +38 -0
  180. package/dist/src/operations/Query/types.d.ts.map +1 -0
  181. package/dist/src/operations/Query/types.js +2 -0
  182. package/dist/src/operations/Update/Update.d.ts +48 -0
  183. package/dist/src/operations/Update/Update.d.ts.map +1 -0
  184. package/dist/src/operations/Update/Update.js +118 -0
  185. package/dist/src/operations/Update/index.d.ts +3 -0
  186. package/dist/src/operations/Update/index.d.ts.map +1 -0
  187. package/dist/src/operations/Update/index.js +23 -0
  188. package/dist/src/operations/Update/types.d.ts +32 -0
  189. package/dist/src/operations/Update/types.d.ts.map +1 -0
  190. package/dist/src/operations/Update/types.js +2 -0
  191. package/dist/src/operations/index.d.ts +7 -0
  192. package/dist/src/operations/index.d.ts.map +1 -0
  193. package/dist/src/operations/index.js +22 -0
  194. package/dist/src/operations/types.d.ts +59 -0
  195. package/dist/src/operations/types.d.ts.map +1 -0
  196. package/dist/src/operations/types.js +2 -0
  197. package/dist/src/operations/utils/RelationshipTransactions.d.ts +64 -0
  198. package/dist/src/operations/utils/RelationshipTransactions.d.ts.map +1 -0
  199. package/dist/src/operations/utils/RelationshipTransactions.js +125 -0
  200. package/dist/src/operations/utils/expressionBuilder.d.ts +9 -0
  201. package/dist/src/operations/utils/expressionBuilder.d.ts.map +1 -0
  202. package/dist/src/operations/utils/expressionBuilder.js +106 -0
  203. package/dist/src/operations/utils/index.d.ts +5 -0
  204. package/dist/src/operations/utils/index.d.ts.map +1 -0
  205. package/dist/src/operations/utils/index.js +25 -0
  206. package/dist/src/operations/utils/types.d.ts +21 -0
  207. package/dist/src/operations/utils/types.d.ts.map +1 -0
  208. package/dist/src/operations/utils/types.js +2 -0
  209. package/dist/src/operations/utils/utils.d.ts +19 -0
  210. package/dist/src/operations/utils/utils.d.ts.map +1 -0
  211. package/dist/src/operations/utils/utils.js +36 -0
  212. package/dist/src/query-utils/Filters.d.ts +10 -0
  213. package/dist/src/query-utils/Filters.d.ts.map +1 -0
  214. package/dist/src/query-utils/Filters.js +25 -0
  215. package/dist/src/query-utils/QueryBuilder.d.ts +90 -0
  216. package/dist/src/query-utils/QueryBuilder.d.ts.map +1 -0
  217. package/dist/src/query-utils/QueryBuilder.js +213 -0
  218. package/dist/src/query-utils/index.d.ts +3 -0
  219. package/dist/src/query-utils/index.d.ts.map +1 -0
  220. package/dist/src/query-utils/index.js +23 -0
  221. package/dist/src/query-utils/types.d.ts +89 -0
  222. package/dist/src/query-utils/types.d.ts.map +1 -0
  223. package/dist/src/query-utils/types.js +2 -0
  224. package/dist/src/relationships/BelongsToLink.d.ts +50 -0
  225. package/dist/src/relationships/BelongsToLink.d.ts.map +1 -0
  226. package/dist/src/relationships/BelongsToLink.js +57 -0
  227. package/dist/src/relationships/JoinTable.d.ts +68 -0
  228. package/dist/src/relationships/JoinTable.d.ts.map +1 -0
  229. package/dist/src/relationships/JoinTable.js +133 -0
  230. package/dist/src/relationships/index.d.ts +3 -0
  231. package/dist/src/relationships/index.d.ts.map +1 -0
  232. package/dist/src/relationships/index.js +10 -0
  233. package/dist/src/types.d.ts +73 -0
  234. package/dist/src/types.d.ts.map +1 -0
  235. package/dist/src/types.js +2 -0
  236. package/dist/src/utils.d.ts +89 -0
  237. package/dist/src/utils.d.ts.map +1 -0
  238. package/dist/src/utils.js +168 -0
  239. package/package.json +61 -0
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const uuid_1 = require("uuid");
7
+ const dynamo_utils_1 = require("../../dynamo-utils");
8
+ const utils_1 = require("../../utils");
9
+ const OperationBase_1 = __importDefault(require("../OperationBase"));
10
+ const utils_2 = require("../utils");
11
+ /**
12
+ * Represents the operation for creating a new entity in the database, including handling its attributes and any related entities' associations. It will handle de-normalizing data to support relationships
13
+ *
14
+ * It encapsulates the logic required to translate entity attributes to a format suitable for DynamoDB, execute the creation transaction, and manage any relationships defined by the entity, such as "BelongsTo" or "HasMany" links.
15
+ *
16
+ * @template T - The type of the entity being created, extending `DynaRecord`.
17
+ */
18
+ class Create extends OperationBase_1.default {
19
+ #transactionBuilder;
20
+ constructor(Entity) {
21
+ super(Entity);
22
+ this.#transactionBuilder = new dynamo_utils_1.TransactWriteBuilder();
23
+ }
24
+ /**
25
+ * Create an entity transaction, including relationship transactions (EX: Creating BelongsToLinks for HasMany, checking existence of relationships, etc)
26
+ * @param attributes
27
+ * @returns
28
+ */
29
+ async run(attributes) {
30
+ const entityData = this.buildEntityData(attributes);
31
+ const tableItem = (0, utils_1.entityToTableItem)(this.EntityClass, entityData);
32
+ this.buildPutItemTransaction(tableItem);
33
+ await this.buildRelationshipTransactions(entityData);
34
+ await this.#transactionBuilder.executeTransaction();
35
+ return (0, utils_1.tableItemToEntity)(this.EntityClass, tableItem);
36
+ }
37
+ /**
38
+ * Builds the entity attributes
39
+ * @param attributes
40
+ * @returns
41
+ */
42
+ buildEntityData(attributes) {
43
+ const id = (0, uuid_1.v4)();
44
+ const createdAt = new Date();
45
+ const pk = this.tableMetadata.partitionKeyAttribute.name;
46
+ const sk = this.tableMetadata.sortKeyAttribute.name;
47
+ const keys = {
48
+ [pk]: this.EntityClass.partitionKeyValue(id),
49
+ [sk]: this.EntityClass.name
50
+ };
51
+ const defaultAttrs = {
52
+ id,
53
+ type: this.EntityClass.name,
54
+ createdAt,
55
+ updatedAt: createdAt
56
+ };
57
+ return { ...keys, ...attributes, ...defaultAttrs };
58
+ }
59
+ /**
60
+ * Build the transaction for the parent entity Create item request
61
+ * @param tableItem
62
+ */
63
+ buildPutItemTransaction(tableItem) {
64
+ const { name: tableName } = this.tableMetadata;
65
+ const putExpression = {
66
+ TableName: tableName,
67
+ Item: tableItem,
68
+ ConditionExpression: `attribute_not_exists(${this.partitionKeyAlias})` // Ensure item doesn't already exist
69
+ };
70
+ this.#transactionBuilder.addPut(putExpression);
71
+ }
72
+ /**
73
+ * Build transaction items for associations
74
+ * @param entityData
75
+ */
76
+ async buildRelationshipTransactions(entityData) {
77
+ const relationshipTransactions = new utils_2.RelationshipTransactions({
78
+ Entity: this.EntityClass,
79
+ transactionBuilder: this.#transactionBuilder
80
+ });
81
+ await relationshipTransactions.build(entityData);
82
+ }
83
+ }
84
+ exports.default = Create;
@@ -0,0 +1,3 @@
1
+ export { default as Create } from "./Create";
2
+ export * from "./types";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/operations/Create/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAC7C,cAAc,SAAS,CAAC"}
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.Create = void 0;
21
+ var Create_1 = require("./Create");
22
+ Object.defineProperty(exports, "Create", { enumerable: true, get: function () { return __importDefault(Create_1).default; } });
23
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,7 @@
1
+ import type DynaRecord from "../../DynaRecord";
2
+ import type { EntityDefinedAttributes } from "../types";
3
+ /**
4
+ * Entity attribute fields that can be set on create. Excludes that are managed by dyna-record
5
+ */
6
+ export type CreateOptions<T extends DynaRecord> = EntityDefinedAttributes<T>;
7
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/Create/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,UAAU,IAAI,uBAAuB,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,74 @@
1
+ import DynaRecord from "../../DynaRecord";
2
+ import type { EntityClass } from "../../types";
3
+ import OperationBase from "../OperationBase";
4
+ /**
5
+ * Implements the operation for deleting an entity and its related data from the database within the ORM framework.
6
+ *
7
+ * Delete an entity, everything in its partition, BelongsToLinks and nullifies ForeignKeys on attributes that BelongTo it
8
+ * If the foreign key is non nullable than it will throw a NullConstraintViolationError
9
+ *
10
+ * The `Delete` operation supports complex scenarios, such as deleting related entities in "BelongsTo" relationships, nullifying or removing foreign keys to maintain data integrity, and handling many-to-many relationships through join tables.
11
+ *
12
+ * @template T - The type of the entity being deleted, extending `DynaRecord`.
13
+ */
14
+ declare class Delete<T extends DynaRecord> extends OperationBase<T> {
15
+ #private;
16
+ constructor(Entity: EntityClass<T>);
17
+ /**
18
+ * Delete an item by id
19
+ * - Deletes the given entity
20
+ * - Deletes each item in the entity's partition
21
+ * - For each item in the entity's partition which is of type 'BelongsToLink' it:
22
+ * - Will nullify the associated relationship's ForeignKey attribute if the attribute is nullable
23
+ * @param id
24
+ */
25
+ run(id: string): Promise<void>;
26
+ /**
27
+ * Deletes an item
28
+ * @param item
29
+ */
30
+ private buildDeleteItemTransaction;
31
+ /**
32
+ * If the item has a JoinTable entry (is part of HasAndBelongsToMany relationship) then delete both JoinTable entries
33
+ * @param item - BelongsToLink from HasAndBelongsToMany relationship
34
+ */
35
+ private buildDeleteJoinTableLinkTransaction;
36
+ /**
37
+ * If the item being deleted has a foreign key reference, nullify the associated relationship's ForeignKey attribute
38
+ * If the ForeignKey is non nullable than it throws a NullConstraintViolationError
39
+ * @param item
40
+ */
41
+ private buildNullifyForeignKeyTransaction;
42
+ /**
43
+ * Deletes associated BelongsTo relationships for each ForeignKey of the item being deleted
44
+ * @param entityId
45
+ * @param item
46
+ */
47
+ private buildDeleteAssociatedBelongsTransaction;
48
+ /**
49
+ * Deletes associated BelongsToLink for a BelongsTo HasMany relationship
50
+ * @param relMeta
51
+ * @param entityId
52
+ * @param foreignKeyValue
53
+ */
54
+ private buildDeleteBelongsToHasManyTransaction;
55
+ /**
56
+ * Deletes associated BelongsToLink for a BelongsTo HasOne relationship
57
+ * @param relMeta
58
+ * @param foreignKeyValue
59
+ */
60
+ private buildDeleteBelongsToHasOneTransaction;
61
+ /**
62
+ * Type guard to check if the item being evaluated is the currentClass
63
+ * @param item
64
+ * @returns
65
+ */
66
+ private isEntityClass;
67
+ /**
68
+ * Track validation errors and throw AggregateError after all validations have been run
69
+ * @param err
70
+ */
71
+ private trackValidationError;
72
+ }
73
+ export default Delete;
74
+ //# sourceMappingURL=Delete.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Delete.d.ts","sourceRoot":"","sources":["../../../../src/operations/Delete/Delete.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAc1C,OAAO,KAAK,EAAE,WAAW,EAAsB,MAAM,aAAa,CAAC;AAEnE,OAAO,aAAa,MAAM,kBAAkB,CAAC;AAI7C;;;;;;;;;GASG;AACH,cAAM,MAAM,CAAC,CAAC,SAAS,UAAU,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;;gBAS7C,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;IAiBlC;;;;;;;OAOG;IACU,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAyC3C;;;OAGG;IACH,OAAO,CAAC,0BAA0B;IAmBlC;;;OAGG;IACH,OAAO,CAAC,mCAAmC;IAoB3C;;;;OAIG;IACH,OAAO,CAAC,iCAAiC;IA0CzC;;;;OAIG;IACH,OAAO,CAAC,uCAAuC;IA0B/C;;;;;OAKG;IACH,OAAO,CAAC,sCAAsC;IAkB9C;;;;OAIG;IACH,OAAO,CAAC,qCAAqC;IAiB7C;;;;OAIG;IACH,OAAO,CAAC,aAAa;IAIrB;;;OAGG;IACH,OAAO,CAAC,oBAAoB;CAG7B;AAED,eAAe,MAAM,CAAC"}
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const DynaRecord_1 = __importDefault(require("../../DynaRecord"));
7
+ const dynamo_utils_1 = require("../../dynamo-utils");
8
+ const errors_1 = require("../../errors");
9
+ const metadata_1 = __importDefault(require("../../metadata"));
10
+ const utils_1 = require("../../metadata/utils");
11
+ const relationships_1 = require("../../relationships");
12
+ const utils_2 = require("../../utils");
13
+ const OperationBase_1 = __importDefault(require("../OperationBase"));
14
+ const utils_3 = require("../utils");
15
+ /**
16
+ * Implements the operation for deleting an entity and its related data from the database within the ORM framework.
17
+ *
18
+ * Delete an entity, everything in its partition, BelongsToLinks and nullifies ForeignKeys on attributes that BelongTo it
19
+ * If the foreign key is non nullable than it will throw a NullConstraintViolationError
20
+ *
21
+ * The `Delete` operation supports complex scenarios, such as deleting related entities in "BelongsTo" relationships, nullifying or removing foreign keys to maintain data integrity, and handling many-to-many relationships through join tables.
22
+ *
23
+ * @template T - The type of the entity being deleted, extending `DynaRecord`.
24
+ */
25
+ class Delete extends OperationBase_1.default {
26
+ #transactionBuilder;
27
+ #tableName;
28
+ #partitionKeyField;
29
+ #sortKeyField;
30
+ #relationsLookup;
31
+ #belongsToRelationships;
32
+ #validationErrors = [];
33
+ constructor(Entity) {
34
+ super(Entity);
35
+ this.#transactionBuilder = new dynamo_utils_1.TransactWriteBuilder();
36
+ const { name: tableName } = this.tableMetadata;
37
+ this.#tableName = tableName;
38
+ this.#partitionKeyField = this.tableMetadata.partitionKeyAttribute.name;
39
+ this.#sortKeyField = this.tableMetadata.sortKeyAttribute.name;
40
+ const relationsObj = (0, utils_3.buildEntityRelationshipMetaObj)(Object.values(this.entityMetadata.relationships));
41
+ this.#relationsLookup = relationsObj.relationsLookup;
42
+ this.#belongsToRelationships = relationsObj.belongsToRelationships;
43
+ }
44
+ /**
45
+ * Delete an item by id
46
+ * - Deletes the given entity
47
+ * - Deletes each item in the entity's partition
48
+ * - For each item in the entity's partition which is of type 'BelongsToLink' it:
49
+ * - Will nullify the associated relationship's ForeignKey attribute if the attribute is nullable
50
+ * @param id
51
+ */
52
+ async run(id) {
53
+ const items = await this.EntityClass.query(id);
54
+ if (items.length === 0) {
55
+ throw new errors_1.NotFoundError(`Item does not exist: ${id}`);
56
+ }
57
+ for (const item of items) {
58
+ if (item.id === id && this.isEntityClass(item)) {
59
+ this.buildDeleteItemTransaction(item, {
60
+ errorMessage: `Failed to delete ${this.EntityClass.name} with Id: ${id}`
61
+ });
62
+ this.buildDeleteAssociatedBelongsTransaction(id, item);
63
+ }
64
+ if (item instanceof relationships_1.BelongsToLink) {
65
+ this.buildDeleteItemTransaction(item, {
66
+ errorMessage: `Failed to delete BelongsToLink with keys: ${JSON.stringify({
67
+ [this.#partitionKeyField]: item[this.#partitionKeyField],
68
+ [this.#sortKeyField]: item[this.#sortKeyField]
69
+ })}`
70
+ });
71
+ this.buildNullifyForeignKeyTransaction(item);
72
+ this.buildDeleteJoinTableLinkTransaction(item);
73
+ }
74
+ }
75
+ if (this.#validationErrors.length === 0) {
76
+ await this.#transactionBuilder.executeTransaction();
77
+ }
78
+ else {
79
+ throw new dynamo_utils_1.TransactionWriteFailedError(this.#validationErrors, "Failed Validations");
80
+ }
81
+ }
82
+ /**
83
+ * Deletes an item
84
+ * @param item
85
+ */
86
+ buildDeleteItemTransaction(item, options) {
87
+ const pkField = this.#partitionKeyField;
88
+ const skField = this.#sortKeyField;
89
+ this.#transactionBuilder.addDelete({
90
+ TableName: this.#tableName,
91
+ Key: {
92
+ [this.partitionKeyAlias]: item[pkField],
93
+ [this.sortKeyAlias]: item[skField]
94
+ }
95
+ }, options.errorMessage);
96
+ }
97
+ /**
98
+ * If the item has a JoinTable entry (is part of HasAndBelongsToMany relationship) then delete both JoinTable entries
99
+ * @param item - BelongsToLink from HasAndBelongsToMany relationship
100
+ */
101
+ buildDeleteJoinTableLinkTransaction(item) {
102
+ const relMeta = this.#relationsLookup[item.foreignEntityType];
103
+ if ((0, utils_1.isHasAndBelongsToManyRelationship)(relMeta)) {
104
+ // Inverse the keys to delete the other JoinTable entry
105
+ const belongsToLinksKeys = {
106
+ [this.#partitionKeyField]: item[this.#sortKeyField],
107
+ [this.#sortKeyField]: item[this.#partitionKeyField]
108
+ };
109
+ this.buildDeleteItemTransaction(belongsToLinksKeys, {
110
+ errorMessage: `Failed to delete BelongsToLink with keys: ${JSON.stringify(belongsToLinksKeys)}`
111
+ });
112
+ }
113
+ }
114
+ /**
115
+ * If the item being deleted has a foreign key reference, nullify the associated relationship's ForeignKey attribute
116
+ * If the ForeignKey is non nullable than it throws a NullConstraintViolationError
117
+ * @param item
118
+ */
119
+ buildNullifyForeignKeyTransaction(item) {
120
+ const relMeta = this.#relationsLookup[item.foreignEntityType];
121
+ if ((0, utils_1.isRelationshipMetadataWithForeignKey)(relMeta)) {
122
+ const entityAttrs = metadata_1.default.getEntityAttributes(relMeta.target.name);
123
+ const attrMeta = Object.values(entityAttrs).find(attr => attr.name === relMeta.foreignKey);
124
+ if (attrMeta?.nullable === false) {
125
+ this.trackValidationError(new errors_1.NullConstraintViolationError(`Cannot set ${relMeta.target.name} with id: '${item.id}' attribute '${relMeta.foreignKey}' to null`));
126
+ }
127
+ const tableKeys = (0, utils_2.entityToTableItem)(this.EntityClass, {
128
+ [this.#partitionKeyField]: relMeta.target.partitionKeyValue(item.foreignKey),
129
+ [this.#sortKeyField]: relMeta.target.name
130
+ });
131
+ const tableAttrs = (0, utils_2.entityToTableItem)(relMeta.target, {
132
+ [relMeta.foreignKey]: null
133
+ });
134
+ const expression = (0, utils_3.expressionBuilder)(tableAttrs);
135
+ this.#transactionBuilder.addUpdate({
136
+ TableName: this.#tableName,
137
+ Key: tableKeys,
138
+ UpdateExpression: expression.UpdateExpression,
139
+ ExpressionAttributeNames: expression.ExpressionAttributeNames
140
+ }, `Failed to remove foreign key attribute from ${relMeta.target.name} with Id: ${item.foreignKey}`);
141
+ }
142
+ }
143
+ /**
144
+ * Deletes associated BelongsTo relationships for each ForeignKey of the item being deleted
145
+ * @param entityId
146
+ * @param item
147
+ */
148
+ buildDeleteAssociatedBelongsTransaction(entityId, item) {
149
+ this.#belongsToRelationships.forEach(relMeta => {
150
+ if ((0, utils_2.isKeyOfObject)(item, relMeta.foreignKey) &&
151
+ item[relMeta.foreignKey] !== undefined) {
152
+ const foreignKeyValue = item[relMeta.foreignKey];
153
+ if ((0, utils_1.doesEntityBelongToRelAsHasMany)(this.EntityClass, relMeta)) {
154
+ this.buildDeleteBelongsToHasManyTransaction(relMeta, entityId, foreignKeyValue);
155
+ }
156
+ if ((0, utils_1.doesEntityBelongToRelAsHasOne)(this.EntityClass, relMeta)) {
157
+ this.buildDeleteBelongsToHasOneTransaction(relMeta, foreignKeyValue);
158
+ }
159
+ }
160
+ });
161
+ }
162
+ /**
163
+ * Deletes associated BelongsToLink for a BelongsTo HasMany relationship
164
+ * @param relMeta
165
+ * @param entityId
166
+ * @param foreignKeyValue
167
+ */
168
+ buildDeleteBelongsToHasManyTransaction(relMeta, entityId, foreignKeyValue) {
169
+ const belongsToLinksKeys = {
170
+ [this.#partitionKeyField]: relMeta.target.partitionKeyValue(foreignKeyValue),
171
+ [this.#sortKeyField]: this.EntityClass.partitionKeyValue(entityId)
172
+ };
173
+ this.buildDeleteItemTransaction(belongsToLinksKeys, {
174
+ errorMessage: `Failed to delete BelongsToLink with keys: ${JSON.stringify(belongsToLinksKeys)}`
175
+ });
176
+ }
177
+ /**
178
+ * Deletes associated BelongsToLink for a BelongsTo HasOne relationship
179
+ * @param relMeta
180
+ * @param foreignKeyValue
181
+ */
182
+ buildDeleteBelongsToHasOneTransaction(relMeta, foreignKeyValue) {
183
+ const belongsToLinksKeys = {
184
+ [this.#partitionKeyField]: relMeta.target.partitionKeyValue(foreignKeyValue),
185
+ [this.#sortKeyField]: this.EntityClass.name
186
+ };
187
+ this.buildDeleteItemTransaction(belongsToLinksKeys, {
188
+ errorMessage: `Failed to delete BelongsToLink with keys: ${JSON.stringify(belongsToLinksKeys)}`
189
+ });
190
+ }
191
+ /**
192
+ * Type guard to check if the item being evaluated is the currentClass
193
+ * @param item
194
+ * @returns
195
+ */
196
+ isEntityClass(item) {
197
+ return item instanceof DynaRecord_1.default;
198
+ }
199
+ /**
200
+ * Track validation errors and throw AggregateError after all validations have been run
201
+ * @param err
202
+ */
203
+ trackValidationError(err) {
204
+ this.#validationErrors.push(err);
205
+ }
206
+ }
207
+ exports.default = Delete;
@@ -0,0 +1,2 @@
1
+ export { default as Delete } from "./Delete";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/operations/Delete/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Delete = void 0;
7
+ var Delete_1 = require("./Delete");
8
+ Object.defineProperty(exports, "Delete", { enumerable: true, get: function () { return __importDefault(Delete_1).default; } });
@@ -0,0 +1,7 @@
1
+ import type DynaRecord from "../../DynaRecord";
2
+ import { type QueryResult } from "../Query";
3
+ export interface DeleteOptions {
4
+ errorMessage: string;
5
+ }
6
+ export type ItemKeys<T extends DynaRecord> = Partial<QueryResult<T>>;
7
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/Delete/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C,MAAM,WAAW,aAAa;IAC5B,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,UAAU,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,91 @@
1
+ import type DynaRecord from "../../DynaRecord";
2
+ import type { EntityClass, Optional } from "../../types";
3
+ import OperationBase from "../OperationBase";
4
+ import type { FindByIdOptions, FindByIdIncludesRes } from "./types";
5
+ /**
6
+ * Facilitates the retrieval of an entity by its identifier (ID) from the database, potentially including its associated entities based on specified relationships.
7
+ *
8
+ * It supports fetching an entity solely by its ID or along with its related entities through specified associations, allowing for flexible retrieval of complex entity graphs in a single operation.
9
+ *
10
+ * @template T - The type of the entity being retrieved, extending `DynaRecord`.
11
+ */
12
+ declare class FindById<T extends DynaRecord> extends OperationBase<T> {
13
+ #private;
14
+ constructor(Entity: EntityClass<T>);
15
+ /**
16
+ * Find an entity by Id and optionally include associations
17
+ * @param {string} id - Entity Id
18
+ * @param {Object} options - FindById options
19
+ * @param {Object[]=} options.include - The associations to include in the query
20
+ * @param {string} options.include[].association - The name of the association to include. Must be defined on the model
21
+ * @returns An entity with optional included associations serialized
22
+ */
23
+ run(id: string, options?: FindByIdOptions<T>): Promise<Optional<T | FindByIdIncludesRes<T, FindByIdOptions<T>>>>;
24
+ /**
25
+ * Find an Entity by id without associations
26
+ * @param {string} id - Entity Id
27
+ * @returns An entity object or undefined
28
+ */
29
+ private findByIdOnly;
30
+ /**
31
+ * Find an entity with included associations
32
+ * @param {string} id - Entity Id
33
+ * @param {Object[]=} includedAssociations - The associations to include in the query
34
+ * @param {string} includedAssociations[].association - The name of the association to include. Must be defined on the model
35
+ * @returns An entity with included associations serialized
36
+ */
37
+ private findByIdWithIncludes;
38
+ /**
39
+ * Build the query to find the entity, and any of the BelongsToLinks for the included models
40
+ * @param id
41
+ * @param includedRelationships
42
+ * @returns
43
+ */
44
+ private buildFindByIdIncludesQuery;
45
+ /**
46
+ * Sort query results into an object containing:
47
+ * - item: The FindById DynamoItem
48
+ * - belongsToLinks: BelongsToLinkDynamoItem records for each of the included relationships
49
+ * @param queryResults
50
+ * @returns
51
+ */
52
+ private filterQueryResults;
53
+ private buildGetIncludedRelationshipsTransaction;
54
+ /**
55
+ * Builds transactions to get an associated entity via a BelongsToLink (HasOne or HasMany)
56
+ * @param belongsToLinks
57
+ * @param relationsLookup
58
+ */
59
+ private buildGetRelationshipsThroughLinksTransaction;
60
+ /**
61
+ * Builds transactions to get an associated entity using a foreign key on the parent model (BelongsTo)
62
+ * @param belongsToLinks
63
+ * @param relationsLookup
64
+ */
65
+ private buildGetRelationshipsThroughForeignKeyTransaction;
66
+ /**
67
+ * Get relationship metadata for the associations included in the query
68
+ * @param includedAssociations
69
+ * @returns
70
+ */
71
+ private getIncludedRelationships;
72
+ /**
73
+ * Serialize the FindById item to its class, serialize results from included relationships query onto the entity
74
+ * @param entityTableItem
75
+ * @param transactionResults
76
+ * @param relationsLookup
77
+ * @returns
78
+ */
79
+ private resolveFindByIdIncludesResults;
80
+ /**
81
+ * Initializes default values for included relationships on a parent entity. It assigns
82
+ * an empty array to each relationship property for HasMany and HasAndBelongsToMany specified in `relationsLookup` if the property exists on `parentEntity`.
83
+ *
84
+ * @param parentEntity - The parent entity to initialize relationship properties on.
85
+ * @param relationsLookup - A mapping of relationship identifiers to their descriptions, including the relationship
86
+ * type and the property name on the parent entity.
87
+ */
88
+ private setIncludedRelationshipDefaults;
89
+ }
90
+ export default FindById;
91
+ //# sourceMappingURL=FindById.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FindById.d.ts","sourceRoot":"","sources":["../../../../src/operations/FindById/FindById.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAW/C,OAAO,KAAK,EAIV,WAAW,EACX,QAAQ,EACT,MAAM,aAAa,CAAC;AASrB,OAAO,aAAa,MAAM,kBAAkB,CAAC;AAC7C,OAAO,KAAK,EACV,eAAe,EACf,mBAAmB,EAGpB,MAAM,SAAS,CAAC;AAQjB;;;;;;GAMG;AACH,cAAM,QAAQ,CAAC,CAAC,SAAS,UAAU,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;;gBAG/C,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;IAKlC;;;;;;;OAOG;IACU,GAAG,CACd,EAAE,EAAE,MAAM,EACV,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAC3B,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,mBAAmB,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAQpE;;;;OAIG;YACW,YAAY;IAmB1B;;;;;;OAMG;YACW,oBAAoB;IAiClC;;;;;OAKG;IACH,OAAO,CAAC,0BAA0B;IAkBlC;;;;;;OAMG;IACH,OAAO,CAAC,kBAAkB;IAa1B,OAAO,CAAC,wCAAwC;IAehD;;;;OAIG;IACH,OAAO,CAAC,4CAA4C;IA6BpD;;;;OAIG;IACH,OAAO,CAAC,iDAAiD;IA0BzD;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAchC;;;;;;OAMG;IACH,OAAO,CAAC,8BAA8B;IAyCtC;;;;;;;OAOG;IACH,OAAO,CAAC,+BAA+B;CAexC;AAED,eAAe,QAAQ,CAAC"}