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,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.expressionBuilder = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Builds a dynamo expression given the table attributes
|
|
6
|
+
* @param tableAttrs The table aliases of the entity attributes
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
const expressionBuilder = (tableAttrs) => {
|
|
10
|
+
const sorted = sortAttributesByOperand(tableAttrs);
|
|
11
|
+
const setExpression = buildUpdateSetExpression(sorted.set);
|
|
12
|
+
const removeExpression = buildUpdateRemoveExpression(sorted.remove);
|
|
13
|
+
const hasSetOperation = Object.keys(setExpression.ExpressionAttributeValues).length > 0;
|
|
14
|
+
return {
|
|
15
|
+
// If the operation has only REMOVE actions, it will not have expression attribute values
|
|
16
|
+
ExpressionAttributeValues: hasSetOperation
|
|
17
|
+
? setExpression.ExpressionAttributeValues
|
|
18
|
+
: undefined,
|
|
19
|
+
ExpressionAttributeNames: {
|
|
20
|
+
...setExpression.ExpressionAttributeNames,
|
|
21
|
+
...removeExpression.ExpressionAttributeNames
|
|
22
|
+
},
|
|
23
|
+
UpdateExpression: [
|
|
24
|
+
setExpression.UpdateExpression,
|
|
25
|
+
removeExpression.UpdateExpression
|
|
26
|
+
]
|
|
27
|
+
.filter(expr => expr)
|
|
28
|
+
.join(" ")
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
exports.expressionBuilder = expressionBuilder;
|
|
32
|
+
/**
|
|
33
|
+
* Sort attributes based on their operand
|
|
34
|
+
* @param tableAttrs
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
const sortAttributesByOperand = (tableAttrs) => {
|
|
38
|
+
return Object.entries(tableAttrs).reduce((acc, [key, value]) => {
|
|
39
|
+
const isRemovingAttr = value === null;
|
|
40
|
+
if (isRemovingAttr) {
|
|
41
|
+
acc.remove[key] = value;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
acc.set[key] = value;
|
|
45
|
+
}
|
|
46
|
+
return acc;
|
|
47
|
+
}, {
|
|
48
|
+
remove: {},
|
|
49
|
+
set: {}
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Build the update [SET](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET) expression
|
|
54
|
+
* @param tableAttrs
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
const buildUpdateSetExpression = (tableAttrs) => {
|
|
58
|
+
const entries = Object.entries(tableAttrs);
|
|
59
|
+
const action = "SET";
|
|
60
|
+
return entries.reduce((acc, [key, val], idx) => {
|
|
61
|
+
const attrName = `#${key}`;
|
|
62
|
+
const attrVal = `:${key}`;
|
|
63
|
+
const expression = ` ${attrName} = ${attrVal},`;
|
|
64
|
+
acc.ExpressionAttributeNames[attrName] = key;
|
|
65
|
+
acc.ExpressionAttributeValues[attrVal] = val;
|
|
66
|
+
acc.UpdateExpression = acc.UpdateExpression.concat(expression);
|
|
67
|
+
// For the first element, add the operand
|
|
68
|
+
if (idx === 0)
|
|
69
|
+
acc.UpdateExpression = `${action}${acc.UpdateExpression}`;
|
|
70
|
+
if (idx === entries.length - 1) {
|
|
71
|
+
// Remove trailing comma from the expression
|
|
72
|
+
acc.UpdateExpression = acc.UpdateExpression.slice(0, -1);
|
|
73
|
+
}
|
|
74
|
+
return acc;
|
|
75
|
+
}, {
|
|
76
|
+
ExpressionAttributeNames: {},
|
|
77
|
+
ExpressionAttributeValues: {},
|
|
78
|
+
UpdateExpression: ""
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Build the update [REMOVE](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.REMOVE) expression for removing attributes
|
|
83
|
+
* @param tableAttrs
|
|
84
|
+
* @returns
|
|
85
|
+
*/
|
|
86
|
+
const buildUpdateRemoveExpression = (tableAttrs) => {
|
|
87
|
+
const entries = Object.entries(tableAttrs);
|
|
88
|
+
const action = "REMOVE";
|
|
89
|
+
return entries.reduce((acc, [key], idx) => {
|
|
90
|
+
const attrName = `#${key}`;
|
|
91
|
+
const expression = ` ${attrName},`;
|
|
92
|
+
acc.ExpressionAttributeNames[attrName] = key;
|
|
93
|
+
acc.UpdateExpression = acc.UpdateExpression.concat(expression);
|
|
94
|
+
// For the first element, add the operand
|
|
95
|
+
if (idx === 0)
|
|
96
|
+
acc.UpdateExpression = `${action}${acc.UpdateExpression}`;
|
|
97
|
+
if (idx === entries.length - 1) {
|
|
98
|
+
// Remove trailing comma from the expression
|
|
99
|
+
acc.UpdateExpression = acc.UpdateExpression.slice(0, -1);
|
|
100
|
+
}
|
|
101
|
+
return acc;
|
|
102
|
+
}, {
|
|
103
|
+
ExpressionAttributeNames: {},
|
|
104
|
+
UpdateExpression: ""
|
|
105
|
+
});
|
|
106
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/operations/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACjF,cAAc,qBAAqB,CAAC;AACpC,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
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.RelationshipTransactions = void 0;
|
|
21
|
+
var RelationshipTransactions_1 = require("./RelationshipTransactions");
|
|
22
|
+
Object.defineProperty(exports, "RelationshipTransactions", { enumerable: true, get: function () { return __importDefault(RelationshipTransactions_1).default; } });
|
|
23
|
+
__exportStar(require("./expressionBuilder"), exports);
|
|
24
|
+
__exportStar(require("./types"), exports);
|
|
25
|
+
__exportStar(require("./utils"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type UpdateCommandInput } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
/**
|
|
3
|
+
* Defines the structure for an update expression using the SET action in DynamoDB, which is used to add new attributes to an item or modify existing attributes. This interface encapsulates the necessary components of an update expression for the [SET](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET) action.
|
|
4
|
+
*/
|
|
5
|
+
export interface UpdateSetExpression {
|
|
6
|
+
UpdateExpression: NonNullable<UpdateCommandInput["UpdateExpression"]>;
|
|
7
|
+
ExpressionAttributeNames: NonNullable<UpdateCommandInput["ExpressionAttributeNames"]>;
|
|
8
|
+
ExpressionAttributeValues: NonNullable<UpdateCommandInput["ExpressionAttributeValues"]>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Defines the structure for an update expression using the REMOVE action in DynamoDB, which is used to delete attributes from an item. This interface encapsulates the necessary components of an update expression for the [REMOVE](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.REMOVE) action, including attribute names and the update expression itself.
|
|
12
|
+
*/
|
|
13
|
+
export interface UpdateRemoveExpression {
|
|
14
|
+
UpdateExpression: NonNullable<UpdateCommandInput["UpdateExpression"]>;
|
|
15
|
+
ExpressionAttributeNames: NonNullable<UpdateCommandInput["ExpressionAttributeNames"]>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Represents either an update expression for setting new or modifying existing attributes of an item (UpdateSetExpression) or an update expression for removing attributes from an item (UpdateRemoveExpression) in DynamoDB.
|
|
19
|
+
*/
|
|
20
|
+
export type UpdateExpression = UpdateSetExpression | UpdateRemoveExpression;
|
|
21
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/operations/utils/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhE;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,gBAAgB,EAAE,WAAW,CAAC,kBAAkB,CAAC,kBAAkB,CAAC,CAAC,CAAC;IACtE,wBAAwB,EAAE,WAAW,CACnC,kBAAkB,CAAC,0BAA0B,CAAC,CAC/C,CAAC;IACF,yBAAyB,EAAE,WAAW,CACpC,kBAAkB,CAAC,2BAA2B,CAAC,CAChD,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,gBAAgB,EAAE,WAAW,CAAC,kBAAkB,CAAC,kBAAkB,CAAC,CAAC,CAAC;IACtE,wBAAwB,EAAE,WAAW,CACnC,kBAAkB,CAAC,0BAA0B,CAAC,CAC/C,CAAC;CACH;AAED;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,mBAAmB,GAAG,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type DynaRecord from "../../DynaRecord";
|
|
2
|
+
import type { RelationshipMetadata } from "../../metadata";
|
|
3
|
+
import type { ForeignKey, Optional, RelationshipMetaObj } from "../../types";
|
|
4
|
+
/**
|
|
5
|
+
* Creates an object including
|
|
6
|
+
* - relationsLookup: Object to look up RelationshipMetadata by Entity name
|
|
7
|
+
* - belongsToRelationships: An array of BelongsTo relationships
|
|
8
|
+
* @param includedRelationships
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export declare const buildEntityRelationshipMetaObj: (relationships: RelationshipMetadata[]) => RelationshipMetaObj;
|
|
12
|
+
/**
|
|
13
|
+
* Extracts a ForeignKey value from an entity instance
|
|
14
|
+
* @param relMeta - RelationshipMetadata for associated foreignKey
|
|
15
|
+
* @param entity - instance of DynaRecord
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export declare const extractForeignKeyFromEntity: <T extends DynaRecord>(relMeta: RelationshipMetadata, entity?: T | undefined) => Optional<ForeignKey>;
|
|
19
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/operations/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAK3D,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAG7E;;;;;;GAMG;AACH,eAAO,MAAM,8BAA8B,kBAC1B,oBAAoB,EAAE,KACpC,mBAaF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,2BAA2B,kCAC7B,oBAAoB,6BAE5B,SAAS,UAAU,CAMrB,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractForeignKeyFromEntity = exports.buildEntityRelationshipMetaObj = void 0;
|
|
4
|
+
const utils_1 = require("../../metadata/utils");
|
|
5
|
+
const utils_2 = require("../../utils");
|
|
6
|
+
/**
|
|
7
|
+
* Creates an object including
|
|
8
|
+
* - relationsLookup: Object to look up RelationshipMetadata by Entity name
|
|
9
|
+
* - belongsToRelationships: An array of BelongsTo relationships
|
|
10
|
+
* @param includedRelationships
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
const buildEntityRelationshipMetaObj = (relationships) => {
|
|
14
|
+
return Object.values(relationships).reduce((acc, rel) => {
|
|
15
|
+
if ((0, utils_1.isBelongsToRelationship)(rel)) {
|
|
16
|
+
acc.belongsToRelationships.push(rel);
|
|
17
|
+
}
|
|
18
|
+
acc.relationsLookup[rel.target.name] = rel;
|
|
19
|
+
return acc;
|
|
20
|
+
}, { relationsLookup: {}, belongsToRelationships: [] });
|
|
21
|
+
};
|
|
22
|
+
exports.buildEntityRelationshipMetaObj = buildEntityRelationshipMetaObj;
|
|
23
|
+
/**
|
|
24
|
+
* Extracts a ForeignKey value from an entity instance
|
|
25
|
+
* @param relMeta - RelationshipMetadata for associated foreignKey
|
|
26
|
+
* @param entity - instance of DynaRecord
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
const extractForeignKeyFromEntity = (relMeta, entity) => {
|
|
30
|
+
return entity !== undefined &&
|
|
31
|
+
(0, utils_1.isRelationshipMetadataWithForeignKey)(relMeta) &&
|
|
32
|
+
(0, utils_2.isKeyOfObject)(entity, relMeta.foreignKey)
|
|
33
|
+
? entity[relMeta.foreignKey]
|
|
34
|
+
: undefined;
|
|
35
|
+
};
|
|
36
|
+
exports.extractForeignKeyFromEntity = extractForeignKeyFromEntity;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type RelationshipMetadata } from "../metadata";
|
|
2
|
+
import { type OrFilter } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Builds the filter so that relationships that are included in the query will have their {@link BelongsToLink} included in the query
|
|
5
|
+
* @param parentClassName
|
|
6
|
+
* @param includedRelationships
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare function includedRelationshipsFilter(parentClassName: string, includedRelationships: RelationshipMetadata[]): OrFilter;
|
|
10
|
+
//# sourceMappingURL=Filters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Filters.d.ts","sourceRoot":"","sources":["../../../src/query-utils/Filters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,SAAS,CAAC;AAWxC;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,eAAe,EAAE,MAAM,EACvB,qBAAqB,EAAE,oBAAoB,EAAE,GAC5C,QAAQ,CAmBV"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.includedRelationshipsFilter = void 0;
|
|
4
|
+
const relationships_1 = require("../relationships");
|
|
5
|
+
/**
|
|
6
|
+
* Builds the filter so that relationships that are included in the query will have their {@link BelongsToLink} included in the query
|
|
7
|
+
* @param parentClassName
|
|
8
|
+
* @param includedRelationships
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
function includedRelationshipsFilter(parentClassName, includedRelationships) {
|
|
12
|
+
const filters = [{ type: parentClassName }];
|
|
13
|
+
const includeBelongsToLinks = includedRelationships.some(rel => rel.type === "HasMany" ||
|
|
14
|
+
rel.type === "HasOne" ||
|
|
15
|
+
rel.type === "HasAndBelongsToMany");
|
|
16
|
+
if (includeBelongsToLinks) {
|
|
17
|
+
const belongsToFilter = {
|
|
18
|
+
type: relationships_1.BelongsToLink.name,
|
|
19
|
+
foreignEntityType: includedRelationships.map(rel => rel.target.name)
|
|
20
|
+
};
|
|
21
|
+
filters.push(belongsToFilter);
|
|
22
|
+
}
|
|
23
|
+
return { $or: filters };
|
|
24
|
+
}
|
|
25
|
+
exports.includedRelationshipsFilter = includedRelationshipsFilter;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type QueryCommandInput } from "@aws-sdk/lib-dynamodb";
|
|
2
|
+
import type { QueryCommandProps } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Constructs and formats a DynamoDB query command based on provided key conditions and query options. This class simplifies the creation of complex DynamoDB queries by abstracting the underlying AWS SDK query command structure, particularly handling the construction of key condition expressions, filter expressions, and expression attribute names and values.
|
|
5
|
+
*
|
|
6
|
+
* Utilizing metadata about the entity and its attributes, `QueryBuilder` generates the necessary DynamoDB expressions to perform precise queries, including support for conditional operators like '=', 'begins_with', and 'IN', as well as logical 'AND' and 'OR' operations.
|
|
7
|
+
*/
|
|
8
|
+
declare class QueryBuilder {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(props: QueryCommandProps);
|
|
11
|
+
/**
|
|
12
|
+
* Builds and returns the `QueryCommandInput` for a DynamoDB query operation.
|
|
13
|
+
* @returns {QueryCommandInput} The configured query command input for AWS SDK.
|
|
14
|
+
*/
|
|
15
|
+
build(): QueryCommandInput;
|
|
16
|
+
/**
|
|
17
|
+
* Build ExpressionAttributeValues
|
|
18
|
+
* @param keyParams
|
|
19
|
+
* @param filterParams
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
private expressionAttributeValueParams;
|
|
23
|
+
/**
|
|
24
|
+
* Build ExpressionAttributeNames
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
private expressionAttributeNames;
|
|
28
|
+
/**
|
|
29
|
+
* Creates the filters
|
|
30
|
+
*
|
|
31
|
+
* Supports 'AND' and 'OR'
|
|
32
|
+
* Currently only works for '=', 'begins_with' or 'IN' operands
|
|
33
|
+
* Does not support operations like 'contains' etc yet
|
|
34
|
+
*
|
|
35
|
+
* @param filter
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
private filterParams;
|
|
39
|
+
/**
|
|
40
|
+
* Creates an AND OR filter
|
|
41
|
+
* @param filter
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
private andOrFilter;
|
|
45
|
+
/**
|
|
46
|
+
* Creates an AND filter
|
|
47
|
+
* @param filter
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
private andFilter;
|
|
51
|
+
/**
|
|
52
|
+
* Creates an AND condition
|
|
53
|
+
* @param attr
|
|
54
|
+
* @param value
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
private andCondition;
|
|
58
|
+
/**
|
|
59
|
+
* Builds an OR filter
|
|
60
|
+
* @param filter
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
63
|
+
private orFilter;
|
|
64
|
+
/**
|
|
65
|
+
* Builds an OR condition
|
|
66
|
+
* @param andFilter \
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
private orCondition;
|
|
70
|
+
/**
|
|
71
|
+
* Type guard to check if its a BeginsWithFilter
|
|
72
|
+
* @param filter
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
private isBeginsWithFilter;
|
|
76
|
+
/**
|
|
77
|
+
* Type guard to check if its a AndOrFilter
|
|
78
|
+
* @param filter
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
private isAndOrFilter;
|
|
82
|
+
/**
|
|
83
|
+
* Type guard to check if its a OrFilter
|
|
84
|
+
* @param filter
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
private isOrFilter;
|
|
88
|
+
}
|
|
89
|
+
export default QueryBuilder;
|
|
90
|
+
//# sourceMappingURL=QueryBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryBuilder.d.ts","sourceRoot":"","sources":["../../../src/query-utils/QueryBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAO/D,OAAO,KAAK,EASV,iBAAiB,EAClB,MAAM,SAAS,CAAC;AAEjB;;;;GAIG;AACH,cAAM,YAAY;;gBAMJ,KAAK,EAAE,iBAAiB;IAWpC;;;OAGG;IACI,KAAK,IAAI,iBAAiB;IAuBjC;;;;;OAKG;IACH,OAAO,CAAC,8BAA8B;IAetC;;;OAGG;IACH,OAAO,CAAC,wBAAwB;IAgChC;;;;;;;;;OASG;IACH,OAAO,CAAC,YAAY;IAWpB;;;;OAIG;IACH,OAAO,CAAC,WAAW;IASnB;;;;OAIG;IACH,OAAO,CAAC,SAAS;IAejB;;;;;OAKG;IACH,OAAO,CAAC,YAAY;IAyBpB;;;;OAIG;IACH,OAAO,CAAC,QAAQ;IAehB;;;;OAIG;IACH,OAAO,CAAC,WAAW;IAanB;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;IAI1B;;;;OAIG;IACH,OAAO,CAAC,aAAa;IAIrB;;;;OAIG;IACH,OAAO,CAAC,UAAU;CAGnB;AAED,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,213 @@
|
|
|
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
|
+
* Constructs and formats a DynamoDB query command based on provided key conditions and query options. This class simplifies the creation of complex DynamoDB queries by abstracting the underlying AWS SDK query command structure, particularly handling the construction of key condition expressions, filter expressions, and expression attribute names and values.
|
|
9
|
+
*
|
|
10
|
+
* Utilizing metadata about the entity and its attributes, `QueryBuilder` generates the necessary DynamoDB expressions to perform precise queries, including support for conditional operators like '=', 'begins_with', and 'IN', as well as logical 'AND' and 'OR' operations.
|
|
11
|
+
*/
|
|
12
|
+
class QueryBuilder {
|
|
13
|
+
#props;
|
|
14
|
+
#tableMetadata;
|
|
15
|
+
#attributeMetadata;
|
|
16
|
+
#attrCounter;
|
|
17
|
+
constructor(props) {
|
|
18
|
+
this.#props = props;
|
|
19
|
+
this.#attrCounter = 0;
|
|
20
|
+
const entityMetadata = metadata_1.default.getEntity(props.entityClassName);
|
|
21
|
+
this.#tableMetadata = metadata_1.default.getTable(entityMetadata.tableClassName);
|
|
22
|
+
this.#attributeMetadata = metadata_1.default.getEntityAttributes(props.entityClassName);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Builds and returns the `QueryCommandInput` for a DynamoDB query operation.
|
|
26
|
+
* @returns {QueryCommandInput} The configured query command input for AWS SDK.
|
|
27
|
+
*/
|
|
28
|
+
build() {
|
|
29
|
+
const { indexName, filter } = this.#props.options ?? {};
|
|
30
|
+
const filterParams = filter !== undefined ? this.filterParams(filter) : undefined;
|
|
31
|
+
const keyFilter = this.andFilter(this.#props.key);
|
|
32
|
+
const hasIndex = indexName !== undefined;
|
|
33
|
+
const hasFilter = filterParams !== undefined;
|
|
34
|
+
return {
|
|
35
|
+
TableName: this.#tableMetadata.name,
|
|
36
|
+
...(hasIndex && { IndexName: indexName }),
|
|
37
|
+
...(hasFilter && { FilterExpression: filterParams.expression }),
|
|
38
|
+
KeyConditionExpression: keyFilter.expression,
|
|
39
|
+
ExpressionAttributeNames: this.expressionAttributeNames(),
|
|
40
|
+
ExpressionAttributeValues: this.expressionAttributeValueParams(keyFilter, filterParams)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Build ExpressionAttributeValues
|
|
45
|
+
* @param keyParams
|
|
46
|
+
* @param filterParams
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
expressionAttributeValueParams(keyParams, filterParams) {
|
|
50
|
+
const hasFilter = this.#props.options?.filter !== undefined;
|
|
51
|
+
const valueParams = hasFilter
|
|
52
|
+
? { ...keyParams.values, ...filterParams?.values }
|
|
53
|
+
: keyParams.values;
|
|
54
|
+
return Object.entries(valueParams).reduce((params, [attrName, value]) => ({ ...params, [`:${attrName}`]: value }), {});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Build ExpressionAttributeNames
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
expressionAttributeNames() {
|
|
61
|
+
const { filter } = this.#props.options ?? {};
|
|
62
|
+
const accumulator = (obj, key) => {
|
|
63
|
+
const tableKey = this.#attributeMetadata[key].alias;
|
|
64
|
+
obj[`#${tableKey}`] = tableKey;
|
|
65
|
+
return obj;
|
|
66
|
+
};
|
|
67
|
+
let expressionAttributeNames = Object.keys(this.#props.key).reduce((acc, key) => accumulator(acc, key), {});
|
|
68
|
+
if (filter !== undefined) {
|
|
69
|
+
const { $or: orFilters = [], ...andFilters } = filter;
|
|
70
|
+
const or = orFilters.reduce((acc, filter) => {
|
|
71
|
+
Object.keys(filter).forEach(key => accumulator(acc, key));
|
|
72
|
+
return acc;
|
|
73
|
+
}, {});
|
|
74
|
+
const and = Object.keys(andFilters).reduce((acc, key) => accumulator(acc, key), {});
|
|
75
|
+
expressionAttributeNames = { ...expressionAttributeNames, ...or, ...and };
|
|
76
|
+
}
|
|
77
|
+
return expressionAttributeNames;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates the filters
|
|
81
|
+
*
|
|
82
|
+
* Supports 'AND' and 'OR'
|
|
83
|
+
* Currently only works for '=', 'begins_with' or 'IN' operands
|
|
84
|
+
* Does not support operations like 'contains' etc yet
|
|
85
|
+
*
|
|
86
|
+
* @param filter
|
|
87
|
+
* @returns
|
|
88
|
+
*/
|
|
89
|
+
filterParams(filter) {
|
|
90
|
+
const isOrFilter = this.isOrFilter(filter);
|
|
91
|
+
if (isOrFilter) {
|
|
92
|
+
const isAndOrFilter = this.isAndOrFilter(filter);
|
|
93
|
+
return isAndOrFilter ? this.andOrFilter(filter) : this.orFilter(filter);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
return this.andFilter(filter);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Creates an AND OR filter
|
|
101
|
+
* @param filter
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
andOrFilter(filter) {
|
|
105
|
+
const { $or: _orFilters, ...andFilters } = filter;
|
|
106
|
+
const orFilterParams = this.orFilter(filter);
|
|
107
|
+
const andFilterParams = this.andFilter(andFilters);
|
|
108
|
+
const expression = `(${orFilterParams.expression}) AND (${andFilterParams.expression})`;
|
|
109
|
+
const values = { ...orFilterParams.values, ...andFilterParams.values };
|
|
110
|
+
return { expression, values };
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Creates an AND filter
|
|
114
|
+
* @param filter
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
andFilter(filter) {
|
|
118
|
+
const params = Object.entries(filter).reduce((obj, [attr, value]) => {
|
|
119
|
+
const { expression, values } = this.andCondition(attr, value);
|
|
120
|
+
return {
|
|
121
|
+
expression: obj.expression.concat(expression),
|
|
122
|
+
values: { ...obj.values, ...values }
|
|
123
|
+
};
|
|
124
|
+
}, { expression: "", values: {} });
|
|
125
|
+
params.expression = params.expression.slice(0, -5); // trim off the trailing " AND "
|
|
126
|
+
return params;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Creates an AND condition
|
|
130
|
+
* @param attr
|
|
131
|
+
* @param value
|
|
132
|
+
* @returns
|
|
133
|
+
*/
|
|
134
|
+
andCondition(attr, value) {
|
|
135
|
+
const tableKey = this.#attributeMetadata[attr].alias;
|
|
136
|
+
let condition;
|
|
137
|
+
let values = {};
|
|
138
|
+
if (Array.isArray(value)) {
|
|
139
|
+
const mappings = value.reduce((acc, val) => {
|
|
140
|
+
const attr = `${tableKey}${++this.#attrCounter}`;
|
|
141
|
+
values[attr] = val;
|
|
142
|
+
return acc.concat(`:${attr}`);
|
|
143
|
+
}, []);
|
|
144
|
+
condition = `#${tableKey} IN (${mappings.join()})`;
|
|
145
|
+
}
|
|
146
|
+
else if (this.isBeginsWithFilter(value)) {
|
|
147
|
+
const attr = `${tableKey}${++this.#attrCounter}`;
|
|
148
|
+
condition = `begins_with(#${tableKey}, :${attr})`;
|
|
149
|
+
values = { [`${attr}`]: value.$beginsWith };
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const attr = `${tableKey}${++this.#attrCounter}`;
|
|
153
|
+
condition = `#${tableKey} = :${attr}`;
|
|
154
|
+
values = { [`${attr}`]: value };
|
|
155
|
+
}
|
|
156
|
+
return { expression: `${condition} AND `, values };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Builds an OR filter
|
|
160
|
+
* @param filter
|
|
161
|
+
* @returns
|
|
162
|
+
*/
|
|
163
|
+
orFilter(filter) {
|
|
164
|
+
const orFilter = filter.$or.reduce((filterParams, filter) => {
|
|
165
|
+
const { expression, values } = this.orCondition(filter);
|
|
166
|
+
return {
|
|
167
|
+
expression: filterParams.expression.concat(expression),
|
|
168
|
+
values: { ...filterParams.values, ...values }
|
|
169
|
+
};
|
|
170
|
+
}, { expression: "", values: {} });
|
|
171
|
+
orFilter.expression = orFilter.expression.slice(0, -4); // trim off the trailing " OR "
|
|
172
|
+
return orFilter;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Builds an OR condition
|
|
176
|
+
* @param andFilter \
|
|
177
|
+
* @returns
|
|
178
|
+
*/
|
|
179
|
+
orCondition(andFilter) {
|
|
180
|
+
const andParams = this.filterParams(andFilter);
|
|
181
|
+
const multipleVals = Object.keys(andParams.values).length > 1;
|
|
182
|
+
const expression = multipleVals
|
|
183
|
+
? `(${andParams.expression}) OR `
|
|
184
|
+
: `${andParams.expression} OR `;
|
|
185
|
+
const values = Object.entries(andParams.values).reduce((obj, [key, val]) => ({ ...obj, [key]: val }), {});
|
|
186
|
+
return { expression, values };
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Type guard to check if its a BeginsWithFilter
|
|
190
|
+
* @param filter
|
|
191
|
+
* @returns
|
|
192
|
+
*/
|
|
193
|
+
isBeginsWithFilter(filter) {
|
|
194
|
+
return filter.$beginsWith !== undefined;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Type guard to check if its a AndOrFilter
|
|
198
|
+
* @param filter
|
|
199
|
+
* @returns
|
|
200
|
+
*/
|
|
201
|
+
isAndOrFilter(filter) {
|
|
202
|
+
return this.isOrFilter(filter) && Object.keys(filter).length > 1;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Type guard to check if its a OrFilter
|
|
206
|
+
* @param filter
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
209
|
+
isOrFilter(filter) {
|
|
210
|
+
return filter.$or !== undefined;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
exports.default = QueryBuilder;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/query-utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACzD,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.QueryBuilder = void 0;
|
|
21
|
+
var QueryBuilder_1 = require("./QueryBuilder");
|
|
22
|
+
Object.defineProperty(exports, "QueryBuilder", { enumerable: true, get: function () { return __importDefault(QueryBuilder_1).default; } });
|
|
23
|
+
__exportStar(require("./types"), exports);
|