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.
- package/README.md +3 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/src/DynaRecord.d.ts +216 -0
- package/dist/src/DynaRecord.d.ts.map +1 -0
- package/dist/src/DynaRecord.js +217 -0
- package/dist/src/Logger.d.ts +42 -0
- package/dist/src/Logger.d.ts.map +1 -0
- package/dist/src/Logger.js +57 -0
- package/dist/src/decorators/Entity.d.ts +23 -0
- package/dist/src/decorators/Entity.d.ts.map +1 -0
- package/dist/src/decorators/Entity.js +32 -0
- package/dist/src/decorators/Table.d.ts +22 -0
- package/dist/src/decorators/Table.d.ts.map +1 -0
- package/dist/src/decorators/Table.js +31 -0
- package/dist/src/decorators/attributes/Attribute.d.ts +26 -0
- package/dist/src/decorators/attributes/Attribute.d.ts.map +1 -0
- package/dist/src/decorators/attributes/Attribute.js +41 -0
- package/dist/src/decorators/attributes/DateAttribute.d.ts +25 -0
- package/dist/src/decorators/attributes/DateAttribute.d.ts.map +1 -0
- package/dist/src/decorators/attributes/DateAttribute.js +43 -0
- package/dist/src/decorators/attributes/DateNullableAttribute.d.ts +26 -0
- package/dist/src/decorators/attributes/DateNullableAttribute.d.ts.map +1 -0
- package/dist/src/decorators/attributes/DateNullableAttribute.js +43 -0
- package/dist/src/decorators/attributes/ForeignKeyAttribute.d.ts +25 -0
- package/dist/src/decorators/attributes/ForeignKeyAttribute.d.ts.map +1 -0
- package/dist/src/decorators/attributes/ForeignKeyAttribute.js +40 -0
- package/dist/src/decorators/attributes/NullableAttribute.d.ts +27 -0
- package/dist/src/decorators/attributes/NullableAttribute.d.ts.map +1 -0
- package/dist/src/decorators/attributes/NullableAttribute.js +41 -0
- package/dist/src/decorators/attributes/NullableForeignKeyAttribute.d.ts +25 -0
- package/dist/src/decorators/attributes/NullableForeignKeyAttribute.d.ts.map +1 -0
- package/dist/src/decorators/attributes/NullableForeignKeyAttribute.js +40 -0
- package/dist/src/decorators/attributes/PartitionKeyAttribute.d.ts +24 -0
- package/dist/src/decorators/attributes/PartitionKeyAttribute.d.ts.map +1 -0
- package/dist/src/decorators/attributes/PartitionKeyAttribute.js +38 -0
- package/dist/src/decorators/attributes/SortKeyAttribute.d.ts +24 -0
- package/dist/src/decorators/attributes/SortKeyAttribute.d.ts.map +1 -0
- package/dist/src/decorators/attributes/SortKeyAttribute.js +38 -0
- package/dist/src/decorators/attributes/index.d.ts +10 -0
- package/dist/src/decorators/attributes/index.d.ts.map +1 -0
- package/dist/src/decorators/attributes/index.js +37 -0
- package/dist/src/decorators/attributes/serializers.d.ts +13 -0
- package/dist/src/decorators/attributes/serializers.d.ts.map +1 -0
- package/dist/src/decorators/attributes/serializers.js +19 -0
- package/dist/src/decorators/index.d.ts +6 -0
- package/dist/src/decorators/index.d.ts.map +1 -0
- package/dist/src/decorators/index.js +27 -0
- package/dist/src/decorators/relationships/BelongsTo.d.ts +34 -0
- package/dist/src/decorators/relationships/BelongsTo.d.ts.map +1 -0
- package/dist/src/decorators/relationships/BelongsTo.js +50 -0
- package/dist/src/decorators/relationships/HasAndBelongsToMany.d.ts +80 -0
- package/dist/src/decorators/relationships/HasAndBelongsToMany.d.ts.map +1 -0
- package/dist/src/decorators/relationships/HasAndBelongsToMany.js +70 -0
- package/dist/src/decorators/relationships/HasMany.d.ts +35 -0
- package/dist/src/decorators/relationships/HasMany.d.ts.map +1 -0
- package/dist/src/decorators/relationships/HasMany.js +48 -0
- package/dist/src/decorators/relationships/HasOne.d.ts +35 -0
- package/dist/src/decorators/relationships/HasOne.d.ts.map +1 -0
- package/dist/src/decorators/relationships/HasOne.js +48 -0
- package/dist/src/decorators/relationships/index.d.ts +6 -0
- package/dist/src/decorators/relationships/index.d.ts.map +1 -0
- package/dist/src/decorators/relationships/index.js +29 -0
- package/dist/src/decorators/relationships/types.d.ts +11 -0
- package/dist/src/decorators/relationships/types.d.ts.map +1 -0
- package/dist/src/decorators/relationships/types.js +2 -0
- package/dist/src/decorators/types.d.ts +30 -0
- package/dist/src/decorators/types.d.ts.map +1 -0
- package/dist/src/decorators/types.js +2 -0
- package/dist/src/dynamo-utils/DynamoClient.d.ts +34 -0
- package/dist/src/dynamo-utils/DynamoClient.d.ts.map +1 -0
- package/dist/src/dynamo-utils/DynamoClient.js +57 -0
- package/dist/src/dynamo-utils/TransactGetBuilder.d.ts +18 -0
- package/dist/src/dynamo-utils/TransactGetBuilder.d.ts.map +1 -0
- package/dist/src/dynamo-utils/TransactGetBuilder.js +32 -0
- package/dist/src/dynamo-utils/TransactWriteBuilder.d.ts +46 -0
- package/dist/src/dynamo-utils/TransactWriteBuilder.d.ts.map +1 -0
- package/dist/src/dynamo-utils/TransactWriteBuilder.js +100 -0
- package/dist/src/dynamo-utils/errors.d.ts +13 -0
- package/dist/src/dynamo-utils/errors.d.ts.map +1 -0
- package/dist/src/dynamo-utils/errors.js +17 -0
- package/dist/src/dynamo-utils/index.d.ts +8 -0
- package/dist/src/dynamo-utils/index.d.ts.map +1 -0
- package/dist/src/dynamo-utils/index.js +30 -0
- package/dist/src/dynamo-utils/types.d.ts +52 -0
- package/dist/src/dynamo-utils/types.d.ts.map +1 -0
- package/dist/src/dynamo-utils/types.js +2 -0
- package/dist/src/errors.d.ts +13 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +17 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +24 -0
- package/dist/src/metadata/AttributeMetadata.d.ts +22 -0
- package/dist/src/metadata/AttributeMetadata.d.ts.map +1 -0
- package/dist/src/metadata/AttributeMetadata.js +27 -0
- package/dist/src/metadata/EntityMetadata.d.ts +42 -0
- package/dist/src/metadata/EntityMetadata.d.ts.map +1 -0
- package/dist/src/metadata/EntityMetadata.js +49 -0
- package/dist/src/metadata/JoinTableMetadata.d.ts +23 -0
- package/dist/src/metadata/JoinTableMetadata.d.ts.map +1 -0
- package/dist/src/metadata/JoinTableMetadata.js +22 -0
- package/dist/src/metadata/MetadataStorage.d.ts +104 -0
- package/dist/src/metadata/MetadataStorage.d.ts.map +1 -0
- package/dist/src/metadata/MetadataStorage.js +194 -0
- package/dist/src/metadata/TableMetadata.d.ts +53 -0
- package/dist/src/metadata/TableMetadata.d.ts.map +1 -0
- package/dist/src/metadata/TableMetadata.js +100 -0
- package/dist/src/metadata/index.d.ts +13 -0
- package/dist/src/metadata/index.d.ts.map +1 -0
- package/dist/src/metadata/index.js +34 -0
- package/dist/src/metadata/relationship-metadata/BelongsToRelationship.d.ts +18 -0
- package/dist/src/metadata/relationship-metadata/BelongsToRelationship.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/BelongsToRelationship.js +26 -0
- package/dist/src/metadata/relationship-metadata/HasAndBelongsToManyRelationship.d.ts +16 -0
- package/dist/src/metadata/relationship-metadata/HasAndBelongsToManyRelationship.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/HasAndBelongsToManyRelationship.js +25 -0
- package/dist/src/metadata/relationship-metadata/HasManyRelationship.d.ts +18 -0
- package/dist/src/metadata/relationship-metadata/HasManyRelationship.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/HasManyRelationship.js +26 -0
- package/dist/src/metadata/relationship-metadata/HasOneRelationship.d.ts +17 -0
- package/dist/src/metadata/relationship-metadata/HasOneRelationship.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/HasOneRelationship.js +25 -0
- package/dist/src/metadata/relationship-metadata/RelationshipMetadata.d.ts +21 -0
- package/dist/src/metadata/relationship-metadata/RelationshipMetadata.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/RelationshipMetadata.js +18 -0
- package/dist/src/metadata/relationship-metadata/index.d.ts +7 -0
- package/dist/src/metadata/relationship-metadata/index.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/index.js +29 -0
- package/dist/src/metadata/relationship-metadata/types.d.ts +16 -0
- package/dist/src/metadata/relationship-metadata/types.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/types.js +2 -0
- package/dist/src/metadata/relationship-metadata/utils.d.ts +3 -0
- package/dist/src/metadata/relationship-metadata/utils.d.ts.map +1 -0
- package/dist/src/metadata/relationship-metadata/utils.js +25 -0
- package/dist/src/metadata/types.d.ts +90 -0
- package/dist/src/metadata/types.d.ts.map +1 -0
- package/dist/src/metadata/types.js +2 -0
- package/dist/src/metadata/utils.d.ts +42 -0
- package/dist/src/metadata/utils.d.ts.map +1 -0
- package/dist/src/metadata/utils.js +67 -0
- package/dist/src/operations/Create/Create.d.ts +39 -0
- package/dist/src/operations/Create/Create.d.ts.map +1 -0
- package/dist/src/operations/Create/Create.js +84 -0
- package/dist/src/operations/Create/index.d.ts +3 -0
- package/dist/src/operations/Create/index.d.ts.map +1 -0
- package/dist/src/operations/Create/index.js +23 -0
- package/dist/src/operations/Create/types.d.ts +7 -0
- package/dist/src/operations/Create/types.d.ts.map +1 -0
- package/dist/src/operations/Create/types.js +2 -0
- package/dist/src/operations/Delete/Delete.d.ts +74 -0
- package/dist/src/operations/Delete/Delete.d.ts.map +1 -0
- package/dist/src/operations/Delete/Delete.js +207 -0
- package/dist/src/operations/Delete/index.d.ts +2 -0
- package/dist/src/operations/Delete/index.d.ts.map +1 -0
- package/dist/src/operations/Delete/index.js +8 -0
- package/dist/src/operations/Delete/types.d.ts +7 -0
- package/dist/src/operations/Delete/types.d.ts.map +1 -0
- package/dist/src/operations/Delete/types.js +2 -0
- package/dist/src/operations/FindById/FindById.d.ts +91 -0
- package/dist/src/operations/FindById/FindById.d.ts.map +1 -0
- package/dist/src/operations/FindById/FindById.js +244 -0
- package/dist/src/operations/FindById/index.d.ts +3 -0
- package/dist/src/operations/FindById/index.d.ts.map +1 -0
- package/dist/src/operations/FindById/index.js +23 -0
- package/dist/src/operations/FindById/types.d.ts +57 -0
- package/dist/src/operations/FindById/types.d.ts.map +1 -0
- package/dist/src/operations/FindById/types.js +2 -0
- package/dist/src/operations/OperationBase.d.ts +37 -0
- package/dist/src/operations/OperationBase.d.ts.map +1 -0
- package/dist/src/operations/OperationBase.js +44 -0
- package/dist/src/operations/Query/Query.d.ts +45 -0
- package/dist/src/operations/Query/Query.d.ts.map +1 -0
- package/dist/src/operations/Query/Query.js +84 -0
- package/dist/src/operations/Query/index.d.ts +3 -0
- package/dist/src/operations/Query/index.d.ts.map +1 -0
- package/dist/src/operations/Query/index.js +23 -0
- package/dist/src/operations/Query/types.d.ts +38 -0
- package/dist/src/operations/Query/types.d.ts.map +1 -0
- package/dist/src/operations/Query/types.js +2 -0
- package/dist/src/operations/Update/Update.d.ts +48 -0
- package/dist/src/operations/Update/Update.d.ts.map +1 -0
- package/dist/src/operations/Update/Update.js +118 -0
- package/dist/src/operations/Update/index.d.ts +3 -0
- package/dist/src/operations/Update/index.d.ts.map +1 -0
- package/dist/src/operations/Update/index.js +23 -0
- package/dist/src/operations/Update/types.d.ts +32 -0
- package/dist/src/operations/Update/types.d.ts.map +1 -0
- package/dist/src/operations/Update/types.js +2 -0
- package/dist/src/operations/index.d.ts +7 -0
- package/dist/src/operations/index.d.ts.map +1 -0
- package/dist/src/operations/index.js +22 -0
- package/dist/src/operations/types.d.ts +59 -0
- package/dist/src/operations/types.d.ts.map +1 -0
- package/dist/src/operations/types.js +2 -0
- package/dist/src/operations/utils/RelationshipTransactions.d.ts +64 -0
- package/dist/src/operations/utils/RelationshipTransactions.d.ts.map +1 -0
- package/dist/src/operations/utils/RelationshipTransactions.js +125 -0
- package/dist/src/operations/utils/expressionBuilder.d.ts +9 -0
- package/dist/src/operations/utils/expressionBuilder.d.ts.map +1 -0
- package/dist/src/operations/utils/expressionBuilder.js +106 -0
- package/dist/src/operations/utils/index.d.ts +5 -0
- package/dist/src/operations/utils/index.d.ts.map +1 -0
- package/dist/src/operations/utils/index.js +25 -0
- package/dist/src/operations/utils/types.d.ts +21 -0
- package/dist/src/operations/utils/types.d.ts.map +1 -0
- package/dist/src/operations/utils/types.js +2 -0
- package/dist/src/operations/utils/utils.d.ts +19 -0
- package/dist/src/operations/utils/utils.d.ts.map +1 -0
- package/dist/src/operations/utils/utils.js +36 -0
- package/dist/src/query-utils/Filters.d.ts +10 -0
- package/dist/src/query-utils/Filters.d.ts.map +1 -0
- package/dist/src/query-utils/Filters.js +25 -0
- package/dist/src/query-utils/QueryBuilder.d.ts +90 -0
- package/dist/src/query-utils/QueryBuilder.d.ts.map +1 -0
- package/dist/src/query-utils/QueryBuilder.js +213 -0
- package/dist/src/query-utils/index.d.ts +3 -0
- package/dist/src/query-utils/index.d.ts.map +1 -0
- package/dist/src/query-utils/index.js +23 -0
- package/dist/src/query-utils/types.d.ts +89 -0
- package/dist/src/query-utils/types.d.ts.map +1 -0
- package/dist/src/query-utils/types.js +2 -0
- package/dist/src/relationships/BelongsToLink.d.ts +50 -0
- package/dist/src/relationships/BelongsToLink.d.ts.map +1 -0
- package/dist/src/relationships/BelongsToLink.js +57 -0
- package/dist/src/relationships/JoinTable.d.ts +68 -0
- package/dist/src/relationships/JoinTable.d.ts.map +1 -0
- package/dist/src/relationships/JoinTable.js +133 -0
- package/dist/src/relationships/index.d.ts +3 -0
- package/dist/src/relationships/index.d.ts.map +1 -0
- package/dist/src/relationships/index.js +10 -0
- package/dist/src/types.d.ts +73 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/utils.d.ts +89 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +168 -0
- package/package.json +61 -0
|
@@ -0,0 +1,244 @@
|
|
|
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 dynamo_utils_1 = require("../../dynamo-utils");
|
|
8
|
+
const query_utils_1 = require("../../query-utils");
|
|
9
|
+
const Filters_1 = require("../../query-utils/Filters");
|
|
10
|
+
const dynamo_utils_2 = require("../../dynamo-utils");
|
|
11
|
+
const utils_1 = require("../../utils");
|
|
12
|
+
const OperationBase_1 = __importDefault(require("../OperationBase"));
|
|
13
|
+
const utils_2 = require("../utils");
|
|
14
|
+
const utils_3 = require("../../metadata/utils");
|
|
15
|
+
const Logger_1 = __importDefault(require("../../Logger"));
|
|
16
|
+
/**
|
|
17
|
+
* Facilitates the retrieval of an entity by its identifier (ID) from the database, potentially including its associated entities based on specified relationships.
|
|
18
|
+
*
|
|
19
|
+
* 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.
|
|
20
|
+
*
|
|
21
|
+
* @template T - The type of the entity being retrieved, extending `DynaRecord`.
|
|
22
|
+
*/
|
|
23
|
+
class FindById extends OperationBase_1.default {
|
|
24
|
+
#transactionBuilder;
|
|
25
|
+
constructor(Entity) {
|
|
26
|
+
super(Entity);
|
|
27
|
+
this.#transactionBuilder = new dynamo_utils_2.TransactGetBuilder();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Find an entity by Id and optionally include associations
|
|
31
|
+
* @param {string} id - Entity Id
|
|
32
|
+
* @param {Object} options - FindById options
|
|
33
|
+
* @param {Object[]=} options.include - The associations to include in the query
|
|
34
|
+
* @param {string} options.include[].association - The name of the association to include. Must be defined on the model
|
|
35
|
+
* @returns An entity with optional included associations serialized
|
|
36
|
+
*/
|
|
37
|
+
async run(id, options) {
|
|
38
|
+
if (options?.include === undefined) {
|
|
39
|
+
return await this.findByIdOnly(id);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return await this.findByIdWithIncludes(id, options.include);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Find an Entity by id without associations
|
|
47
|
+
* @param {string} id - Entity Id
|
|
48
|
+
* @returns An entity object or undefined
|
|
49
|
+
*/
|
|
50
|
+
async findByIdOnly(id) {
|
|
51
|
+
const { name: tableName } = this.tableMetadata;
|
|
52
|
+
const res = await dynamo_utils_1.DynamoClient.getItem({
|
|
53
|
+
TableName: tableName,
|
|
54
|
+
Key: {
|
|
55
|
+
[this.partitionKeyAlias]: this.EntityClass.partitionKeyValue(id),
|
|
56
|
+
[this.sortKeyAlias]: this.EntityClass.name
|
|
57
|
+
},
|
|
58
|
+
ConsistentRead: true
|
|
59
|
+
});
|
|
60
|
+
if (res === undefined) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return (0, utils_1.tableItemToEntity)(this.EntityClass, res);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Find an entity with included associations
|
|
69
|
+
* @param {string} id - Entity Id
|
|
70
|
+
* @param {Object[]=} includedAssociations - The associations to include in the query
|
|
71
|
+
* @param {string} includedAssociations[].association - The name of the association to include. Must be defined on the model
|
|
72
|
+
* @returns An entity with included associations serialized
|
|
73
|
+
*/
|
|
74
|
+
async findByIdWithIncludes(id, includedAssociations) {
|
|
75
|
+
const includedRels = this.getIncludedRelationships(includedAssociations);
|
|
76
|
+
const params = this.buildFindByIdIncludesQuery(id, includedRels);
|
|
77
|
+
const queryResults = await dynamo_utils_1.DynamoClient.query({
|
|
78
|
+
...params,
|
|
79
|
+
ConsistentRead: true
|
|
80
|
+
});
|
|
81
|
+
if (queryResults.length === 0) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
const sortedQueryResults = this.filterQueryResults(queryResults);
|
|
85
|
+
const relationsObj = (0, utils_2.buildEntityRelationshipMetaObj)(includedRels);
|
|
86
|
+
this.buildGetIncludedRelationshipsTransaction(sortedQueryResults, relationsObj);
|
|
87
|
+
const transactionRes = await this.#transactionBuilder.executeTransaction();
|
|
88
|
+
return this.resolveFindByIdIncludesResults(sortedQueryResults.item, transactionRes, relationsObj.relationsLookup);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Build the query to find the entity, and any of the BelongsToLinks for the included models
|
|
92
|
+
* @param id
|
|
93
|
+
* @param includedRelationships
|
|
94
|
+
* @returns
|
|
95
|
+
*/
|
|
96
|
+
buildFindByIdIncludesQuery(id, includedRelationships) {
|
|
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) {
|
|
113
|
+
return queryResults.reduce((acc, res) => {
|
|
114
|
+
const typeAlias = this.tableMetadata.defaultAttributes.type.alias;
|
|
115
|
+
if (res[typeAlias] === this.EntityClass.name)
|
|
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
|
+
});
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
Logger_1.default.error(
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
149
|
+
`Corrupted foreign key value. Invalid type. ${foreignEntityType} - ${foreignKey}`);
|
|
150
|
+
}
|
|
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
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get relationship metadata for the associations included in the query
|
|
179
|
+
* @param includedAssociations
|
|
180
|
+
* @returns
|
|
181
|
+
*/
|
|
182
|
+
getIncludedRelationships(includedAssociations) {
|
|
183
|
+
return includedAssociations.reduce((acc, includedRel) => {
|
|
184
|
+
const key = includedRel.association;
|
|
185
|
+
const included = this.entityMetadata.relationships[key];
|
|
186
|
+
if (included !== undefined)
|
|
187
|
+
acc.push(included);
|
|
188
|
+
return acc;
|
|
189
|
+
}, []);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Serialize the FindById item to its class, serialize results from included relationships query onto the entity
|
|
193
|
+
* @param entityTableItem
|
|
194
|
+
* @param transactionResults
|
|
195
|
+
* @param relationsLookup
|
|
196
|
+
* @returns
|
|
197
|
+
*/
|
|
198
|
+
resolveFindByIdIncludesResults(entityTableItem, transactionResults, relationsLookup) {
|
|
199
|
+
const parentEntity = (0, utils_1.tableItemToEntity)(this.EntityClass, entityTableItem);
|
|
200
|
+
const typeAlias = this.tableMetadata.defaultAttributes.type.alias;
|
|
201
|
+
this.setIncludedRelationshipDefaults(parentEntity, relationsLookup);
|
|
202
|
+
transactionResults.forEach(res => {
|
|
203
|
+
const tableItem = res.Item;
|
|
204
|
+
if (tableItem !== undefined) {
|
|
205
|
+
const rel = relationsLookup[tableItem[typeAlias]];
|
|
206
|
+
if ((0, utils_1.isKeyOfEntity)(parentEntity, rel.propertyName)) {
|
|
207
|
+
if (rel.type === "HasMany" || rel.type === "HasAndBelongsToMany") {
|
|
208
|
+
const entity = (0, utils_1.tableItemToEntity)(rel.target, tableItem);
|
|
209
|
+
const entities = parentEntity[rel.propertyName] ?? [];
|
|
210
|
+
if (Array.isArray(entities)) {
|
|
211
|
+
entities.push(entity);
|
|
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
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
return parentEntity;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Initializes default values for included relationships on a parent entity. It assigns
|
|
227
|
+
* an empty array to each relationship property for HasMany and HasAndBelongsToMany specified in `relationsLookup` if the property exists on `parentEntity`.
|
|
228
|
+
*
|
|
229
|
+
* @param parentEntity - The parent entity to initialize relationship properties on.
|
|
230
|
+
* @param relationsLookup - A mapping of relationship identifiers to their descriptions, including the relationship
|
|
231
|
+
* type and the property name on the parent entity.
|
|
232
|
+
*/
|
|
233
|
+
setIncludedRelationshipDefaults(parentEntity, relationsLookup) {
|
|
234
|
+
Object.values(relationsLookup).forEach(rel => {
|
|
235
|
+
if (!(0, utils_1.isKeyOfEntity)(parentEntity, rel.propertyName))
|
|
236
|
+
return;
|
|
237
|
+
if ((0, utils_3.isHasManyRelationship)(rel) ||
|
|
238
|
+
(0, utils_3.isHasAndBelongsToManyRelationship)(rel)) {
|
|
239
|
+
(0, utils_1.safeAssign)(parentEntity, rel.propertyName, []);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
exports.default = FindById;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/operations/FindById/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,YAAY,CAAC;AACjD,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.FindById = void 0;
|
|
21
|
+
var FindById_1 = require("./FindById");
|
|
22
|
+
Object.defineProperty(exports, "FindById", { enumerable: true, get: function () { return __importDefault(FindById_1).default; } });
|
|
23
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { QueryItems } from "../../dynamo-utils";
|
|
2
|
+
import type DynaRecord from "../../DynaRecord";
|
|
3
|
+
import type { EntityAttributes, RelationshipAttributeNames } from "../types";
|
|
4
|
+
import type { BelongsToLinkDynamoItem } from "../../types";
|
|
5
|
+
/**
|
|
6
|
+
* Defines options for the `FindById` operation, allowing specification of additional associations to include in the query result.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
9
|
+
*
|
|
10
|
+
* @property {Array<{ association: RelationshipAttributeNames<T> }>} [include] - An array of association names to be included in the result of the query. Each association name must be a valid relationship attribute name for the entity `T`.
|
|
11
|
+
*/
|
|
12
|
+
export interface FindByIdOptions<T extends DynaRecord> {
|
|
13
|
+
include?: Array<{
|
|
14
|
+
association: RelationshipAttributeNames<T>;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Represents a list of associations to be included in the query result, derived from the `FindByIdOptions`.
|
|
19
|
+
*
|
|
20
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
21
|
+
*/
|
|
22
|
+
export type IncludedAssociations<T extends DynaRecord> = NonNullable<FindByIdOptions<T>["include"]>;
|
|
23
|
+
/**
|
|
24
|
+
* Describes the structure of query results, sorting them into the main entity item and any associated `BelongsToLink` items. Used during processing
|
|
25
|
+
*
|
|
26
|
+
* @property {QueryItems[number]} item - The main entity item retrieved from the query.
|
|
27
|
+
* @property {BelongsToLinkDynamoItem[]} belongsToLinks - An array of `BelongsToLinkDynamoItem` instances associated with the main entity item.
|
|
28
|
+
*/
|
|
29
|
+
export interface SortedQueryResults {
|
|
30
|
+
item: QueryItems[number];
|
|
31
|
+
belongsToLinks: BelongsToLinkDynamoItem[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Derives the keys of included associations from `FindByIdOptions`, representing the names of relationships specified to be included in the query result.
|
|
35
|
+
*
|
|
36
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
37
|
+
* @template Opts - The options for the `FindById` operation.
|
|
38
|
+
*/
|
|
39
|
+
type IncludedKeys<T extends DynaRecord, Opts extends FindByIdOptions<T>> = Opts extends Required<FindByIdOptions<T>> ? [...NonNullable<Opts>["include"]][number]["association"] : never;
|
|
40
|
+
/**
|
|
41
|
+
* Describes the entity attributes, extending them with any specified included associations for comprehensive query results.
|
|
42
|
+
*
|
|
43
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
44
|
+
* @template P - The properties of the entity `T`.
|
|
45
|
+
*/
|
|
46
|
+
type EntityKeysWithIncludedAssociations<T extends DynaRecord, P extends keyof T> = {
|
|
47
|
+
[K in P]: T[K] extends DynaRecord ? EntityAttributes<T> : T[K] extends DynaRecord[] ? Array<EntityAttributes<T>> : T[K];
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Represents the result of a `FindById` operation, including the main entity and any specified associated entities.
|
|
51
|
+
*
|
|
52
|
+
* @template T - The type of the main entity, extending `DynaRecord`.
|
|
53
|
+
* @template Opts - The options for the `FindById` operation, specifying included associations.
|
|
54
|
+
*/
|
|
55
|
+
export type FindByIdIncludesRes<T extends DynaRecord, Opts extends FindByIdOptions<T>> = EntityKeysWithIncludedAssociations<T, keyof EntityAttributes<T> | IncludedKeys<T, Opts>>;
|
|
56
|
+
export {};
|
|
57
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/FindById/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE3D;;;;;;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,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;IACzB,cAAc,EAAE,uBAAuB,EAAE,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,gBAAgB,CAAC,CAAC,CAAC,GACnB,CAAC,CAAC,CAAC,CAAC,SAAS,UAAU,EAAE,GACvB,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,GAC1B,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,gBAAgB,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,EAAE,IAAI,CAAC,CAClD,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type DynaRecord from "../DynaRecord";
|
|
2
|
+
import { type EntityMetadata, type TableMetadata } from "../metadata";
|
|
3
|
+
import type { EntityClass } from "../types";
|
|
4
|
+
/**
|
|
5
|
+
* Serves as an abstract base class for various operation classes within the ORM system that perform actions on entities, such as creating, updating, querying, or deleting. This class provides shared properties and functionality that are common across different types of operations, centralizing the handling of entity and table metadata.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type of the entity that the operation will be performed on, extending `DynaRecord`.
|
|
8
|
+
*
|
|
9
|
+
* @method run
|
|
10
|
+
* An abstract method that must be implemented by subclasses to execute the specific operation. The parameters and return type of this method can vary depending on the operation being performed.
|
|
11
|
+
*/
|
|
12
|
+
declare abstract class OperationBase<T extends DynaRecord> {
|
|
13
|
+
/**
|
|
14
|
+
* The class of the entity on which the operation is performed, providing static methods and properties related to the entity.
|
|
15
|
+
*/
|
|
16
|
+
protected readonly EntityClass: EntityClass<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Metadata for the entity, including information about its attributes and relationships.
|
|
19
|
+
*/
|
|
20
|
+
protected readonly entityMetadata: EntityMetadata;
|
|
21
|
+
/**
|
|
22
|
+
* Metadata for the table associated with the entity, including details about the table's structure and how the entity's attributes map to table columns.
|
|
23
|
+
*/
|
|
24
|
+
protected readonly tableMetadata: TableMetadata;
|
|
25
|
+
/**
|
|
26
|
+
* The alias used for the entity's partition key attribute in the database table, derived from the table metadata.
|
|
27
|
+
*/
|
|
28
|
+
protected readonly partitionKeyAlias: string;
|
|
29
|
+
/**
|
|
30
|
+
* The alias used for the entity's sort key attribute in the database table, if applicable, derived from the table metadata.
|
|
31
|
+
*/
|
|
32
|
+
protected readonly sortKeyAlias: string;
|
|
33
|
+
constructor(Entity: EntityClass<T>);
|
|
34
|
+
abstract run(...params: unknown[]): unknown;
|
|
35
|
+
}
|
|
36
|
+
export default OperationBase;
|
|
37
|
+
//# sourceMappingURL=OperationBase.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OperationBase.d.ts","sourceRoot":"","sources":["../../../src/operations/OperationBase.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,eAAe,CAAC;AAC5C,OAAiB,EAAE,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C;;;;;;;GAOG;AACH,uBAAe,aAAa,CAAC,CAAC,SAAS,UAAU;IAC/C;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAE/C;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,cAAc,CAAC;IAElD;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;IAEhD;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC;IAE7C;;OAEG;IACH,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;gBAE5B,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;aAQlB,GAAG,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO;CACnD;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
/**
|
|
8
|
+
* Serves as an abstract base class for various operation classes within the ORM system that perform actions on entities, such as creating, updating, querying, or deleting. This class provides shared properties and functionality that are common across different types of operations, centralizing the handling of entity and table metadata.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of the entity that the operation will be performed on, extending `DynaRecord`.
|
|
11
|
+
*
|
|
12
|
+
* @method run
|
|
13
|
+
* An abstract method that must be implemented by subclasses to execute the specific operation. The parameters and return type of this method can vary depending on the operation being performed.
|
|
14
|
+
*/
|
|
15
|
+
class OperationBase {
|
|
16
|
+
/**
|
|
17
|
+
* The class of the entity on which the operation is performed, providing static methods and properties related to the entity.
|
|
18
|
+
*/
|
|
19
|
+
EntityClass;
|
|
20
|
+
/**
|
|
21
|
+
* Metadata for the entity, including information about its attributes and relationships.
|
|
22
|
+
*/
|
|
23
|
+
entityMetadata;
|
|
24
|
+
/**
|
|
25
|
+
* Metadata for the table associated with the entity, including details about the table's structure and how the entity's attributes map to table columns.
|
|
26
|
+
*/
|
|
27
|
+
tableMetadata;
|
|
28
|
+
/**
|
|
29
|
+
* The alias used for the entity's partition key attribute in the database table, derived from the table metadata.
|
|
30
|
+
*/
|
|
31
|
+
partitionKeyAlias;
|
|
32
|
+
/**
|
|
33
|
+
* The alias used for the entity's sort key attribute in the database table, if applicable, derived from the table metadata.
|
|
34
|
+
*/
|
|
35
|
+
sortKeyAlias;
|
|
36
|
+
constructor(Entity) {
|
|
37
|
+
this.EntityClass = Entity;
|
|
38
|
+
this.entityMetadata = metadata_1.default.getEntity(Entity.name);
|
|
39
|
+
this.tableMetadata = metadata_1.default.getTable(this.entityMetadata.tableClassName);
|
|
40
|
+
this.partitionKeyAlias = this.tableMetadata.partitionKeyAttribute.alias;
|
|
41
|
+
this.sortKeyAlias = this.tableMetadata.sortKeyAttribute.alias;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.default = OperationBase;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type DynaRecord from "../../DynaRecord";
|
|
2
|
+
import { type QueryOptions as QueryBuilderOptions } from "../../query-utils";
|
|
3
|
+
import OperationBase from "../OperationBase";
|
|
4
|
+
import type { EntityKeyConditions, QueryOptions, QueryResults } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* Provides functionality to query entities from the database based on partition key, sort key, and optional filter conditions.
|
|
7
|
+
*
|
|
8
|
+
* The `Query` operation supports two main query patterns: querying by a simple entity ID or by more complex key conditions that include partition key, optional sort key, and filters. It can handle querying for both entities and their relationships, like "BelongsTo" links, based on the provided conditions.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
11
|
+
*/
|
|
12
|
+
declare class Query<T extends DynaRecord> extends OperationBase<T> {
|
|
13
|
+
/**
|
|
14
|
+
* Run the query operation
|
|
15
|
+
* @param key EntityId or object with PartitionKey and optional SortKey conditions
|
|
16
|
+
* @param options Filter conditions, indexName, or SortKey conditions if querying by keys
|
|
17
|
+
* @returns Array of Entity or BelongsToLinks
|
|
18
|
+
*/
|
|
19
|
+
run(key: string | EntityKeyConditions<T>, options?: QueryBuilderOptions | Omit<QueryOptions, "indexName">): Promise<QueryResults<T>>;
|
|
20
|
+
/**
|
|
21
|
+
* Query by PartitionKey and optional SortKey/Filter/Index conditions
|
|
22
|
+
* @param {Object} key - PartitionKey value and optional SortKey condition. Keys must be attributes defined on the model
|
|
23
|
+
* @param {Object=} options - QueryBuilderOptions. Supports filter and indexName
|
|
24
|
+
* @param {Object=} options.filter - Filter conditions object. Keys must be attributes defined on the model. Value can be exact value for Equality. Array for "IN" or $beginsWith
|
|
25
|
+
* @param {string=} options.indexName - The name of the index to filter on
|
|
26
|
+
*/
|
|
27
|
+
private queryByKey;
|
|
28
|
+
/**
|
|
29
|
+
* Query an EntityPartition by EntityId and optional SortKey/Filter conditions.
|
|
30
|
+
* QueryByIndex not supported. Use Query with keys if indexName is needed
|
|
31
|
+
* @param {string} id - Entity Id
|
|
32
|
+
* @param {Object=} options - QueryOptions. Supports filter and skCondition
|
|
33
|
+
* @param {Object=} options.skCondition - Sort Key condition. Can be an exact value or { $beginsWith: "val" }
|
|
34
|
+
* @param {Object=} options.filter - Filter conditions object. Keys must be attributes defined on the model. Value can be exact value for Equality. Array for "IN" or $beginsWith
|
|
35
|
+
*/
|
|
36
|
+
private queryEntity;
|
|
37
|
+
/**
|
|
38
|
+
* Resolve dynamo query results to the entity class
|
|
39
|
+
* @param queryResults
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
private resolveQueryResults;
|
|
43
|
+
}
|
|
44
|
+
export default Query;
|
|
45
|
+
//# sourceMappingURL=Query.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Query.d.ts","sourceRoot":"","sources":["../../../../src/operations/Query/Query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAEL,KAAK,YAAY,IAAI,mBAAmB,EACzC,MAAM,mBAAmB,CAAC;AAQ3B,OAAO,aAAa,MAAM,kBAAkB,CAAC;AAC7C,OAAO,KAAK,EAAE,mBAAmB,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE/E;;;;;;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;CAS5B;AAED,eAAe,KAAK,CAAC"}
|
|
@@ -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 query_utils_1 = require("../../query-utils");
|
|
7
|
+
const DynamoClient_1 = __importDefault(require("../../dynamo-utils/DynamoClient"));
|
|
8
|
+
const utils_1 = require("../../utils");
|
|
9
|
+
const OperationBase_1 = __importDefault(require("../OperationBase"));
|
|
10
|
+
/**
|
|
11
|
+
* Provides functionality to query entities from the database based on partition key, sort key, and optional filter conditions.
|
|
12
|
+
*
|
|
13
|
+
* The `Query` operation supports two main query patterns: querying by a simple entity ID or by more complex key conditions that include partition key, optional sort key, and filters. It can handle querying for both entities and their relationships, like "BelongsTo" links, based on the provided conditions.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
16
|
+
*/
|
|
17
|
+
class Query extends OperationBase_1.default {
|
|
18
|
+
/**
|
|
19
|
+
* Run the query operation
|
|
20
|
+
* @param key EntityId or object with PartitionKey and optional SortKey conditions
|
|
21
|
+
* @param options Filter conditions, indexName, or SortKey conditions if querying by keys
|
|
22
|
+
* @returns Array of Entity or BelongsToLinks
|
|
23
|
+
*/
|
|
24
|
+
async run(key, options) {
|
|
25
|
+
if (typeof key === "string") {
|
|
26
|
+
return await this.queryEntity(key, options);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
return await this.queryByKey(key, options);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Query by PartitionKey and optional SortKey/Filter/Index conditions
|
|
34
|
+
* @param {Object} key - PartitionKey value and optional SortKey condition. Keys must be attributes defined on the model
|
|
35
|
+
* @param {Object=} options - QueryBuilderOptions. Supports filter and indexName
|
|
36
|
+
* @param {Object=} options.filter - Filter conditions object. Keys must be attributes defined on the model. Value can be exact value for Equality. Array for "IN" or $beginsWith
|
|
37
|
+
* @param {string=} options.indexName - The name of the index to filter on
|
|
38
|
+
*/
|
|
39
|
+
async queryByKey(key, options) {
|
|
40
|
+
const params = new query_utils_1.QueryBuilder({
|
|
41
|
+
entityClassName: this.EntityClass.name,
|
|
42
|
+
key,
|
|
43
|
+
options
|
|
44
|
+
}).build();
|
|
45
|
+
const queryResults = await DynamoClient_1.default.query(params);
|
|
46
|
+
return this.resolveQueryResults(queryResults);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Query an EntityPartition by EntityId and optional SortKey/Filter conditions.
|
|
50
|
+
* QueryByIndex not supported. Use Query with keys if indexName is needed
|
|
51
|
+
* @param {string} id - Entity Id
|
|
52
|
+
* @param {Object=} options - QueryOptions. Supports filter and skCondition
|
|
53
|
+
* @param {Object=} options.skCondition - Sort Key condition. Can be an exact value or { $beginsWith: "val" }
|
|
54
|
+
* @param {Object=} options.filter - Filter conditions object. Keys must be attributes defined on the model. Value can be exact value for Equality. Array for "IN" or $beginsWith
|
|
55
|
+
*/
|
|
56
|
+
async queryEntity(id, options) {
|
|
57
|
+
const modelPk = this.tableMetadata.partitionKeyAttribute.name;
|
|
58
|
+
const modelSk = this.tableMetadata.sortKeyAttribute.name;
|
|
59
|
+
const keyCondition = {
|
|
60
|
+
[modelPk]: this.EntityClass.partitionKeyValue(id),
|
|
61
|
+
...(options?.skCondition !== undefined && {
|
|
62
|
+
[modelSk]: options?.skCondition
|
|
63
|
+
})
|
|
64
|
+
};
|
|
65
|
+
const params = new query_utils_1.QueryBuilder({
|
|
66
|
+
entityClassName: this.EntityClass.name,
|
|
67
|
+
key: keyCondition,
|
|
68
|
+
options
|
|
69
|
+
}).build();
|
|
70
|
+
const queryResults = await DynamoClient_1.default.query(params);
|
|
71
|
+
return this.resolveQueryResults(queryResults);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Resolve dynamo query results to the entity class
|
|
75
|
+
* @param queryResults
|
|
76
|
+
* @returns
|
|
77
|
+
*/
|
|
78
|
+
resolveQueryResults(queryResults) {
|
|
79
|
+
return queryResults.map(res => (0, utils_1.isBelongsToLinkDynamoItem)(res, this.tableMetadata)
|
|
80
|
+
? (0, utils_1.tableItemToBelongsToLink)(this.tableMetadata, res)
|
|
81
|
+
: (0, utils_1.tableItemToEntity)(this.EntityClass, res));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.default = Query;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/operations/Query/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,SAAS,CAAC;AAC3C,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.Query = void 0;
|
|
21
|
+
var Query_1 = require("./Query");
|
|
22
|
+
Object.defineProperty(exports, "Query", { enumerable: true, get: function () { return __importDefault(Query_1).default; } });
|
|
23
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type DynaRecord from "../../DynaRecord";
|
|
2
|
+
import type { KeyConditions, QueryOptions as QueryBuilderOptions, SortKeyCondition } from "../../query-utils";
|
|
3
|
+
import type { BelongsToLink } from "../../relationships";
|
|
4
|
+
import type { EntityAttributes } from "../types";
|
|
5
|
+
/**
|
|
6
|
+
* Extends the basic query builder options by adding an optional sort key condition for more precise querying capabilities.
|
|
7
|
+
*
|
|
8
|
+
* @extends QueryBuilderOptions - Base query options provided by the query utilities.
|
|
9
|
+
* @property {SortKeyCondition?} skCondition - An optional condition for the sort key to further refine the query. This can be an exact match condition or a condition specifying a range or beginning match for the sort key.
|
|
10
|
+
*/
|
|
11
|
+
export interface QueryOptions extends QueryBuilderOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Condition to query sort key by
|
|
14
|
+
*/
|
|
15
|
+
skCondition?: SortKeyCondition;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Defines partition key conditions for querying entities based on their keys. This type is used to specify the conditions under which an entity or a set of entities can be queried from the database.
|
|
19
|
+
*
|
|
20
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
21
|
+
* @property {KeyConditions} - Conditions applied to entity keys. Each key in the entity can have conditions such as equality, range conditions, or begins with conditions.
|
|
22
|
+
*/
|
|
23
|
+
export type EntityKeyConditions<T> = {
|
|
24
|
+
[K in keyof T]?: KeyConditions;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Represents the results of a query operation, which can include both entity attributes and associated `BelongsToLink` objects.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
30
|
+
*/
|
|
31
|
+
export type QueryResults<T extends DynaRecord> = Array<EntityAttributes<T> | BelongsToLink>;
|
|
32
|
+
/**
|
|
33
|
+
* A utility type that represents a single item in the query results, which can be either an entity or a `BelongsToLink`. This type is derived from the `QueryResults` array type, providing a convenient way to refer to individual results from a query.
|
|
34
|
+
*
|
|
35
|
+
* @template T - The type of the entity being queried, extending `DynaRecord`.
|
|
36
|
+
*/
|
|
37
|
+
export type QueryResult<T extends DynaRecord> = QueryResults<T>[number];
|
|
38
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +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,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAEjD;;;;;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;;;;GAIG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,UAAU,IAAI,KAAK,CACpD,gBAAgB,CAAC,CAAC,CAAC,GAAG,aAAa,CACpC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,UAAU,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC"}
|