dyna-record 0.1.5 → 0.2.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/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 +7 -0
- package/dist/src/decorators/relationships/HasMany.d.ts.map +1 -1
- package/dist/src/decorators/relationships/HasMany.js +12 -2
- 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 +21 -1
- package/dist/src/metadata/EntityMetadata.d.ts.map +1 -1
- package/dist/src/metadata/EntityMetadata.js +33 -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/BelongsToRelationship.d.ts +1 -1
- package/dist/src/metadata/relationship-metadata/BelongsToRelationship.js +1 -1
- package/dist/src/metadata/relationship-metadata/HasManyRelationship.d.ts +2 -1
- package/dist/src/metadata/relationship-metadata/HasManyRelationship.d.ts.map +1 -1
- package/dist/src/metadata/relationship-metadata/HasManyRelationship.js +2 -1
- package/dist/src/metadata/relationship-metadata/OwnedByRelationship.d.ts +18 -0
- package/dist/src/metadata/relationship-metadata/OwnedByRelationship.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/OwnedByRelationship.js +26 -0
- package/dist/src/metadata/relationship-metadata/RelationshipMetadata.d.ts +1 -1
- package/dist/src/metadata/relationship-metadata/RelationshipMetadata.d.ts.map +1 -1
- package/dist/src/metadata/relationship-metadata/index.d.ts +2 -1
- package/dist/src/metadata/relationship-metadata/index.d.ts.map +1 -1
- package/dist/src/metadata/relationship-metadata/index.js +3 -1
- package/dist/src/metadata/relationship-metadata/types.d.ts +7 -2
- package/dist/src/metadata/relationship-metadata/types.d.ts.map +1 -1
- package/dist/src/metadata/relationship-metadata/utils.d.ts.map +1 -1
- package/dist/src/metadata/relationship-metadata/utils.js +3 -0
- package/dist/src/metadata/types.d.ts +7 -4
- package/dist/src/metadata/types.d.ts.map +1 -1
- package/dist/src/metadata/utils.d.ts +8 -4
- package/dist/src/metadata/utils.d.ts.map +1 -1
- package/dist/src/metadata/utils.js +8 -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 +171 -22
- package/dist/src/operations/Delete/Delete.d.ts +32 -23
- package/dist/src/operations/Delete/Delete.d.ts.map +1 -1
- package/dist/src/operations/Delete/Delete.js +117 -100
- 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 +409 -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 +5 -5
- 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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/Delete/types.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/Delete/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,YAAY,EAAE,MAAM,CAAC;CACtB"}
|
|
@@ -36,33 +36,12 @@ declare class FindById<T extends DynaRecord> extends OperationBase<T> {
|
|
|
36
36
|
*/
|
|
37
37
|
private findByIdWithIncludes;
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
40
|
-
* @param id
|
|
41
|
-
* @param
|
|
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
|
|
39
|
+
* Filters query results by parent entity and related entities for processing
|
|
40
|
+
* @param entityId - The id of the parent entity
|
|
41
|
+
* @param queryResults - The results of the query on the parent partition
|
|
50
42
|
* @returns
|
|
51
43
|
*/
|
|
52
44
|
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
45
|
/**
|
|
67
46
|
* Get relationship metadata for the associations included in the query
|
|
68
47
|
* @param includedAssociations
|
|
@@ -70,10 +49,10 @@ declare class FindById<T extends DynaRecord> extends OperationBase<T> {
|
|
|
70
49
|
*/
|
|
71
50
|
private getIncludedRelationships;
|
|
72
51
|
/**
|
|
73
|
-
*
|
|
74
|
-
* @param
|
|
75
|
-
* @param
|
|
76
|
-
* @param
|
|
52
|
+
* Resolve query results into parent entity with included relationships
|
|
53
|
+
* @param parentEntity
|
|
54
|
+
* @param relatedEntities
|
|
55
|
+
* @param includedRelMeta
|
|
77
56
|
* @returns
|
|
78
57
|
*/
|
|
79
58
|
private resolveFindByIdIncludesResults;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FindById.d.ts","sourceRoot":"","sources":["../../../../src/operations/FindById/FindById.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"FindById.d.ts","sourceRoot":"","sources":["../../../../src/operations/FindById/FindById.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAI/C,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAsB,MAAM,aAAa,CAAC;AAE7E,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;IA4BlC;;;;;OAKG;IACH,OAAO,CAAC,kBAAkB;IAkB1B;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAchC;;;;;;OAMG;IACH,OAAO,CAAC,8BAA8B;IA0BtC;;;;;;;OAOG;IACH,OAAO,CAAC,+BAA+B;CAaxC;AAED,eAAe,QAAQ,CAAC"}
|
|
@@ -3,16 +3,12 @@ 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
|
-
const metadata_1 = __importDefault(require("../../metadata"));
|
|
7
|
-
const dynamo_utils_1 = require("../../dynamo-utils");
|
|
8
|
-
const query_utils_1 = require("../../query-utils");
|
|
9
6
|
const Filters_1 = require("../../query-utils/Filters");
|
|
10
|
-
const
|
|
7
|
+
const dynamo_utils_1 = require("../../dynamo-utils");
|
|
11
8
|
const utils_1 = require("../../utils");
|
|
12
9
|
const OperationBase_1 = __importDefault(require("../OperationBase"));
|
|
13
10
|
const utils_2 = require("../utils");
|
|
14
11
|
const utils_3 = require("../../metadata/utils");
|
|
15
|
-
const Logger_1 = __importDefault(require("../../Logger"));
|
|
16
12
|
/**
|
|
17
13
|
* Facilitates the retrieval of an entity by its identifier (ID) from the database, potentially including its associated entities based on specified relationships.
|
|
18
14
|
*
|
|
@@ -24,7 +20,7 @@ class FindById extends OperationBase_1.default {
|
|
|
24
20
|
#transactionBuilder;
|
|
25
21
|
constructor(Entity) {
|
|
26
22
|
super(Entity);
|
|
27
|
-
this.#transactionBuilder = new
|
|
23
|
+
this.#transactionBuilder = new dynamo_utils_1.TransactGetBuilder();
|
|
28
24
|
}
|
|
29
25
|
/**
|
|
30
26
|
* Find an entity by Id and optionally include associations
|
|
@@ -72,107 +68,34 @@ class FindById extends OperationBase_1.default {
|
|
|
72
68
|
* @returns An entity with included associations serialized
|
|
73
69
|
*/
|
|
74
70
|
async findByIdWithIncludes(id, includedAssociations) {
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
const queryResults = await
|
|
78
|
-
|
|
79
|
-
ConsistentRead: true
|
|
71
|
+
const includedRelMeta = this.getIncludedRelationships(includedAssociations);
|
|
72
|
+
const includedTypesFilter = (0, Filters_1.includedRelationshipsFilter)(this.EntityClass.name, includedRelMeta);
|
|
73
|
+
const queryResults = await this.EntityClass.query(id, {
|
|
74
|
+
filter: includedTypesFilter
|
|
80
75
|
});
|
|
81
|
-
if (queryResults.length === 0)
|
|
76
|
+
if (queryResults.length === 0)
|
|
82
77
|
return undefined;
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.
|
|
87
|
-
const transactionRes = await this.#transactionBuilder.executeTransaction();
|
|
88
|
-
return this.resolveFindByIdIncludesResults(sortedQueryResults.item, transactionRes, relationsObj.relationsLookup);
|
|
78
|
+
const filtered = this.filterQueryResults(id, queryResults);
|
|
79
|
+
if (filtered.entity === undefined)
|
|
80
|
+
return;
|
|
81
|
+
return this.resolveFindByIdIncludesResults(filtered.entity, filtered.relatedEntities, includedRelMeta);
|
|
89
82
|
}
|
|
90
83
|
/**
|
|
91
|
-
*
|
|
92
|
-
* @param id
|
|
93
|
-
* @param
|
|
84
|
+
* Filters query results by parent entity and related entities for processing
|
|
85
|
+
* @param entityId - The id of the parent entity
|
|
86
|
+
* @param queryResults - The results of the query on the parent partition
|
|
94
87
|
* @returns
|
|
95
88
|
*/
|
|
96
|
-
|
|
97
|
-
const modelPartitionKey = this.tableMetadata.partitionKeyAttribute.name;
|
|
98
|
-
const partitionFilter = (0, Filters_1.includedRelationshipsFilter)(this.EntityClass.name, includedRelationships);
|
|
99
|
-
return new query_utils_1.QueryBuilder({
|
|
100
|
-
entityClassName: this.EntityClass.name,
|
|
101
|
-
key: { [modelPartitionKey]: this.EntityClass.partitionKeyValue(id) },
|
|
102
|
-
options: { filter: partitionFilter }
|
|
103
|
-
}).build();
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Sort query results into an object containing:
|
|
107
|
-
* - item: The FindById DynamoItem
|
|
108
|
-
* - belongsToLinks: BelongsToLinkDynamoItem records for each of the included relationships
|
|
109
|
-
* @param queryResults
|
|
110
|
-
* @returns
|
|
111
|
-
*/
|
|
112
|
-
filterQueryResults(queryResults) {
|
|
89
|
+
filterQueryResults(entityId, queryResults) {
|
|
113
90
|
return queryResults.reduce((acc, res) => {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
acc.item = res;
|
|
117
|
-
if ((0, utils_1.isBelongsToLinkDynamoItem)(res, this.tableMetadata))
|
|
118
|
-
acc.belongsToLinks.push(res);
|
|
119
|
-
return acc;
|
|
120
|
-
}, { item: {}, belongsToLinks: [] });
|
|
121
|
-
}
|
|
122
|
-
buildGetIncludedRelationshipsTransaction(sortedQueryResults, relationsObj) {
|
|
123
|
-
this.buildGetRelationshipsThroughLinksTransaction(sortedQueryResults.belongsToLinks, relationsObj.relationsLookup);
|
|
124
|
-
this.buildGetRelationshipsThroughForeignKeyTransaction(sortedQueryResults.item, relationsObj.belongsToRelationships);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Builds transactions to get an associated entity via a BelongsToLink (HasOne or HasMany)
|
|
128
|
-
* @param belongsToLinks
|
|
129
|
-
* @param relationsLookup
|
|
130
|
-
*/
|
|
131
|
-
buildGetRelationshipsThroughLinksTransaction(belongsToLinks, relationsLookup) {
|
|
132
|
-
const { name: tableName, defaultAttributes } = this.tableMetadata;
|
|
133
|
-
belongsToLinks.forEach(link => {
|
|
134
|
-
const foreignKey = link[defaultAttributes.foreignKey.alias];
|
|
135
|
-
const foreignEntityType = link[defaultAttributes.foreignEntityType.alias];
|
|
136
|
-
if ((0, utils_1.isPropertyKey)(foreignEntityType) && (0, utils_1.isString)(foreignKey)) {
|
|
137
|
-
const rel = relationsLookup[foreignEntityType];
|
|
138
|
-
this.#transactionBuilder.addGet({
|
|
139
|
-
TableName: tableName,
|
|
140
|
-
Key: {
|
|
141
|
-
[this.partitionKeyAlias]: rel.target.partitionKeyValue(foreignKey),
|
|
142
|
-
[this.sortKeyAlias]: rel.target.name
|
|
143
|
-
}
|
|
144
|
-
});
|
|
91
|
+
if (entityId === res.id) {
|
|
92
|
+
acc.entity = res;
|
|
145
93
|
}
|
|
146
94
|
else {
|
|
147
|
-
|
|
148
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
149
|
-
`Corrupted foreign key value. Invalid type. ${foreignEntityType} - ${foreignKey}`);
|
|
95
|
+
acc.relatedEntities.push(res);
|
|
150
96
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Builds transactions to get an associated entity using a foreign key on the parent model (BelongsTo)
|
|
155
|
-
* @param belongsToLinks
|
|
156
|
-
* @param relationsLookup
|
|
157
|
-
*/
|
|
158
|
-
buildGetRelationshipsThroughForeignKeyTransaction(item, belongsToRelationships) {
|
|
159
|
-
if (belongsToRelationships.length > 0) {
|
|
160
|
-
const { name: tableName } = this.tableMetadata;
|
|
161
|
-
const attributes = metadata_1.default.getEntityAttributes(this.EntityClass.name);
|
|
162
|
-
belongsToRelationships.forEach(rel => {
|
|
163
|
-
const foreignKeyTableAlias = attributes[rel.foreignKey].alias;
|
|
164
|
-
const foreignKeyVal = item[foreignKeyTableAlias];
|
|
165
|
-
if (foreignKeyVal === undefined)
|
|
166
|
-
return;
|
|
167
|
-
this.#transactionBuilder.addGet({
|
|
168
|
-
TableName: tableName,
|
|
169
|
-
Key: {
|
|
170
|
-
[this.partitionKeyAlias]: rel.target.partitionKeyValue(foreignKeyVal),
|
|
171
|
-
[this.sortKeyAlias]: rel.target.name
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
}
|
|
97
|
+
return acc;
|
|
98
|
+
}, { entity: undefined, relatedEntities: [] });
|
|
176
99
|
}
|
|
177
100
|
/**
|
|
178
101
|
* Get relationship metadata for the associations included in the query
|
|
@@ -189,35 +112,25 @@ class FindById extends OperationBase_1.default {
|
|
|
189
112
|
}, []);
|
|
190
113
|
}
|
|
191
114
|
/**
|
|
192
|
-
*
|
|
193
|
-
* @param
|
|
194
|
-
* @param
|
|
195
|
-
* @param
|
|
115
|
+
* Resolve query results into parent entity with included relationships
|
|
116
|
+
* @param parentEntity
|
|
117
|
+
* @param relatedEntities
|
|
118
|
+
* @param includedRelMeta
|
|
196
119
|
* @returns
|
|
197
120
|
*/
|
|
198
|
-
resolveFindByIdIncludesResults(
|
|
199
|
-
const
|
|
200
|
-
const typeAlias = this.tableMetadata.defaultAttributes.type.alias;
|
|
121
|
+
resolveFindByIdIncludesResults(parentEntity, relatedEntities, includedRelMeta) {
|
|
122
|
+
const { relationsLookup } = (0, utils_2.buildEntityRelationshipMetaObj)(includedRelMeta);
|
|
201
123
|
this.setIncludedRelationshipDefaults(parentEntity, relationsLookup);
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
if (
|
|
205
|
-
const
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
Object.assign(parentEntity, { [rel.propertyName]: entities });
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
Object.assign(parentEntity, {
|
|
217
|
-
[rel.propertyName]: (0, utils_1.tableItemToEntity)(rel.target, tableItem)
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
}
|
|
124
|
+
relatedEntities.forEach(entity => {
|
|
125
|
+
const rel = relationsLookup[entity.type];
|
|
126
|
+
if (rel.type === "HasMany" || rel.type === "HasAndBelongsToMany") {
|
|
127
|
+
const entities = parentEntity[rel.propertyName];
|
|
128
|
+
if (Array.isArray(entities))
|
|
129
|
+
entities.push(entity);
|
|
130
|
+
Object.assign(parentEntity, { [rel.propertyName]: entities });
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
Object.assign(parentEntity, { [rel.propertyName]: entity });
|
|
221
134
|
}
|
|
222
135
|
});
|
|
223
136
|
return parentEntity;
|
|
@@ -232,8 +145,6 @@ class FindById extends OperationBase_1.default {
|
|
|
232
145
|
*/
|
|
233
146
|
setIncludedRelationshipDefaults(parentEntity, relationsLookup) {
|
|
234
147
|
Object.values(relationsLookup).forEach(rel => {
|
|
235
|
-
if (!(0, utils_1.isKeyOfEntity)(parentEntity, rel.propertyName))
|
|
236
|
-
return;
|
|
237
148
|
if ((0, utils_3.isHasManyRelationship)(rel) ||
|
|
238
149
|
(0, utils_3.isHasAndBelongsToManyRelationship)(rel)) {
|
|
239
150
|
(0, utils_1.safeAssign)(parentEntity, rel.propertyName, []);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { QueryItems } from "../../dynamo-utils";
|
|
2
1
|
import type DynaRecord from "../../DynaRecord";
|
|
3
|
-
import type {
|
|
4
|
-
import type
|
|
2
|
+
import type { EntityAttributesOnly, FunctionFields, RelationshipAttributeNames } from "../types";
|
|
3
|
+
import { type QueryResult, type QueryResults } from "../Query";
|
|
5
4
|
/**
|
|
6
5
|
* Defines options for the `FindById` operation, allowing specification of additional associations to include in the query result.
|
|
7
6
|
*
|
|
@@ -21,14 +20,14 @@ export interface FindByIdOptions<T extends DynaRecord> {
|
|
|
21
20
|
*/
|
|
22
21
|
export type IncludedAssociations<T extends DynaRecord> = NonNullable<FindByIdOptions<T>["include"]>;
|
|
23
22
|
/**
|
|
24
|
-
* Describes the structure of query results, sorting them into the main entity item and any associated
|
|
23
|
+
* Describes the structure of query results, sorting them into the main entity item and any associated items. Used during processing
|
|
25
24
|
*
|
|
26
25
|
* @property {QueryItems[number]} item - The main entity item retrieved from the query.
|
|
27
|
-
* @property {
|
|
26
|
+
* @property {QueryItems[]} relatedEntities - An array of `denormalized records` instances associated with the main entity item.
|
|
28
27
|
*/
|
|
29
28
|
export interface SortedQueryResults {
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
entity?: QueryResult<DynaRecord>;
|
|
30
|
+
relatedEntities: QueryResults<DynaRecord>;
|
|
32
31
|
}
|
|
33
32
|
/**
|
|
34
33
|
* Derives the keys of included associations from `FindByIdOptions`, representing the names of relationships specified to be included in the query result.
|
|
@@ -44,7 +43,7 @@ type IncludedKeys<T extends DynaRecord, Opts extends FindByIdOptions<T>> = Opts
|
|
|
44
43
|
* @template P - The properties of the entity `T`.
|
|
45
44
|
*/
|
|
46
45
|
type EntityKeysWithIncludedAssociations<T extends DynaRecord, P extends keyof T> = {
|
|
47
|
-
[K in P]: T[K] extends DynaRecord ?
|
|
46
|
+
[K in P]: T[K] extends DynaRecord ? EntityAttributesOnly<T> : T[K] extends DynaRecord[] ? Array<EntityAttributesOnly<T[K][number]>> : T[K];
|
|
48
47
|
};
|
|
49
48
|
/**
|
|
50
49
|
* Represents the result of a `FindById` operation, including the main entity and any specified associated entities.
|
|
@@ -52,6 +51,6 @@ type EntityKeysWithIncludedAssociations<T extends DynaRecord, P extends keyof T>
|
|
|
52
51
|
* @template T - The type of the main entity, extending `DynaRecord`.
|
|
53
52
|
* @template Opts - The options for the `FindById` operation, specifying included associations.
|
|
54
53
|
*/
|
|
55
|
-
export type FindByIdIncludesRes<T extends DynaRecord, Opts extends FindByIdOptions<T>> = EntityKeysWithIncludedAssociations<T, keyof
|
|
54
|
+
export type FindByIdIncludesRes<T extends DynaRecord, Opts extends FindByIdOptions<T>> = EntityKeysWithIncludedAssociations<T, keyof EntityAttributesOnly<T> | IncludedKeys<T, Opts> | FunctionFields<T>>;
|
|
56
55
|
export {};
|
|
57
56
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/FindById/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/FindById/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EACV,oBAAoB,EACpB,cAAc,EACd,0BAA0B,EAC3B,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AAE/D;;;;;;GAMG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,UAAU;IACnD,OAAO,CAAC,EAAE,KAAK,CAAC;QAAE,WAAW,EAAE,0BAA0B,CAAC,CAAC,CAAC,CAAA;KAAE,CAAC,CAAC;CACjE;AAED;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,UAAU,IAAI,WAAW,CAClE,eAAe,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAC9B,CAAC;AAEF;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE,WAAW,CAAC,UAAU,CAAC,CAAC;IACjC,eAAe,EAAE,YAAY,CAAC,UAAU,CAAC,CAAC;CAC3C;AAED;;;;;GAKG;AACH,KAAK,YAAY,CAAC,CAAC,SAAS,UAAU,EAAE,IAAI,SAAS,eAAe,CAAC,CAAC,CAAC,IACrE,IAAI,SAAS,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,GACrC,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,GACxD,KAAK,CAAC;AAEZ;;;;;GAKG;AACH,KAAK,kCAAkC,CACrC,CAAC,SAAS,UAAU,EACpB,CAAC,SAAS,MAAM,CAAC,IACf;KACD,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,UAAU,GAC7B,oBAAoB,CAAC,CAAC,CAAC,GACvB,CAAC,CAAC,CAAC,CAAC,SAAS,UAAU,EAAE,GACvB,KAAK,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GACzC,CAAC,CAAC,CAAC,CAAC;CACX,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,CAC7B,CAAC,SAAS,UAAU,EACpB,IAAI,SAAS,eAAe,CAAC,CAAC,CAAC,IAC7B,kCAAkC,CACpC,CAAC,EACD,MAAM,oBAAoB,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAC1E,CAAC"}
|
|
@@ -14,7 +14,7 @@ declare class Query<T extends DynaRecord> extends OperationBase<T> {
|
|
|
14
14
|
* Run the query operation
|
|
15
15
|
* @param key EntityId or object with PartitionKey and optional SortKey conditions
|
|
16
16
|
* @param options Filter conditions, indexName, or SortKey conditions if querying by keys
|
|
17
|
-
* @returns Array of Entity or
|
|
17
|
+
* @returns Array of Entity or denormalized records
|
|
18
18
|
*/
|
|
19
19
|
run(key: string | EntityKeyConditions<T>, options?: QueryBuilderOptions | Omit<QueryOptions, "indexName">): Promise<QueryResults<T>>;
|
|
20
20
|
/**
|
|
@@ -40,6 +40,7 @@ declare class Query<T extends DynaRecord> extends OperationBase<T> {
|
|
|
40
40
|
* @returns
|
|
41
41
|
*/
|
|
42
42
|
private resolveQueryResults;
|
|
43
|
+
private tableItemToLinkedEntity;
|
|
43
44
|
}
|
|
44
45
|
export default Query;
|
|
45
46
|
//# sourceMappingURL=Query.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Query.d.ts","sourceRoot":"","sources":["../../../../src/operations/Query/Query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"Query.d.ts","sourceRoot":"","sources":["../../../../src/operations/Query/Query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAEL,KAAK,YAAY,IAAI,mBAAmB,EACzC,MAAM,mBAAmB,CAAC;AAI3B,OAAO,aAAa,MAAM,kBAAkB,CAAC;AAC7C,OAAO,KAAK,EACV,mBAAmB,EACnB,YAAY,EAEZ,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB;;;;;;GAMG;AACH,cAAM,KAAK,CAAC,CAAC,SAAS,UAAU,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;IACxD;;;;;OAKG;IACU,GAAG,CACd,GAAG,EAAE,MAAM,GAAG,mBAAmB,CAAC,CAAC,CAAC,EACpC,OAAO,CAAC,EAAE,mBAAmB,GAAG,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC,GAC9D,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;IAQ3B;;;;;;OAMG;YACW,UAAU;IAexB;;;;;;;OAOG;YACW,WAAW;IAyBzB;;;;OAIG;IACH,OAAO,CAAC,mBAAmB;IAY3B,OAAO,CAAC,uBAAuB;CAchC;AAED,eAAe,KAAK,CAAC"}
|
|
@@ -3,6 +3,7 @@ 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
|
+
const metadata_1 = __importDefault(require("../../metadata"));
|
|
6
7
|
const query_utils_1 = require("../../query-utils");
|
|
7
8
|
const DynamoClient_1 = __importDefault(require("../../dynamo-utils/DynamoClient"));
|
|
8
9
|
const utils_1 = require("../../utils");
|
|
@@ -19,7 +20,7 @@ class Query extends OperationBase_1.default {
|
|
|
19
20
|
* Run the query operation
|
|
20
21
|
* @param key EntityId or object with PartitionKey and optional SortKey conditions
|
|
21
22
|
* @param options Filter conditions, indexName, or SortKey conditions if querying by keys
|
|
22
|
-
* @returns Array of Entity or
|
|
23
|
+
* @returns Array of Entity or denormalized records
|
|
23
24
|
*/
|
|
24
25
|
async run(key, options) {
|
|
25
26
|
if (typeof key === "string") {
|
|
@@ -76,9 +77,21 @@ class Query extends OperationBase_1.default {
|
|
|
76
77
|
* @returns
|
|
77
78
|
*/
|
|
78
79
|
resolveQueryResults(queryResults) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
const typeAlias = this.tableMetadata.defaultAttributes.type.alias;
|
|
81
|
+
return queryResults.map(res => res[typeAlias] === this.EntityClass.name
|
|
82
|
+
? (0, utils_1.tableItemToEntity)(this.EntityClass, res)
|
|
83
|
+
: this.tableItemToLinkedEntity(res));
|
|
84
|
+
}
|
|
85
|
+
tableItemToLinkedEntity(tableItem) {
|
|
86
|
+
const typeAlias = this.tableMetadata.defaultAttributes.type.alias;
|
|
87
|
+
const entityName = tableItem[typeAlias];
|
|
88
|
+
if ((0, utils_1.isString)(entityName)) {
|
|
89
|
+
const entityMeta = metadata_1.default.getEntity(entityName);
|
|
90
|
+
return (0, utils_1.tableItemToEntity)(entityMeta.EntityClass, tableItem);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw new Error("Malformed data. Unable to infer entity type");
|
|
94
|
+
}
|
|
82
95
|
}
|
|
83
96
|
}
|
|
84
97
|
exports.default = Query;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type DynaRecord from "../../DynaRecord";
|
|
2
2
|
import type { KeyConditions, QueryOptions as QueryBuilderOptions, SortKeyCondition } from "../../query-utils";
|
|
3
|
-
import type {
|
|
4
|
-
import type { EntityAttributes } from "../types";
|
|
3
|
+
import type { EntityAttributesInstance } from "../types";
|
|
5
4
|
/**
|
|
6
5
|
* Extends the basic query builder options by adding an optional sort key condition for more precise querying capabilities.
|
|
7
6
|
*
|
|
@@ -24,13 +23,48 @@ export type EntityKeyConditions<T> = {
|
|
|
24
23
|
[K in keyof T]?: KeyConditions;
|
|
25
24
|
};
|
|
26
25
|
/**
|
|
27
|
-
*
|
|
26
|
+
* Asserts that a given type `E` extends `DynaRecord`. If `E` does not extend `DynaRecord`, it resolves to `never`.
|
|
27
|
+
*
|
|
28
|
+
* This is used to enforce that only valid `DynaRecord` types are included in relationships or other dependent types.
|
|
29
|
+
*
|
|
30
|
+
* @template E - The type to assert.
|
|
31
|
+
*/
|
|
32
|
+
export type AssertDynaRecord<E> = E extends DynaRecord ? E : never;
|
|
33
|
+
/**
|
|
34
|
+
* Extracts the keys of a `DynaRecord` type `T` whose properties are either a `DynaRecord` or an array of `DynaRecord`.
|
|
35
|
+
*
|
|
36
|
+
* This type is used to identify properties in a `DynaRecord` that represent relationships to other entities.
|
|
37
|
+
*
|
|
38
|
+
* @template T - The `DynaRecord` type to evaluate.
|
|
39
|
+
* @returns A union of property names in `T` that represent relationships.
|
|
40
|
+
*/
|
|
41
|
+
export type RelationshipProperties<T extends DynaRecord> = Extract<{
|
|
42
|
+
[K in keyof T]: NonNullable<T[K]> extends DynaRecord | DynaRecord[] ? K : never;
|
|
43
|
+
}[keyof T], string>;
|
|
44
|
+
/**
|
|
45
|
+
* Maps the relationship properties of a `DynaRecord` type `T` to their corresponding entity types.
|
|
46
|
+
*
|
|
47
|
+
* For array relationships, this resolves to the entity type of the array elements.
|
|
48
|
+
* For single relationships, this resolves to the entity type of the property.
|
|
49
|
+
*
|
|
50
|
+
* @template T - The `DynaRecord` type to evaluate.
|
|
51
|
+
* @returns A union of all entity types referenced by the relationships in `T`.
|
|
52
|
+
*/
|
|
53
|
+
export type RelationshipEntities<T extends DynaRecord> = {
|
|
54
|
+
[K in RelationshipProperties<T>]: NonNullable<T[K]> extends Array<infer U> ? AssertDynaRecord<U> : AssertDynaRecord<NonNullable<T[K]>>;
|
|
55
|
+
}[RelationshipProperties<T>];
|
|
56
|
+
/**
|
|
57
|
+
* Represents the results of a query operation for a `DynaRecord` entity `T`.
|
|
58
|
+
*
|
|
59
|
+
* The results include attributes of the queried entity as well as attributes of its related entities.
|
|
60
|
+
* For related entities, their attributes are derived from their relationship definitions in `T`.
|
|
28
61
|
*
|
|
29
62
|
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
63
|
+
* @returns An array of objects representing the entity attributes of `T` or its related entities.
|
|
30
64
|
*/
|
|
31
|
-
export type QueryResults<T extends DynaRecord> = Array<
|
|
65
|
+
export type QueryResults<T extends DynaRecord> = Array<EntityAttributesInstance<T> | (RelationshipEntities<T> extends infer R ? R extends DynaRecord ? EntityAttributesInstance<R> : never : never)>;
|
|
32
66
|
/**
|
|
33
|
-
* A utility type that represents a single item in the query results, which can be either an entity or a
|
|
67
|
+
* A utility type that represents a single item in the query results, which can be either an entity or a associated entity. This type is derived from the `QueryResults` array type, providing a convenient way to refer to individual results from a query.
|
|
34
68
|
*
|
|
35
69
|
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
36
70
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/Query/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EACV,aAAa,EACb,YAAY,IAAI,mBAAmB,EACnC,gBAAgB,EACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/Query/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EACV,aAAa,EACb,YAAY,IAAI,mBAAmB,EACnC,gBAAgB,EACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,UAAU,CAAC;AAEzD;;;;;GAKG;AACH,MAAM,WAAW,YAAa,SAAQ,mBAAmB;IACvD;;OAEG;IACH,WAAW,CAAC,EAAE,gBAAgB,CAAC;CAChC;AAED;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI;KAClC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,aAAa;CAC/B,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,GAAG,CAAC,GAAG,KAAK,CAAC;AAEnE;;;;;;;GAOG;AACH,MAAM,MAAM,sBAAsB,CAAC,CAAC,SAAS,UAAU,IAAI,OAAO,CAChE;KACG,CAAC,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU,EAAE,GAC/D,CAAC,GACD,KAAK;CACV,CAAC,MAAM,CAAC,CAAC,EACV,MAAM,CACP,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,UAAU,IAAI;KACtD,CAAC,IAAI,sBAAsB,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GACtE,gBAAgB,CAAC,CAAC,CAAC,GACnB,gBAAgB,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACxC,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,CAAC;AAE7B;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,UAAU,IAAI,KAAK,CAClD,wBAAwB,CAAC,CAAC,CAAC,GAC3B,CAAC,oBAAoB,CAAC,CAAC,CAAC,SAAS,MAAM,CAAC,GACpC,CAAC,SAAS,UAAU,GAClB,wBAAwB,CAAC,CAAC,CAAC,GAC3B,KAAK,GACP,KAAK,CAAC,CACb,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,UAAU,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC"}
|