dyna-record 0.1.5 → 0.2.0
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/README.md +11 -13
- package/dist/src/DynaRecord.d.ts +36 -28
- package/dist/src/DynaRecord.d.ts.map +1 -1
- package/dist/src/DynaRecord.js +39 -36
- package/dist/src/decorators/Table.d.ts.map +1 -1
- package/dist/src/decorators/attributes/BooleanAttribute.d.ts.map +1 -1
- package/dist/src/decorators/attributes/DateAttribute.d.ts.map +1 -1
- package/dist/src/decorators/attributes/EnumAttribute.d.ts.map +1 -1
- package/dist/src/decorators/attributes/ForeignKeyAttribute.d.ts.map +1 -1
- package/dist/src/decorators/attributes/NumberAttribute.d.ts.map +1 -1
- package/dist/src/decorators/attributes/PartitionKeyAttribute.d.ts.map +1 -1
- package/dist/src/decorators/attributes/SortKeyAttribute.d.ts.map +1 -1
- package/dist/src/decorators/attributes/StringAttribute.d.ts.map +1 -1
- package/dist/src/decorators/attributes/serializers.d.ts +1 -1
- package/dist/src/decorators/relationships/BelongsTo.d.ts.map +1 -1
- package/dist/src/decorators/relationships/HasAndBelongsToMany.d.ts.map +1 -1
- package/dist/src/decorators/relationships/HasMany.d.ts.map +1 -1
- package/dist/src/decorators/relationships/HasOne.d.ts.map +1 -1
- package/dist/src/decorators/types.d.ts +2 -2
- package/dist/src/decorators/types.d.ts.map +1 -1
- package/dist/src/dynamo-utils/TransactGetBuilder.d.ts +4 -0
- package/dist/src/dynamo-utils/TransactGetBuilder.d.ts.map +1 -1
- package/dist/src/dynamo-utils/TransactGetBuilder.js +6 -0
- package/dist/src/metadata/EntityMetadata.d.ts +13 -1
- package/dist/src/metadata/EntityMetadata.d.ts.map +1 -1
- package/dist/src/metadata/EntityMetadata.js +21 -0
- package/dist/src/metadata/TableMetadata.d.ts.map +1 -1
- package/dist/src/metadata/TableMetadata.js +1 -3
- package/dist/src/metadata/relationship-metadata/types.d.ts +4 -0
- package/dist/src/metadata/relationship-metadata/types.d.ts.map +1 -1
- package/dist/src/metadata/types.d.ts +2 -3
- package/dist/src/metadata/types.d.ts.map +1 -1
- package/dist/src/metadata/utils.d.ts.map +1 -1
- package/dist/src/operations/Create/Create.d.ts +95 -16
- package/dist/src/operations/Create/Create.d.ts.map +1 -1
- package/dist/src/operations/Create/Create.js +170 -22
- package/dist/src/operations/Delete/Delete.d.ts +29 -25
- package/dist/src/operations/Delete/Delete.d.ts.map +1 -1
- package/dist/src/operations/Delete/Delete.js +105 -104
- package/dist/src/operations/Delete/types.d.ts +0 -3
- package/dist/src/operations/Delete/types.d.ts.map +1 -1
- package/dist/src/operations/FindById/FindById.d.ts +7 -28
- package/dist/src/operations/FindById/FindById.d.ts.map +1 -1
- package/dist/src/operations/FindById/FindById.js +36 -125
- package/dist/src/operations/FindById/types.d.ts +8 -9
- package/dist/src/operations/FindById/types.d.ts.map +1 -1
- package/dist/src/operations/Query/Query.d.ts +2 -1
- package/dist/src/operations/Query/Query.d.ts.map +1 -1
- package/dist/src/operations/Query/Query.js +17 -4
- package/dist/src/operations/Query/types.d.ts +39 -5
- package/dist/src/operations/Query/types.d.ts.map +1 -1
- package/dist/src/operations/Update/Update.d.ts +185 -24
- package/dist/src/operations/Update/Update.d.ts.map +1 -1
- package/dist/src/operations/Update/Update.js +402 -72
- package/dist/src/operations/Update/UpdateDryRun.d.ts +10 -0
- package/dist/src/operations/Update/UpdateDryRun.d.ts.map +1 -0
- package/dist/src/operations/Update/UpdateDryRun.js +15 -0
- package/dist/src/operations/Update/index.d.ts +1 -0
- package/dist/src/operations/Update/index.d.ts.map +1 -1
- package/dist/src/operations/Update/index.js +3 -1
- package/dist/src/operations/types.d.ts +6 -1
- package/dist/src/operations/types.d.ts.map +1 -1
- package/dist/src/operations/utils/expressionBuilder.d.ts.map +1 -1
- package/dist/src/operations/utils/expressionBuilder.js +1 -4
- package/dist/src/operations/utils/index.d.ts +0 -1
- package/dist/src/operations/utils/index.d.ts.map +1 -1
- package/dist/src/operations/utils/index.js +0 -6
- package/dist/src/operations/utils/utils.d.ts +13 -3
- package/dist/src/operations/utils/utils.d.ts.map +1 -1
- package/dist/src/operations/utils/utils.js +33 -3
- package/dist/src/query-utils/Filters.d.ts +1 -1
- package/dist/src/query-utils/Filters.d.ts.map +1 -1
- package/dist/src/query-utils/Filters.js +7 -15
- package/dist/src/query-utils/QueryBuilder.d.ts.map +1 -1
- package/dist/src/query-utils/QueryBuilder.js +9 -1
- package/dist/src/relationships/JoinTable.d.ts +17 -7
- package/dist/src/relationships/JoinTable.d.ts.map +1 -1
- package/dist/src/relationships/JoinTable.js +77 -20
- package/dist/src/relationships/index.d.ts +0 -1
- package/dist/src/relationships/index.d.ts.map +1 -1
- package/dist/src/relationships/index.js +1 -3
- package/dist/src/types.d.ts +6 -8
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils.d.ts +10 -32
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +13 -59
- package/package.json +4 -4
- package/dist/src/operations/utils/RelationshipTransactions.d.ts +0 -64
- package/dist/src/operations/utils/RelationshipTransactions.d.ts.map +0 -1
- package/dist/src/operations/utils/RelationshipTransactions.js +0 -125
- package/dist/src/relationships/BelongsToLink.d.ts +0 -51
- package/dist/src/relationships/BelongsToLink.d.ts.map +0 -1
- package/dist/src/relationships/BelongsToLink.js +0 -57
package/dist/src/utils.js
CHANGED
|
@@ -3,9 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.isNullableString = exports.safeAssign = exports.isString = exports.chunkArray = exports.isKeyOfObject = exports.isKeyOfEntity = exports.createInstance = exports.tableItemToEntity = exports.entityToTableItem = void 0;
|
|
7
7
|
const metadata_1 = __importDefault(require("./metadata"));
|
|
8
|
-
const relationships_1 = require("./relationships");
|
|
9
8
|
/**
|
|
10
9
|
* Convert an entity to its aliased table item fields to for dynamo interactions
|
|
11
10
|
* @param entityClassName
|
|
@@ -19,7 +18,9 @@ const entityToTableItem = (EntityClass, entityData) => {
|
|
|
19
18
|
if (attrMeta !== undefined) {
|
|
20
19
|
const { alias, serializers } = attrMeta;
|
|
21
20
|
// If the attribute has a custom serializer, serialize it
|
|
22
|
-
const value = serializers === undefined
|
|
21
|
+
const value = serializers === undefined || val === null
|
|
22
|
+
? val
|
|
23
|
+
: serializers.toTableAttribute(val);
|
|
23
24
|
acc[alias] = value;
|
|
24
25
|
}
|
|
25
26
|
return acc;
|
|
@@ -64,35 +65,6 @@ const createInstance = (EntityClass, attributes) => {
|
|
|
64
65
|
return entity;
|
|
65
66
|
};
|
|
66
67
|
exports.createInstance = createInstance;
|
|
67
|
-
/**
|
|
68
|
-
* Serialize a dynamo table item response to a BelongsToLink
|
|
69
|
-
* @param tableMeta - Table metadata
|
|
70
|
-
* @param tableItem - Table item from dynamo response
|
|
71
|
-
* @returns - { @link BelongsToLink }
|
|
72
|
-
*/
|
|
73
|
-
const tableItemToBelongsToLink = (tableMeta, tableItem) => {
|
|
74
|
-
const link = new relationships_1.BelongsToLink();
|
|
75
|
-
const belongsToLinkAttrs = {
|
|
76
|
-
...{
|
|
77
|
-
[tableMeta.partitionKeyAttribute.alias]: tableMeta.partitionKeyAttribute
|
|
78
|
-
},
|
|
79
|
-
...{ [tableMeta.sortKeyAttribute.alias]: tableMeta.sortKeyAttribute },
|
|
80
|
-
...tableMeta.defaultTableAttributes
|
|
81
|
-
};
|
|
82
|
-
Object.keys(tableItem).forEach(attrName => {
|
|
83
|
-
const attrMeta = belongsToLinkAttrs[attrName];
|
|
84
|
-
if (attrMeta !== undefined) {
|
|
85
|
-
const { name: entityKey, serializers } = attrMeta;
|
|
86
|
-
const rawVal = tableItem[attrName];
|
|
87
|
-
const val = serializers?.toEntityAttribute === undefined
|
|
88
|
-
? rawVal
|
|
89
|
-
: serializers?.toEntityAttribute(rawVal);
|
|
90
|
-
(0, exports.safeAssign)(link, entityKey, val);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
return link;
|
|
94
|
-
};
|
|
95
|
-
exports.tableItemToBelongsToLink = tableItemToBelongsToLink;
|
|
96
68
|
/**
|
|
97
69
|
* Type guard to check if the key is defined on the entity
|
|
98
70
|
*/
|
|
@@ -110,15 +82,6 @@ const isKeyOfObject = (entity, key) => {
|
|
|
110
82
|
return key in entity;
|
|
111
83
|
};
|
|
112
84
|
exports.isKeyOfObject = isKeyOfObject;
|
|
113
|
-
/**
|
|
114
|
-
* Type guard to check if the DynamoTableItem is a BelongsToLink
|
|
115
|
-
* @param res DynamoTableItem
|
|
116
|
-
* @returns boolean
|
|
117
|
-
*/
|
|
118
|
-
const isBelongsToLinkDynamoItem = (res, tableMeta) => {
|
|
119
|
-
return res[tableMeta.defaultAttributes.type.alias] === relationships_1.BelongsToLink.name;
|
|
120
|
-
};
|
|
121
|
-
exports.isBelongsToLinkDynamoItem = isBelongsToLinkDynamoItem;
|
|
122
85
|
/**
|
|
123
86
|
* Break an array into chunks
|
|
124
87
|
* @param array
|
|
@@ -129,24 +92,6 @@ const chunkArray = (array, size) => {
|
|
|
129
92
|
return Array.from({ length: Math.ceil(array.length / size) }, (_, index) => array.slice(index * size, (index + 1) * size));
|
|
130
93
|
};
|
|
131
94
|
exports.chunkArray = chunkArray;
|
|
132
|
-
/**
|
|
133
|
-
* Checks if a value is a valid property key (string, number, or symbol).
|
|
134
|
-
*
|
|
135
|
-
* @param value The value to be checked. This can be of any type.
|
|
136
|
-
* @returns `true` if the value is a `string`, `number`, or `symbol` (i.e., a valid property key); otherwise, `false`.
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* Logger.log(isPropertyKey('test')); // true
|
|
140
|
-
* Logger.log(isPropertyKey(123)); // true
|
|
141
|
-
* Logger.log(isPropertyKey(Symbol('sym'))); // true
|
|
142
|
-
* Logger.log(isPropertyKey({})); // false
|
|
143
|
-
*/
|
|
144
|
-
const isPropertyKey = (value) => {
|
|
145
|
-
return (typeof value === "string" ||
|
|
146
|
-
typeof value === "number" ||
|
|
147
|
-
typeof value === "symbol");
|
|
148
|
-
};
|
|
149
|
-
exports.isPropertyKey = isPropertyKey;
|
|
150
95
|
/**
|
|
151
96
|
* Checks if the given value is a string.
|
|
152
97
|
*
|
|
@@ -179,3 +124,12 @@ const safeAssign = (object, key, value) => {
|
|
|
179
124
|
object[key] = value;
|
|
180
125
|
};
|
|
181
126
|
exports.safeAssign = safeAssign;
|
|
127
|
+
/**
|
|
128
|
+
* Type guard to check if a string is a Nullable<string>
|
|
129
|
+
* @param val
|
|
130
|
+
* @returns
|
|
131
|
+
*/
|
|
132
|
+
const isNullableString = (val) => {
|
|
133
|
+
return typeof val === "string" || val === null;
|
|
134
|
+
};
|
|
135
|
+
exports.isNullableString = isNullableString;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dyna-record",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Typescript Object Relational Mapper (ORM) for Dynamo",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -49,9 +49,9 @@
|
|
|
49
49
|
"prettier": "^3.2.5",
|
|
50
50
|
"ts-jest": "^29.1.2",
|
|
51
51
|
"ts-node": "^10.9.2",
|
|
52
|
-
"typedoc": "^0.
|
|
53
|
-
"typedoc-plugin-missing-exports": "^
|
|
54
|
-
"typescript": "^5.
|
|
52
|
+
"typedoc": "^0.27.6",
|
|
53
|
+
"typedoc-plugin-missing-exports": "^3.1.0",
|
|
54
|
+
"typescript": "^5.7.2"
|
|
55
55
|
},
|
|
56
56
|
"engines": {
|
|
57
57
|
"npm": ">=10.0.0",
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type DynaRecord from "../../DynaRecord";
|
|
2
|
-
import type { TransactWriteBuilder } from "../../dynamo-utils";
|
|
3
|
-
import { type BelongsToRelationship } from "../../metadata";
|
|
4
|
-
import type { EntityClass } from "../../types";
|
|
5
|
-
type EntityData<T extends DynaRecord> = Pick<T, "id"> & Partial<T>;
|
|
6
|
-
interface RelationshipTransactionsProps<T extends DynaRecord> {
|
|
7
|
-
/**
|
|
8
|
-
* Entity for which relationships are being persisted
|
|
9
|
-
*/
|
|
10
|
-
readonly Entity: EntityClass<T>;
|
|
11
|
-
/**
|
|
12
|
-
* TransactionBuilder instance to add relationship transactions to
|
|
13
|
-
*/
|
|
14
|
-
readonly transactionBuilder: TransactWriteBuilder;
|
|
15
|
-
/**
|
|
16
|
-
* Optional callback to add logic to persisting BelongsTo HasMany relationships
|
|
17
|
-
* @param rel The BelongsToRelationship metadata of which the Entity BelongsTo HasMany of
|
|
18
|
-
* @param entityId The ID of the entity
|
|
19
|
-
* @returns
|
|
20
|
-
*/
|
|
21
|
-
belongsToHasManyCb?: (rel: BelongsToRelationship, entityId: string) => Promise<void>;
|
|
22
|
-
/**
|
|
23
|
-
* Optional callback to add logic to persisting BelongsTo HasOne relationships
|
|
24
|
-
* @param rel The BelongsToRelationship metadata of which the Entity BelongsTo HasOne of
|
|
25
|
-
* @param entityId The ID of the entity
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
|
-
belongsToHasOneCb?: (rel: BelongsToRelationship, entityId: string) => Promise<void>;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Builds transactions for persisting create/update relationships
|
|
32
|
-
* - Sets/removes ForeignKeys
|
|
33
|
-
* - Adds/removes BelongsToLinks
|
|
34
|
-
*/
|
|
35
|
-
declare class RelationshipTransactions<T extends DynaRecord> {
|
|
36
|
-
#private;
|
|
37
|
-
constructor(props: RelationshipTransactionsProps<T>);
|
|
38
|
-
build<T extends DynaRecord>(entityData: EntityData<T>): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Builds a ConditionCheck transaction that ensures the associated relationship exists
|
|
41
|
-
* @param rel
|
|
42
|
-
* @param relationshipId
|
|
43
|
-
* @returns
|
|
44
|
-
*/
|
|
45
|
-
private buildRelationshipExistsConditionTransaction;
|
|
46
|
-
/**
|
|
47
|
-
* Creates a BelongsToLink transaction item in the parents partition if the entity BelongsTo a HasOne association
|
|
48
|
-
* Adds conditional check to ensure the parent doesn't already have one of the entity being associated
|
|
49
|
-
* @param entityId Id of the entity being persisted
|
|
50
|
-
* @param rel BelongsTo relationship metadata for which the entity being persisted is a BelongsTo HasOne
|
|
51
|
-
* @param relationshipId Id of the parent entity of which the entity being persisted BelongsTo
|
|
52
|
-
*/
|
|
53
|
-
private buildBelongsToHasOne;
|
|
54
|
-
/**
|
|
55
|
-
* Creates a BelongsToLink transaction item in the parents partition if the entity BelongsTo a HasMany association
|
|
56
|
-
* @param rel BelongsTo relationship metadata for which the entity being persisted is a BelongsTo HasMany
|
|
57
|
-
* @param entityId Id of the entity being persisted
|
|
58
|
-
* @param relationshipId Id of the parent entity of which the entity being persisted BelongsTo
|
|
59
|
-
*/
|
|
60
|
-
private buildBelongsToHasMany;
|
|
61
|
-
private isNullableString;
|
|
62
|
-
}
|
|
63
|
-
export default RelationshipTransactions;
|
|
64
|
-
//# sourceMappingURL=RelationshipTransactions.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RelationshipTransactions.d.ts","sourceRoot":"","sources":["../../../../src/operations/utils/RelationshipTransactions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EAGV,oBAAoB,EACrB,MAAM,oBAAoB,CAAC;AAC5B,OAAiB,EAEf,KAAK,qBAAqB,EAE3B,MAAM,gBAAgB,CAAC;AAOxB,OAAO,KAAK,EAAE,WAAW,EAAY,MAAM,aAAa,CAAC;AAIzD,KAAK,UAAU,CAAC,CAAC,SAAS,UAAU,IAAI,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEnE,UAAU,6BAA6B,CAAC,CAAC,SAAS,UAAU;IAC1D;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAChC;;OAEG;IACH,QAAQ,CAAC,kBAAkB,EAAE,oBAAoB,CAAC;IAClD;;;;;OAKG;IACH,kBAAkB,CAAC,EAAE,CACnB,GAAG,EAAE,qBAAqB,EAC1B,QAAQ,EAAE,MAAM,KACb,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,CAClB,GAAG,EAAE,qBAAqB,EAC1B,QAAQ,EAAE,MAAM,KACb,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB;AAED;;;;GAIG;AACH,cAAM,wBAAwB,CAAC,CAAC,SAAS,UAAU;;gBAOrC,KAAK,EAAE,6BAA6B,CAAC,CAAC,CAAC;IAUtC,KAAK,CAAC,CAAC,SAAS,UAAU,EACrC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,GACxB,OAAO,CAAC,IAAI,CAAC;IAoChB;;;;;OAKG;IACH,OAAO,CAAC,2CAA2C;IAoBnD;;;;;;OAMG;YACW,oBAAoB;IAgClC;;;;;OAKG;YACW,qBAAqB;IAkCnC,OAAO,CAAC,gBAAgB;CAGzB;AAED,eAAe,wBAAwB,CAAC"}
|
|
@@ -1,125 +0,0 @@
|
|
|
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 metadata_1 = __importDefault(require("../../metadata"));
|
|
7
|
-
const utils_1 = require("../../metadata/utils");
|
|
8
|
-
const relationships_1 = require("../../relationships");
|
|
9
|
-
const utils_2 = require("../../utils");
|
|
10
|
-
const utils_3 = require("./utils");
|
|
11
|
-
/**
|
|
12
|
-
* Builds transactions for persisting create/update relationships
|
|
13
|
-
* - Sets/removes ForeignKeys
|
|
14
|
-
* - Adds/removes BelongsToLinks
|
|
15
|
-
*/
|
|
16
|
-
class RelationshipTransactions {
|
|
17
|
-
#props;
|
|
18
|
-
#entityMetadata;
|
|
19
|
-
#tableMetadata;
|
|
20
|
-
#partitionKeyAlias;
|
|
21
|
-
#sortKeyAlias;
|
|
22
|
-
constructor(props) {
|
|
23
|
-
this.#props = props;
|
|
24
|
-
this.#entityMetadata = metadata_1.default.getEntity(props.Entity.name);
|
|
25
|
-
this.#tableMetadata = metadata_1.default.getTable(this.#entityMetadata.tableClassName);
|
|
26
|
-
this.#partitionKeyAlias = this.#tableMetadata.partitionKeyAttribute.alias;
|
|
27
|
-
this.#sortKeyAlias = this.#tableMetadata.sortKeyAttribute.alias;
|
|
28
|
-
}
|
|
29
|
-
async build(entityData) {
|
|
30
|
-
const { relationships } = this.#entityMetadata;
|
|
31
|
-
for (const rel of Object.values(relationships)) {
|
|
32
|
-
const isBelongsTo = (0, utils_1.isBelongsToRelationship)(rel);
|
|
33
|
-
if (isBelongsTo) {
|
|
34
|
-
const relationshipId = (0, utils_3.extractForeignKeyFromEntity)(rel, entityData);
|
|
35
|
-
const isUpdatingRelationshipId = relationshipId !== undefined;
|
|
36
|
-
if (isUpdatingRelationshipId && this.isNullableString(relationshipId)) {
|
|
37
|
-
if (relationshipId !== null) {
|
|
38
|
-
this.buildRelationshipExistsConditionTransaction(rel, relationshipId);
|
|
39
|
-
}
|
|
40
|
-
const callbackParams = [rel, entityData.id, relationshipId];
|
|
41
|
-
if ((0, utils_1.doesEntityBelongToRelAsHasMany)(this.#props.Entity, rel)) {
|
|
42
|
-
await this.buildBelongsToHasMany(...callbackParams);
|
|
43
|
-
}
|
|
44
|
-
if ((0, utils_1.doesEntityBelongToRelAsHasOne)(this.#props.Entity, rel)) {
|
|
45
|
-
await this.buildBelongsToHasOne(...callbackParams);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Builds a ConditionCheck transaction that ensures the associated relationship exists
|
|
53
|
-
* @param rel
|
|
54
|
-
* @param relationshipId
|
|
55
|
-
* @returns
|
|
56
|
-
*/
|
|
57
|
-
buildRelationshipExistsConditionTransaction(rel, relationshipId) {
|
|
58
|
-
const { name: tableName } = this.#tableMetadata;
|
|
59
|
-
const errMsg = `${rel.target.name} with ID '${relationshipId}' does not exist`;
|
|
60
|
-
const conditionCheck = {
|
|
61
|
-
TableName: tableName,
|
|
62
|
-
Key: {
|
|
63
|
-
[this.#partitionKeyAlias]: rel.target.partitionKeyValue(relationshipId),
|
|
64
|
-
[this.#sortKeyAlias]: rel.target.name
|
|
65
|
-
},
|
|
66
|
-
ConditionExpression: `attribute_exists(${this.#partitionKeyAlias})`
|
|
67
|
-
};
|
|
68
|
-
this.#props.transactionBuilder.addConditionCheck(conditionCheck, errMsg);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Creates a BelongsToLink transaction item in the parents partition if the entity BelongsTo a HasOne association
|
|
72
|
-
* Adds conditional check to ensure the parent doesn't already have one of the entity being associated
|
|
73
|
-
* @param entityId Id of the entity being persisted
|
|
74
|
-
* @param rel BelongsTo relationship metadata for which the entity being persisted is a BelongsTo HasOne
|
|
75
|
-
* @param relationshipId Id of the parent entity of which the entity being persisted BelongsTo
|
|
76
|
-
*/
|
|
77
|
-
async buildBelongsToHasOne(rel, entityId, relationshipId) {
|
|
78
|
-
const { name: tableName } = this.#tableMetadata;
|
|
79
|
-
if (this.#props.belongsToHasOneCb !== undefined) {
|
|
80
|
-
await this.#props.belongsToHasOneCb(rel, entityId);
|
|
81
|
-
}
|
|
82
|
-
if (relationshipId !== null) {
|
|
83
|
-
const link = relationships_1.BelongsToLink.build(this.#props.Entity.name, entityId);
|
|
84
|
-
const keys = {
|
|
85
|
-
[this.#partitionKeyAlias]: rel.target.partitionKeyValue(relationshipId),
|
|
86
|
-
[this.#sortKeyAlias]: this.#props.Entity.name
|
|
87
|
-
};
|
|
88
|
-
const putExpression = {
|
|
89
|
-
TableName: tableName,
|
|
90
|
-
Item: { ...keys, ...(0, utils_2.entityToTableItem)(rel.target, link) },
|
|
91
|
-
ConditionExpression: `attribute_not_exists(${this.#partitionKeyAlias})` // Ensure item doesn't already exist
|
|
92
|
-
};
|
|
93
|
-
this.#props.transactionBuilder.addPut(putExpression, `${rel.target.name} with id: ${relationshipId} already has an associated ${this.#props.Entity.name}`);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Creates a BelongsToLink transaction item in the parents partition if the entity BelongsTo a HasMany association
|
|
98
|
-
* @param rel BelongsTo relationship metadata for which the entity being persisted is a BelongsTo HasMany
|
|
99
|
-
* @param entityId Id of the entity being persisted
|
|
100
|
-
* @param relationshipId Id of the parent entity of which the entity being persisted BelongsTo
|
|
101
|
-
*/
|
|
102
|
-
async buildBelongsToHasMany(rel, entityId, relationshipId) {
|
|
103
|
-
const { name: tableName } = this.#tableMetadata;
|
|
104
|
-
if (this.#props.belongsToHasManyCb !== undefined) {
|
|
105
|
-
await this.#props.belongsToHasManyCb(rel, entityId);
|
|
106
|
-
}
|
|
107
|
-
if (relationshipId !== null) {
|
|
108
|
-
const link = relationships_1.BelongsToLink.build(this.#props.Entity.name, entityId);
|
|
109
|
-
const keys = {
|
|
110
|
-
[this.#partitionKeyAlias]: rel.target.partitionKeyValue(relationshipId),
|
|
111
|
-
[this.#sortKeyAlias]: this.#props.Entity.partitionKeyValue(link.foreignKey)
|
|
112
|
-
};
|
|
113
|
-
const putExpression = {
|
|
114
|
-
TableName: tableName,
|
|
115
|
-
Item: { ...keys, ...(0, utils_2.entityToTableItem)(rel.target, link) },
|
|
116
|
-
ConditionExpression: `attribute_not_exists(${this.#partitionKeyAlias})` // Ensure item doesn't already exist
|
|
117
|
-
};
|
|
118
|
-
this.#props.transactionBuilder.addPut(putExpression, `${this.#props.Entity.name} with ID '${entityId}' already belongs to ${rel.target.name} with Id '${relationshipId}'`);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
isNullableString(val) {
|
|
122
|
-
return typeof val === "string" || val === null;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
exports.default = RelationshipTransactions;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import type DynaRecord from "../DynaRecord";
|
|
2
|
-
import type { ForeignKey } from "../types";
|
|
3
|
-
import type { EntityAttributes } from "../operations";
|
|
4
|
-
/**
|
|
5
|
-
* Extends `DynaRecord` with properties specific to "BelongsTo" relationships, such as `foreignEntityType` and `foreignKey`.
|
|
6
|
-
*/
|
|
7
|
-
interface BelongsToLinkProps extends EntityAttributes<DynaRecord> {
|
|
8
|
-
foreignEntityType: string;
|
|
9
|
-
foreignKey: string;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Represents a "BelongsTo" relationship link between entities within the ORM system. Instances of this class are used to track and manage associations where one entity belongs to another, encapsulating the connection details including the type of the foreign entity and its foreign key.
|
|
13
|
-
*
|
|
14
|
-
* This class implements the `BelongsToLinkProps` interface, ensuring consistency in the properties used to describe the "BelongsTo" relationship.
|
|
15
|
-
*/
|
|
16
|
-
declare class BelongsToLink implements BelongsToLinkProps {
|
|
17
|
-
/**
|
|
18
|
-
* A unique identifier for the link itself, automatically generated upon creation.
|
|
19
|
-
*/
|
|
20
|
-
readonly id: string;
|
|
21
|
-
/**
|
|
22
|
-
* The type of the link, statically set to "BelongsToLink".
|
|
23
|
-
*/
|
|
24
|
-
readonly type: string;
|
|
25
|
-
/**
|
|
26
|
-
* The name of the entity type to which the link points (the "parent" entity in the relationship).
|
|
27
|
-
*/
|
|
28
|
-
readonly foreignEntityType: string;
|
|
29
|
-
/**
|
|
30
|
-
* The foreign key value identifying the specific instance of the foreign entity to which the link belongs. While it is a string, it represents the value of a `ForeignKey` attribute in the related entity.
|
|
31
|
-
*/
|
|
32
|
-
readonly foreignKey: ForeignKey;
|
|
33
|
-
/**
|
|
34
|
-
* The timestamp marking when the link was created
|
|
35
|
-
*/
|
|
36
|
-
readonly createdAt: Date;
|
|
37
|
-
/**
|
|
38
|
-
* The timestamp marking the last update to the link. Initially set to the same value as `createdAt`.
|
|
39
|
-
*/
|
|
40
|
-
readonly updatedAt: Date;
|
|
41
|
-
constructor(item?: BelongsToLink);
|
|
42
|
-
/**
|
|
43
|
-
* A static method to construct a `BelongsToLinkProps` object with specified properties, including auto-generated `id` and timestamp fields. This method facilitates the creation of new link instances without directly instantiating the class.
|
|
44
|
-
* @param {string} foreignEntityType - The name of the entity type to which the new link will belong.
|
|
45
|
-
* @param {string} foreignKey - The foreign key value identifying the specific instance of the foreign entity.
|
|
46
|
-
* @returns {BelongsToLinkProps} - A new `BelongsToLinkProps` object ready for use in creating or managing a "BelongsTo" relationship.
|
|
47
|
-
*/
|
|
48
|
-
static build(foreignEntityType: string, foreignKey: string): BelongsToLinkProps;
|
|
49
|
-
}
|
|
50
|
-
export default BelongsToLink;
|
|
51
|
-
//# sourceMappingURL=BelongsToLink.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BelongsToLink.d.ts","sourceRoot":"","sources":["../../../src/relationships/BelongsToLink.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,UAAU,MAAM,eAAe,CAAC;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEtD;;GAEG;AACH,UAAU,kBAAmB,SAAQ,gBAAgB,CAAC,UAAU,CAAC;IAC/D,iBAAiB,EAAE,MAAM,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,cAAM,aAAc,YAAW,kBAAkB;IAC/C;;OAEG;IACH,SAAgB,EAAE,EAAE,MAAM,CAAC;IAC3B;;OAEG;IACH,SAAgB,IAAI,EAAE,MAAM,CAAC;IAC7B;;OAEG;IACH,SAAgB,iBAAiB,EAAE,MAAM,CAAC;IAC1C;;OAEG;IACH,SAAgB,UAAU,EAAE,UAAU,CAAC;IACvC;;OAEG;IACH,SAAgB,SAAS,EAAE,IAAI,CAAC;IAChC;;OAEG;IACH,SAAgB,SAAS,EAAE,IAAI,CAAC;gBAEpB,IAAI,CAAC,EAAE,aAAa;IAMhC;;;;;OAKG;WACW,KAAK,CACjB,iBAAiB,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,GACjB,kBAAkB;CAYtB;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const uuid_1 = require("uuid");
|
|
4
|
-
/**
|
|
5
|
-
* Represents a "BelongsTo" relationship link between entities within the ORM system. Instances of this class are used to track and manage associations where one entity belongs to another, encapsulating the connection details including the type of the foreign entity and its foreign key.
|
|
6
|
-
*
|
|
7
|
-
* This class implements the `BelongsToLinkProps` interface, ensuring consistency in the properties used to describe the "BelongsTo" relationship.
|
|
8
|
-
*/
|
|
9
|
-
class BelongsToLink {
|
|
10
|
-
/**
|
|
11
|
-
* A unique identifier for the link itself, automatically generated upon creation.
|
|
12
|
-
*/
|
|
13
|
-
id;
|
|
14
|
-
/**
|
|
15
|
-
* The type of the link, statically set to "BelongsToLink".
|
|
16
|
-
*/
|
|
17
|
-
type;
|
|
18
|
-
/**
|
|
19
|
-
* The name of the entity type to which the link points (the "parent" entity in the relationship).
|
|
20
|
-
*/
|
|
21
|
-
foreignEntityType;
|
|
22
|
-
/**
|
|
23
|
-
* The foreign key value identifying the specific instance of the foreign entity to which the link belongs. While it is a string, it represents the value of a `ForeignKey` attribute in the related entity.
|
|
24
|
-
*/
|
|
25
|
-
foreignKey;
|
|
26
|
-
/**
|
|
27
|
-
* The timestamp marking when the link was created
|
|
28
|
-
*/
|
|
29
|
-
createdAt;
|
|
30
|
-
/**
|
|
31
|
-
* The timestamp marking the last update to the link. Initially set to the same value as `createdAt`.
|
|
32
|
-
*/
|
|
33
|
-
updatedAt;
|
|
34
|
-
constructor(item) {
|
|
35
|
-
if (item !== undefined) {
|
|
36
|
-
Object.assign(this, item);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* A static method to construct a `BelongsToLinkProps` object with specified properties, including auto-generated `id` and timestamp fields. This method facilitates the creation of new link instances without directly instantiating the class.
|
|
41
|
-
* @param {string} foreignEntityType - The name of the entity type to which the new link will belong.
|
|
42
|
-
* @param {string} foreignKey - The foreign key value identifying the specific instance of the foreign entity.
|
|
43
|
-
* @returns {BelongsToLinkProps} - A new `BelongsToLinkProps` object ready for use in creating or managing a "BelongsTo" relationship.
|
|
44
|
-
*/
|
|
45
|
-
static build(foreignEntityType, foreignKey) {
|
|
46
|
-
const createdAt = new Date();
|
|
47
|
-
return {
|
|
48
|
-
id: (0, uuid_1.v4)(),
|
|
49
|
-
type: BelongsToLink.name,
|
|
50
|
-
foreignKey,
|
|
51
|
-
foreignEntityType,
|
|
52
|
-
createdAt,
|
|
53
|
-
updatedAt: createdAt
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.default = BelongsToLink;
|