quidproquo-actionprocessor-awslambda 0.0.103 → 0.0.104
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/lib/getActionProcessor/core/keyValueStore/getKeyValueStoreDeleteActionProcessor.js +2 -2
- package/lib/getActionProcessor/core/keyValueStore/getKeyValueStoreQueryActionProcessor.js +2 -2
- package/lib/getActionProcessor/core/keyValueStore/getKeyValueStoreUpdateActionProcessor.js +2 -4
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/logic/dynamo/convertObjectToDynamoMap.d.ts +0 -1
- package/lib/logic/dynamo/convertObjectToDynamoMap.js +34 -36
- package/lib/logic/dynamo/deleteItem.d.ts +2 -1
- package/lib/logic/dynamo/deleteItem.js +6 -4
- package/lib/logic/dynamo/putItem.d.ts +0 -7
- package/lib/logic/dynamo/putItem.js +25 -33
- package/lib/logic/dynamo/qpqDynamoOrm/buildDynamoQuery.d.ts +4 -1
- package/lib/logic/dynamo/qpqDynamoOrm/buildDynamoQuery.js +37 -28
- package/lib/logic/dynamo/qpqDynamoOrm/buildDynamoUpdate.d.ts +11 -0
- package/lib/logic/dynamo/qpqDynamoOrm/buildDynamoUpdate.js +99 -0
- package/lib/logic/dynamo/qpqDynamoOrm/index.d.ts +2 -0
- package/lib/logic/dynamo/qpqDynamoOrm/index.js +2 -0
- package/lib/logic/dynamo/updateItem.d.ts +2 -3
- package/lib/logic/dynamo/updateItem.js +10 -31
- package/package.json +1 -1
- /package/lib/logic/dynamo/qpqDynamoOrm/{thing.d.ts → getDynamoTableIndexByConfigAndQuery.d.ts} +0 -0
- /package/lib/logic/dynamo/qpqDynamoOrm/{thing.js → getDynamoTableIndexByConfigAndQuery.js} +0 -0
|
@@ -14,10 +14,10 @@ const awsNamingUtils_1 = require("../../../awsNamingUtils");
|
|
|
14
14
|
const quidproquo_core_2 = require("quidproquo-core");
|
|
15
15
|
const dynamo_1 = require("../../../logic/dynamo");
|
|
16
16
|
const getProcessKeyValueStoreDelete = (qpqConfig) => {
|
|
17
|
-
return ({ keyValueStoreName, key }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
return ({ keyValueStoreName, key, sortKey }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
18
|
const dynamoTableName = (0, awsNamingUtils_1.getQpqRuntimeResourceNameFromConfig)(keyValueStoreName, qpqConfig, 'kvs');
|
|
19
19
|
const region = quidproquo_core_1.qpqCoreUtils.getApplicationModuleDeployRegion(qpqConfig);
|
|
20
|
-
yield (0, dynamo_1.deleteItem)(dynamoTableName, key,
|
|
20
|
+
yield (0, dynamo_1.deleteItem)(dynamoTableName, region, key, sortKey);
|
|
21
21
|
return (0, quidproquo_core_2.actionResult)(void 0);
|
|
22
22
|
});
|
|
23
23
|
};
|
|
@@ -13,7 +13,7 @@ const quidproquo_core_1 = require("quidproquo-core");
|
|
|
13
13
|
const awsNamingUtils_1 = require("../../../awsNamingUtils");
|
|
14
14
|
const quidproquo_core_2 = require("quidproquo-core");
|
|
15
15
|
const dynamo_1 = require("../../../logic/dynamo");
|
|
16
|
-
const
|
|
16
|
+
const qpqDynamoOrm_1 = require("../../../logic/dynamo/qpqDynamoOrm");
|
|
17
17
|
const getProcessKeyValueStoreQuery = (qpqConfig) => {
|
|
18
18
|
return ({ keyValueStoreName, keyCondition, filterCondition }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
19
|
const dynamoTableName = (0, awsNamingUtils_1.getQpqRuntimeResourceNameFromConfig)(keyValueStoreName, qpqConfig, 'kvs');
|
|
@@ -22,7 +22,7 @@ const getProcessKeyValueStoreQuery = (qpqConfig) => {
|
|
|
22
22
|
if (!storeConfig) {
|
|
23
23
|
return (0, quidproquo_core_1.actionResultError)(quidproquo_core_1.ErrorTypeEnum.NotFound, `Could not find key value store with name "${keyValueStoreName}"`);
|
|
24
24
|
}
|
|
25
|
-
const items = yield (0, dynamo_1.query)(dynamoTableName, region, keyCondition, filterCondition, (0,
|
|
25
|
+
const items = yield (0, dynamo_1.query)(dynamoTableName, region, keyCondition, filterCondition, (0, qpqDynamoOrm_1.getDynamoTableIndexByConfigAndQuery)(storeConfig, keyCondition));
|
|
26
26
|
return (0, quidproquo_core_2.actionResult)(items);
|
|
27
27
|
});
|
|
28
28
|
};
|
|
@@ -14,12 +14,10 @@ const awsNamingUtils_1 = require("../../../awsNamingUtils");
|
|
|
14
14
|
const quidproquo_core_2 = require("quidproquo-core");
|
|
15
15
|
const dynamo_1 = require("../../../logic/dynamo");
|
|
16
16
|
const getProcessKeyValueStoreUpdate = (qpqConfig) => {
|
|
17
|
-
return ({ keyValueStoreName, key,
|
|
17
|
+
return ({ keyValueStoreName, key, sortKey, updates, options }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
18
|
const dynamoTableName = (0, awsNamingUtils_1.getQpqRuntimeResourceNameFromConfig)(keyValueStoreName, qpqConfig, 'kvs');
|
|
19
19
|
const region = quidproquo_core_1.qpqCoreUtils.getApplicationModuleDeployRegion(qpqConfig);
|
|
20
|
-
yield (0, dynamo_1.updateItem)(dynamoTableName,
|
|
21
|
-
expires: options === null || options === void 0 ? void 0 : options.ttl,
|
|
22
|
-
}, region);
|
|
20
|
+
yield (0, dynamo_1.updateItem)(dynamoTableName, region, updates, key, sortKey);
|
|
23
21
|
return (0, quidproquo_core_2.actionResult)(void 0);
|
|
24
22
|
});
|
|
25
23
|
};
|
package/lib/index.d.ts
CHANGED
|
@@ -4,3 +4,5 @@ export * as awsNamingUtils from './awsNamingUtils';
|
|
|
4
4
|
export * from './runtimeConfig/QPQAWSResourceMap';
|
|
5
5
|
export * from './types';
|
|
6
6
|
export * from './logic/parametersManager/getParameter';
|
|
7
|
+
export * from './logic/parametersManager/getParameters';
|
|
8
|
+
export * from './logic/cache/memoFunc';
|
package/lib/index.js
CHANGED
|
@@ -33,3 +33,5 @@ exports.awsNamingUtils = __importStar(require("./awsNamingUtils"));
|
|
|
33
33
|
__exportStar(require("./runtimeConfig/QPQAWSResourceMap"), exports);
|
|
34
34
|
__exportStar(require("./types"), exports);
|
|
35
35
|
__exportStar(require("./logic/parametersManager/getParameter"), exports);
|
|
36
|
+
__exportStar(require("./logic/parametersManager/getParameters"), exports);
|
|
37
|
+
__exportStar(require("./logic/cache/memoFunc"), exports);
|
|
@@ -1,41 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// Deprecated: 2021-06-01
|
|
3
|
+
// export function convertObjectToDynamoMap(obj: any) {
|
|
4
|
+
// const map: any = {};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertDynamoMapToObject =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return map;
|
|
37
|
-
}
|
|
38
|
-
exports.convertObjectToDynamoMap = convertObjectToDynamoMap;
|
|
6
|
+
exports.convertDynamoMapToObject = void 0;
|
|
7
|
+
// for (const property in obj) {
|
|
8
|
+
// const value = obj[property];
|
|
9
|
+
// const valueType = typeof value;
|
|
10
|
+
// switch (valueType) {
|
|
11
|
+
// case 'string':
|
|
12
|
+
// map[property] = { S: value };
|
|
13
|
+
// break;
|
|
14
|
+
// case 'number':
|
|
15
|
+
// map[property] = { N: value.toString() };
|
|
16
|
+
// break;
|
|
17
|
+
// case 'boolean':
|
|
18
|
+
// map[property] = { BOOL: value };
|
|
19
|
+
// break;
|
|
20
|
+
// case 'object':
|
|
21
|
+
// if (Array.isArray(value)) {
|
|
22
|
+
// map[property] = {
|
|
23
|
+
// L: value.map((item) => convertObjectToDynamoMap({ temp: item }).temp),
|
|
24
|
+
// };
|
|
25
|
+
// } else if (value !== null) {
|
|
26
|
+
// map[property] = { M: convertObjectToDynamoMap(value) };
|
|
27
|
+
// } else {
|
|
28
|
+
// map[property] = { NULL: true };
|
|
29
|
+
// }
|
|
30
|
+
// break;
|
|
31
|
+
// default:
|
|
32
|
+
// throw new Error(`Unsupported data type: ${valueType}`);
|
|
33
|
+
// }
|
|
34
|
+
// }
|
|
35
|
+
// return map;
|
|
36
|
+
// }
|
|
39
37
|
function convertDynamoMapToObject(dynamoMap) {
|
|
40
38
|
const obj = {};
|
|
41
39
|
for (const property in dynamoMap) {
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { KvsCoreDataType } from 'quidproquo-core';
|
|
2
|
+
export declare function deleteItem(tableName: string, region: string, key: KvsCoreDataType, sortKey?: KvsCoreDataType): Promise<void>;
|
|
@@ -11,17 +11,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.deleteItem = void 0;
|
|
13
13
|
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
14
|
-
|
|
14
|
+
const qpqDynamoOrm_1 = require("./qpqDynamoOrm");
|
|
15
|
+
function deleteItem(tableName, region, key, sortKey) {
|
|
15
16
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
17
|
const dynamoClient = new client_dynamodb_1.DynamoDBClient({ region });
|
|
17
18
|
const deleteItemParams = {
|
|
18
19
|
TableName: tableName,
|
|
19
20
|
Key: {
|
|
20
|
-
key:
|
|
21
|
-
S: key,
|
|
22
|
-
},
|
|
21
|
+
key: (0, qpqDynamoOrm_1.buildAttributeValue)(key),
|
|
23
22
|
},
|
|
24
23
|
};
|
|
24
|
+
if (sortKey != undefined) {
|
|
25
|
+
deleteItemParams.Key.sortKey = (0, qpqDynamoOrm_1.buildAttributeValue)(sortKey);
|
|
26
|
+
}
|
|
25
27
|
yield dynamoClient.send(new client_dynamodb_1.DeleteItemCommand(deleteItemParams));
|
|
26
28
|
});
|
|
27
29
|
}
|
|
@@ -2,11 +2,4 @@ import { KvsKey } from 'quidproquo-core';
|
|
|
2
2
|
export interface PutItemOptions {
|
|
3
3
|
expires?: number;
|
|
4
4
|
}
|
|
5
|
-
export type KvsKeyValue<T> = {
|
|
6
|
-
key: KvsKey;
|
|
7
|
-
value: T;
|
|
8
|
-
};
|
|
9
|
-
export type DynamoAttributeType = 'S' | 'N' | 'B';
|
|
10
|
-
export declare const readKvsAttributeValue: <T, K extends keyof T>(item: T, key: KvsKey) => KvsKeyValue<T[K]>;
|
|
11
|
-
export declare const getDynamoValueTypeFromKvsKey: (key: KvsKey) => DynamoAttributeType;
|
|
12
5
|
export declare function putItem<Item>(tableName: string, item: Item, attributes: KvsKey[], options: PutItemOptions, region: string): Promise<void>;
|
|
@@ -9,46 +9,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.putItem =
|
|
12
|
+
exports.putItem = void 0;
|
|
13
13
|
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
const qpqDynamoOrm_1 = require("./qpqDynamoOrm");
|
|
15
|
+
// We currently only support objects as root items in dynamo
|
|
16
|
+
// Storing a list (array) as the root of your DynamoDB record is indeed possible,
|
|
17
|
+
// but there are some caveats. One of the main challenges is that DynamoDB doesn't support
|
|
18
|
+
// complex operations on list items. For instance, you can't perform a conditional update or
|
|
19
|
+
// delete on a specific item in the list based on its value. However, you can read and write
|
|
20
|
+
// entire lists, and you can append items to a list with an update operation.
|
|
21
|
+
//
|
|
22
|
+
// TODO?: support lists as root items in dynamo ~ Probably not worth it
|
|
23
|
+
// a root list probably should be a table!
|
|
24
|
+
// Something like this:
|
|
25
|
+
// const convertObjectOrListToDynamoMap = (value: KvsObjectDataType | KvsListDataType): AttributeValue => {
|
|
26
|
+
// const attributeValue = buildAttributeValue(value);
|
|
27
|
+
// if ('M' in attributeValue) {
|
|
28
|
+
// return attributeValue.M!;
|
|
29
|
+
// }
|
|
30
|
+
// if ('L' in attributeValue) {
|
|
31
|
+
// return attributeValue.L!;
|
|
32
|
+
// }
|
|
33
|
+
// throw new Error("Value must be an object or a list");
|
|
34
|
+
// }
|
|
35
|
+
const convertObjectToDynamoItem = (obj) => {
|
|
36
|
+
return (0, qpqDynamoOrm_1.buildAttributeValue)(obj).M;
|
|
22
37
|
};
|
|
23
|
-
exports.readKvsAttributeValue = readKvsAttributeValue;
|
|
24
|
-
const getDynamoValueTypeFromKvsKey = (key) => {
|
|
25
|
-
switch (key.type) {
|
|
26
|
-
case 'string':
|
|
27
|
-
return 'S';
|
|
28
|
-
case 'number':
|
|
29
|
-
return 'N';
|
|
30
|
-
case 'binary':
|
|
31
|
-
return 'B';
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
exports.getDynamoValueTypeFromKvsKey = getDynamoValueTypeFromKvsKey;
|
|
35
38
|
function putItem(tableName, item, attributes, options, region) {
|
|
36
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
40
|
const dynamoClient = new client_dynamodb_1.DynamoDBClient({ region });
|
|
38
|
-
// Read all the values from the item
|
|
39
|
-
const dynamoProps = attributes
|
|
40
|
-
.filter((a, index, self) => index === self.findIndex((t) => t.key === a.key))
|
|
41
|
-
.reduce((acc, attribute) => {
|
|
42
|
-
const { key, value } = (0, exports.readKvsAttributeValue)(item, attribute);
|
|
43
|
-
const attributeValue = {
|
|
44
|
-
[(0, exports.getDynamoValueTypeFromKvsKey)(key)]: value,
|
|
45
|
-
};
|
|
46
|
-
acc[key.key] = attributeValue;
|
|
47
|
-
return acc;
|
|
48
|
-
}, {});
|
|
49
41
|
yield dynamoClient.send(new client_dynamodb_1.PutItemCommand({
|
|
50
42
|
TableName: tableName,
|
|
51
|
-
Item: (
|
|
43
|
+
Item: convertObjectToDynamoItem(item),
|
|
52
44
|
}));
|
|
53
45
|
});
|
|
54
46
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { KvsQueryOperation, KvsQueryCondition, KvsLogicalOperator } from 'quidproquo-core';
|
|
1
|
+
import { KvsQueryOperation, KvsQueryCondition, KvsLogicalOperator, KvsAdvancedDataType } from 'quidproquo-core';
|
|
2
2
|
import { AttributeValue } from '@aws-sdk/client-dynamodb';
|
|
3
|
+
export declare const getItemName: (name: string) => string;
|
|
4
|
+
export declare const getValueName: (value: KvsAdvancedDataType) => string;
|
|
3
5
|
export declare const isKvsQueryCondition: (query: KvsQueryOperation) => query is KvsQueryCondition;
|
|
4
6
|
export declare const isKvsLogicalOperator: (query: KvsQueryOperation) => query is KvsLogicalOperator;
|
|
7
|
+
export declare const buildAttributeValue: (value: KvsAdvancedDataType) => AttributeValue;
|
|
5
8
|
export declare const buildDynamoQueryExpression: (query?: KvsQueryOperation) => string | undefined;
|
|
6
9
|
export declare const buildExpressionAttributeValues: (queries: (KvsQueryOperation | undefined)[]) => Record<string, AttributeValue>;
|
|
7
10
|
export declare const buildExpressionAttributeNames: (queries: (KvsQueryOperation | undefined)[]) => Record<string, string>;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.buildExpressionAttributeNames = exports.buildExpressionAttributeValues = exports.buildDynamoQueryExpression = exports.isKvsLogicalOperator = exports.isKvsQueryCondition = void 0;
|
|
6
|
+
exports.buildExpressionAttributeNames = exports.buildExpressionAttributeValues = exports.buildDynamoQueryExpression = exports.buildAttributeValue = exports.isKvsLogicalOperator = exports.isKvsQueryCondition = exports.getValueName = exports.getItemName = void 0;
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
8
|
const quidproquo_core_1 = require("quidproquo-core");
|
|
9
9
|
// TODO: Come up with a better way of generating item name / values
|
|
@@ -15,52 +15,54 @@ const getHash = (name) => {
|
|
|
15
15
|
const getItemName = (name) => {
|
|
16
16
|
return `#${getHash(name)}`;
|
|
17
17
|
};
|
|
18
|
+
exports.getItemName = getItemName;
|
|
18
19
|
const getValueName = (value) => {
|
|
19
20
|
return `:${getHash(`${typeof value}-${JSON.stringify(value)}`)}`;
|
|
20
21
|
};
|
|
22
|
+
exports.getValueName = getValueName;
|
|
21
23
|
const buildDynamoQueryExpressionBetween = (query) => {
|
|
22
24
|
if (!query.key || !query.valueB || !query.valueB) {
|
|
23
25
|
throw new Error(`Invalid query condition ${quidproquo_core_1.KvsQueryOperationType.Between}`);
|
|
24
26
|
}
|
|
25
|
-
return `${getItemName(query.key)} BETWEEN ${getValueName(query.valueA)} AND ${getValueName(query.valueB)}`;
|
|
27
|
+
return `${(0, exports.getItemName)(query.key)} BETWEEN ${(0, exports.getValueName)(query.valueA)} AND ${(0, exports.getValueName)(query.valueB)}`;
|
|
26
28
|
};
|
|
27
29
|
const buildDynamoQueryExpressionEqual = (query) => {
|
|
28
|
-
return `${getItemName(query.key)} = ${getValueName(query.valueA)}`;
|
|
30
|
+
return `${(0, exports.getItemName)(query.key)} = ${(0, exports.getValueName)(query.valueA)}`;
|
|
29
31
|
};
|
|
30
32
|
const buildDynamoQueryExpressionNotEqual = (query) => {
|
|
31
|
-
return `${getItemName(query.key)} <> ${getValueName(query.valueA)}`;
|
|
33
|
+
return `${(0, exports.getItemName)(query.key)} <> ${(0, exports.getValueName)(query.valueA)}`;
|
|
32
34
|
};
|
|
33
35
|
const buildDynamoQueryExpressionLessThan = (query) => {
|
|
34
|
-
return `${getItemName(query.key)} < ${getValueName(query.valueA)}`;
|
|
36
|
+
return `${(0, exports.getItemName)(query.key)} < ${(0, exports.getValueName)(query.valueA)}`;
|
|
35
37
|
};
|
|
36
38
|
const buildDynamoQueryExpressionLessThanOrEqual = (query) => {
|
|
37
|
-
return `${getItemName(query.key)} <= ${getValueName(query.valueA)}`;
|
|
39
|
+
return `${(0, exports.getItemName)(query.key)} <= ${(0, exports.getValueName)(query.valueA)}`;
|
|
38
40
|
};
|
|
39
41
|
const buildDynamoQueryExpressionGreaterThan = (query) => {
|
|
40
|
-
return `${getItemName(query.key)} > ${getValueName(query.valueA)}`;
|
|
42
|
+
return `${(0, exports.getItemName)(query.key)} > ${(0, exports.getValueName)(query.valueA)}`;
|
|
41
43
|
};
|
|
42
44
|
const buildDynamoQueryExpressionGreaterThanOrEqual = (query) => {
|
|
43
|
-
return `${getItemName(query.key)} >= ${getValueName(query.valueA)}`;
|
|
45
|
+
return `${(0, exports.getItemName)(query.key)} >= ${(0, exports.getValueName)(query.valueA)}`;
|
|
44
46
|
};
|
|
45
47
|
const buildDynamoQueryExpressionIn = (query) => {
|
|
46
|
-
return `${getItemName(query.key)} IN (${query.valueA
|
|
47
|
-
.map((v) => getValueName(v))
|
|
48
|
+
return `${(0, exports.getItemName)(query.key)} IN (${query.valueA
|
|
49
|
+
.map((v) => (0, exports.getValueName)(v))
|
|
48
50
|
.join(', ')})`;
|
|
49
51
|
};
|
|
50
52
|
const buildDynamoQueryExpressionExists = (query) => {
|
|
51
|
-
return `attribute_exists(${getItemName(query.key)})`;
|
|
53
|
+
return `attribute_exists(${(0, exports.getItemName)(query.key)})`;
|
|
52
54
|
};
|
|
53
55
|
const buildDynamoQueryExpressionNotExists = (query) => {
|
|
54
|
-
return `attribute_not_exists(${getItemName(query.key)})`;
|
|
56
|
+
return `attribute_not_exists(${(0, exports.getItemName)(query.key)})`;
|
|
55
57
|
};
|
|
56
58
|
const buildDynamoQueryExpressionBeginsWith = (query) => {
|
|
57
|
-
return `begins_with(${getItemName(query.key)}, ${getValueName(query.valueA)})`;
|
|
59
|
+
return `begins_with(${(0, exports.getItemName)(query.key)}, ${(0, exports.getValueName)(query.valueA)})`;
|
|
58
60
|
};
|
|
59
61
|
const buildDynamoQueryExpressionContains = (query) => {
|
|
60
|
-
return `contains(${getItemName(query.key)}, ${getValueName(query.valueA)})`;
|
|
62
|
+
return `contains(${(0, exports.getItemName)(query.key)}, ${(0, exports.getValueName)(query.valueA)})`;
|
|
61
63
|
};
|
|
62
64
|
const buildDynamoQueryExpressionNotContains = (query) => {
|
|
63
|
-
return `NOT contains(${getItemName(query.key)}, ${getValueName(query.valueA)})`;
|
|
65
|
+
return `NOT contains(${(0, exports.getItemName)(query.key)}, ${(0, exports.getValueName)(query.valueA)})`;
|
|
64
66
|
};
|
|
65
67
|
const buildDynamoQueryExpressionOr = (query) => {
|
|
66
68
|
return query.conditions.map((c) => `(${buildDynamoQueryExpressionRoot(c)})`).join(' OR ');
|
|
@@ -118,7 +120,8 @@ const buildDynamoQueryExpressionRoot = (query) => {
|
|
|
118
120
|
throw new Error(`Invalid query operation: ${JSON.stringify(query)}`);
|
|
119
121
|
};
|
|
120
122
|
const buildAttributeValue = (value) => {
|
|
121
|
-
|
|
123
|
+
const valueType = typeof value;
|
|
124
|
+
switch (valueType) {
|
|
122
125
|
case 'string':
|
|
123
126
|
return { S: value };
|
|
124
127
|
case 'number':
|
|
@@ -127,17 +130,23 @@ const buildAttributeValue = (value) => {
|
|
|
127
130
|
return { BOOL: value };
|
|
128
131
|
case 'object':
|
|
129
132
|
if (Array.isArray(value)) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
return {
|
|
134
|
+
L: value.map((item) => (0, exports.buildAttributeValue)(item)),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
else if (value !== null) {
|
|
138
|
+
return {
|
|
139
|
+
M: Object.fromEntries(Object.entries(value).map(([k, v]) => [k, (0, exports.buildAttributeValue)(v)])),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
return { NULL: true };
|
|
135
144
|
}
|
|
136
|
-
throw new Error(`Invalid attribute value type: ${typeof value}`);
|
|
137
145
|
default:
|
|
138
|
-
throw new Error(`
|
|
146
|
+
throw new Error(`Unsupported data type: ${valueType}`);
|
|
139
147
|
}
|
|
140
148
|
};
|
|
149
|
+
exports.buildAttributeValue = buildAttributeValue;
|
|
141
150
|
const buildDynamoQueryExpression = (query) => {
|
|
142
151
|
if (!query) {
|
|
143
152
|
return undefined;
|
|
@@ -150,12 +159,12 @@ const buildExpressionAttributeValues = (queries) => {
|
|
|
150
159
|
const traverse = (query) => {
|
|
151
160
|
if ((0, exports.isKvsQueryCondition)(query)) {
|
|
152
161
|
if (query.valueA !== undefined) {
|
|
153
|
-
const valueNameA = getValueName(query.valueA);
|
|
154
|
-
values[valueNameA] = buildAttributeValue(query.valueA);
|
|
162
|
+
const valueNameA = (0, exports.getValueName)(query.valueA);
|
|
163
|
+
values[valueNameA] = (0, exports.buildAttributeValue)(query.valueA);
|
|
155
164
|
}
|
|
156
165
|
if (query.valueB !== undefined) {
|
|
157
|
-
const valueNameB = getValueName(query.valueB);
|
|
158
|
-
values[valueNameB] = buildAttributeValue(query.valueB);
|
|
166
|
+
const valueNameB = (0, exports.getValueName)(query.valueB);
|
|
167
|
+
values[valueNameB] = (0, exports.buildAttributeValue)(query.valueB);
|
|
159
168
|
}
|
|
160
169
|
}
|
|
161
170
|
else if ((0, exports.isKvsLogicalOperator)(query)) {
|
|
@@ -172,7 +181,7 @@ const buildExpressionAttributeNames = (queries) => {
|
|
|
172
181
|
const names = {};
|
|
173
182
|
const traverse = (query) => {
|
|
174
183
|
if ((0, exports.isKvsQueryCondition)(query)) {
|
|
175
|
-
const itemName = getItemName(query.key);
|
|
184
|
+
const itemName = (0, exports.getItemName)(query.key);
|
|
176
185
|
names[itemName] = query.key;
|
|
177
186
|
}
|
|
178
187
|
else if ((0, exports.isKvsLogicalOperator)(query)) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AttributeValue } from '@aws-sdk/client-dynamodb';
|
|
2
|
+
import { KvsUpdate } from 'quidproquo-core';
|
|
3
|
+
interface ExpressionAttributeNameMap {
|
|
4
|
+
[key: string]: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const buildUpdateExpressionAttributeNames: (updates: KvsUpdate) => ExpressionAttributeNameMap;
|
|
7
|
+
export declare const buildUpdateExpressionAttributeValues: (updates: KvsUpdate) => {
|
|
8
|
+
[key: string]: AttributeValue;
|
|
9
|
+
} | undefined;
|
|
10
|
+
export declare const buildDynamoUpdateExpression: (updates: KvsUpdate) => string;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildDynamoUpdateExpression = exports.buildUpdateExpressionAttributeValues = exports.buildUpdateExpressionAttributeNames = void 0;
|
|
4
|
+
const quidproquo_core_1 = require("quidproquo-core");
|
|
5
|
+
const buildDynamoQuery_1 = require("./buildDynamoQuery");
|
|
6
|
+
const buildUpdateExpressionAttributeNames = (updates) => {
|
|
7
|
+
let attributeNames = {};
|
|
8
|
+
for (let update of updates) {
|
|
9
|
+
if (Array.isArray(update.attributePath)) {
|
|
10
|
+
// Handle nested attributes
|
|
11
|
+
for (let i = 0; i < update.attributePath.length; i++) {
|
|
12
|
+
let attribute = update.attributePath[i];
|
|
13
|
+
if (typeof attribute === 'string') {
|
|
14
|
+
const itemName = (0, buildDynamoQuery_1.getItemName)(attribute);
|
|
15
|
+
attributeNames[itemName] = attribute;
|
|
16
|
+
// Replace attribute name in path with safe name
|
|
17
|
+
update.attributePath[i] = itemName;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
// Handle top-level attributes
|
|
23
|
+
const itemName = (0, buildDynamoQuery_1.getItemName)(update.attributePath);
|
|
24
|
+
attributeNames[itemName] = update.attributePath;
|
|
25
|
+
update.attributePath = itemName;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return attributeNames;
|
|
29
|
+
};
|
|
30
|
+
exports.buildUpdateExpressionAttributeNames = buildUpdateExpressionAttributeNames;
|
|
31
|
+
const buildUpdateExpressionAttributeValues = (updates) => {
|
|
32
|
+
let attributeValues = {};
|
|
33
|
+
for (let update of updates) {
|
|
34
|
+
if (update.value !== undefined) {
|
|
35
|
+
const valuePlaceholder = (0, buildDynamoQuery_1.getValueName)(update.value);
|
|
36
|
+
attributeValues[valuePlaceholder] = (0, buildDynamoQuery_1.buildAttributeValue)(update.value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return Object.keys(attributeValues).length > 0 ? attributeValues : undefined;
|
|
40
|
+
};
|
|
41
|
+
exports.buildUpdateExpressionAttributeValues = buildUpdateExpressionAttributeValues;
|
|
42
|
+
const buildDynamoUpdateExpressionSet = (update) => {
|
|
43
|
+
if (!update.value) {
|
|
44
|
+
throw new Error("Value must be provided for 'SET' action");
|
|
45
|
+
}
|
|
46
|
+
return `SET ${getNestedItemName(update.attributePath)} = ${(0, buildDynamoQuery_1.getValueName)(update.value)}`;
|
|
47
|
+
};
|
|
48
|
+
const buildDynamoUpdateExpressionRemove = (update) => {
|
|
49
|
+
return `REMOVE ${getNestedItemName(update.attributePath)}`;
|
|
50
|
+
};
|
|
51
|
+
const buildDynamoUpdateExpressionAdd = (update) => {
|
|
52
|
+
if (!update.value) {
|
|
53
|
+
throw new Error("Value must be provided for 'ADD' action");
|
|
54
|
+
}
|
|
55
|
+
return `ADD ${getNestedItemName(update.attributePath)} ${(0, buildDynamoQuery_1.getValueName)(update.value)}`;
|
|
56
|
+
};
|
|
57
|
+
const buildDynamoUpdateExpressionDelete = (update) => {
|
|
58
|
+
if (update.value) {
|
|
59
|
+
return `DELETE ${getNestedItemName(update.attributePath)} ${(0, buildDynamoQuery_1.getValueName)(update.value)}`;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
return `DELETE ${getNestedItemName(update.attributePath)}`;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const getNestedItemName = (attributePath) => {
|
|
66
|
+
if (Array.isArray(attributePath)) {
|
|
67
|
+
let path = '';
|
|
68
|
+
for (let i = 0; i < attributePath.length; i++) {
|
|
69
|
+
if (typeof attributePath[i] === 'string') {
|
|
70
|
+
path += `.${(0, buildDynamoQuery_1.getItemName)(attributePath[i])}`;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
path += `[${attributePath[i]}]`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return path.substring(1); // remove the leading dot
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return (0, buildDynamoQuery_1.getItemName)(attributePath);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const buildDynamoUpdateExpressionRoot = (update) => {
|
|
83
|
+
switch (update.action) {
|
|
84
|
+
case quidproquo_core_1.KvsUpdateActionType.Set:
|
|
85
|
+
return buildDynamoUpdateExpressionSet(update);
|
|
86
|
+
case quidproquo_core_1.KvsUpdateActionType.Remove:
|
|
87
|
+
return buildDynamoUpdateExpressionRemove(update);
|
|
88
|
+
case quidproquo_core_1.KvsUpdateActionType.Add:
|
|
89
|
+
return buildDynamoUpdateExpressionAdd(update);
|
|
90
|
+
case quidproquo_core_1.KvsUpdateActionType.Delete:
|
|
91
|
+
return buildDynamoUpdateExpressionDelete(update);
|
|
92
|
+
default:
|
|
93
|
+
throw new Error(`Invalid update action type: ${update.action}`);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const buildDynamoUpdateExpression = (updates) => {
|
|
97
|
+
return updates.map((update) => buildDynamoUpdateExpressionRoot(update)).join(', ');
|
|
98
|
+
};
|
|
99
|
+
exports.buildDynamoUpdateExpression = buildDynamoUpdateExpression;
|
|
@@ -15,3 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./buildDynamoQuery"), exports);
|
|
18
|
+
__exportStar(require("./buildDynamoUpdate"), exports);
|
|
19
|
+
__exportStar(require("./getDynamoTableIndexByConfigAndQuery"), exports);
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
} | undefined, region: string): Promise<void>;
|
|
1
|
+
import { KvsUpdate, KvsCoreDataType } from 'quidproquo-core';
|
|
2
|
+
export declare function updateItem(tableName: string, region: string, update: KvsUpdate, key: KvsCoreDataType, sortKey?: KvsCoreDataType): Promise<void>;
|
|
@@ -11,44 +11,23 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.updateItem = void 0;
|
|
13
13
|
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
14
|
-
|
|
15
|
-
function updateItem(tableName,
|
|
14
|
+
const qpqDynamoOrm_1 = require("./qpqDynamoOrm");
|
|
15
|
+
function updateItem(tableName, region, update, key, sortKey) {
|
|
16
16
|
return __awaiter(this, void 0, void 0, function* () {
|
|
17
17
|
const dynamoClient = new client_dynamodb_1.DynamoDBClient({ region });
|
|
18
|
-
const
|
|
19
|
-
return `SET #value.#${attr} = :${attr}`;
|
|
20
|
-
});
|
|
21
|
-
const expressionAttributeNames = Object.assign({ '#value': 'value' }, Object.keys(value).reduce((obj, attr) => {
|
|
22
|
-
obj[`#${attr}`] = attr;
|
|
23
|
-
return obj;
|
|
24
|
-
}, {}));
|
|
25
|
-
const expressionAttributeValues = Object.keys(value).reduce((obj, attr) => {
|
|
26
|
-
obj[`:${attr}`] = { S: JSON.stringify(value[attr]) };
|
|
27
|
-
return obj;
|
|
28
|
-
}, {});
|
|
29
|
-
if (options.expires) {
|
|
30
|
-
updateExpressions.push('SET expires = :expires');
|
|
31
|
-
expressionAttributeValues[':expires'] = { N: options.expires.toString() };
|
|
32
|
-
}
|
|
33
|
-
const updateItemParams = {
|
|
18
|
+
const params = {
|
|
34
19
|
TableName: tableName,
|
|
35
20
|
Key: {
|
|
36
|
-
|
|
37
|
-
S: key,
|
|
38
|
-
},
|
|
21
|
+
id: (0, qpqDynamoOrm_1.buildAttributeValue)(key),
|
|
39
22
|
},
|
|
40
|
-
UpdateExpression:
|
|
41
|
-
|
|
42
|
-
|
|
23
|
+
UpdateExpression: (0, qpqDynamoOrm_1.buildDynamoUpdateExpression)(update),
|
|
24
|
+
ExpressionAttributeValues: (0, qpqDynamoOrm_1.buildUpdateExpressionAttributeValues)(update),
|
|
25
|
+
ExpressionAttributeNames: (0, qpqDynamoOrm_1.buildUpdateExpressionAttributeNames)(update),
|
|
43
26
|
};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
yield dynamoClient.send(new client_dynamodb_1.UpdateItemCommand(updateItemParams));
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
console.error('Error updating item:', error);
|
|
50
|
-
throw error;
|
|
27
|
+
if (sortKey) {
|
|
28
|
+
params.Key['sk'] = (0, qpqDynamoOrm_1.buildAttributeValue)(sortKey);
|
|
51
29
|
}
|
|
30
|
+
console.log(yield dynamoClient.send(new client_dynamodb_1.UpdateItemCommand(params)));
|
|
52
31
|
});
|
|
53
32
|
}
|
|
54
33
|
exports.updateItem = updateItem;
|
package/package.json
CHANGED
/package/lib/logic/dynamo/qpqDynamoOrm/{thing.d.ts → getDynamoTableIndexByConfigAndQuery.d.ts}
RENAMED
|
File without changes
|
|
File without changes
|