-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveData.js
34 lines (27 loc) · 902 Bytes
/
saveData.js
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
const awsXRay = require('aws-xray-sdk');
const AWS = awsXRay.captureAWS(require('aws-sdk'));
const client = new AWS.DynamoDB.DocumentClient({});
exports.exec = async (event) => {
const unixTimestamp = new Date().getTime();
const { id, yearMonth, value } = event.detail;
await client.update({
TableName: process.env.DATA_TABLE,
Key: { id, yearMonth: Number(yearMonth)},
UpdateExpression:
"SET amount = if_not_exists(amount,:v), updatedAt = :updatedAt, createdAt = if_not_exists(createdAt, :createdAt)",
ExpressionAttributeValues: {
':v': 0,
':createdAt': unixTimestamp,
':updatedAt': unixTimestamp,
}
}).promise();
await client.update({
TableName: process.env.DATA_TABLE,
Key: { id, yearMonth: Number(yearMonth)},
UpdateExpression:
"ADD amount :v",
ExpressionAttributeValues: {
':v': value,
}
}).promise();
}