/
put-item.ts
125 lines (113 loc) · 4.42 KB
/
put-item.ts
1
2
3
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Construct } from 'constructs';
import { DynamoMethod, getDynamoResourceArn, transformAttributeValueMap } from './private/utils';
import { DynamoAttributeValue, DynamoConsumedCapacity, DynamoItemCollectionMetrics, DynamoReturnValues } from './shared-types';
import * as ddb from '../../../aws-dynamodb';
import * as iam from '../../../aws-iam';
import * as sfn from '../../../aws-stepfunctions';
import { Stack } from '../../../core';
/**
* Properties for DynamoPutItem Task
*/
export interface DynamoPutItemProps extends sfn.TaskStateBaseProps {
/**
* A map of attribute name/value pairs, one for each attribute.
* Only the primary key attributes are required;
* you can optionally provide other attribute name-value pairs for the item.
*
* @see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-Item
*/
readonly item: { [key: string]: DynamoAttributeValue };
/**
* The name of the table where the item should be written .
*/
readonly table: ddb.ITable;
/**
* A condition that must be satisfied in order for a conditional PutItem operation to succeed.
*
* @see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-ConditionExpression
*
* @default - No condition expression
*/
readonly conditionExpression?: string;
/**
* One or more substitution tokens for attribute names in an expression
*
* @see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-ExpressionAttributeNames
*
* @default - No expression attribute names
*/
readonly expressionAttributeNames?: { [key: string]: string };
/**
* One or more values that can be substituted in an expression.
*
* @see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-ExpressionAttributeValues
*
* @default - No expression attribute values
*/
readonly expressionAttributeValues?: { [key: string]: DynamoAttributeValue };
/**
* Determines the level of detail about provisioned throughput consumption that is returned in the response
*
* @see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-ReturnConsumedCapacity
*
* @default DynamoConsumedCapacity.NONE
*/
readonly returnConsumedCapacity?: DynamoConsumedCapacity;
/**
* The item collection metrics to returned in the response
*
* @see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html#LSI.ItemCollections
*
* @default DynamoItemCollectionMetrics.NONE
*/
readonly returnItemCollectionMetrics?: DynamoItemCollectionMetrics;
/**
* Use ReturnValues if you want to get the item attributes as they appeared
* before they were updated with the PutItem request.
*
* @see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-ReturnValues
*
* @default DynamoReturnValues.NONE
*/
readonly returnValues?: DynamoReturnValues;
}
/**
* A StepFunctions task to call DynamoPutItem
*/
export class DynamoPutItem extends sfn.TaskStateBase {
protected readonly taskMetrics?: sfn.TaskMetricsConfig;
protected readonly taskPolicies?: iam.PolicyStatement[];
constructor(scope: Construct, id: string, private readonly props: DynamoPutItemProps) {
super(scope, id, props);
this.taskPolicies = [
new iam.PolicyStatement({
resources: [
Stack.of(this).formatArn({
service: 'dynamodb',
resource: 'table',
resourceName: props.table.tableName,
}),
],
actions: [`dynamodb:${DynamoMethod.PUT}Item`],
}),
];
}
/**
* @internal
*/
protected _renderTask(): any {
return {
Resource: getDynamoResourceArn(DynamoMethod.PUT),
Parameters: sfn.FieldUtils.renderObject({
Item: transformAttributeValueMap(this.props.item),
TableName: this.props.table.tableName,
ConditionExpression: this.props.conditionExpression,
ExpressionAttributeNames: this.props.expressionAttributeNames,
ExpressionAttributeValues: transformAttributeValueMap(this.props.expressionAttributeValues),
ReturnConsumedCapacity: this.props.returnConsumedCapacity,
ReturnItemCollectionMetrics: this.props.returnItemCollectionMetrics,
ReturnValues: this.props.returnValues,
}),
};
}
}