Lazy load todo app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb"); | |
const { DynamoDBDocumentClient, PutCommand } = require("@aws-sdk/lib-dynamodb"); | |
const dynamoClient = new DynamoDBClient({ region: process.env.AWS_REGION }); | |
const ddbClient = DynamoDBDocumentClient.from(dynamoClient); | |
let snsClient, PublishBatchCommand, SNSClient | |
const { v4: uuidv4 } = require("uuid"); | |
const loadSns = () => { | |
({ SNSClient, PublishBatchCommand } = require("@aws-sdk/client-sns")); | |
snsClient = new SNSClient({ region: process.env.AWS_REGION }); | |
} | |
module.exports.addItem = async (event) => { | |
const body = JSON.parse(event.body); | |
const promises = [] | |
const newItemId = uuidv4() | |
if (body.userId === 'aj') { | |
loadSns(); | |
const snsCommand = new PublishBatchCommand({ | |
PublishBatchRequestEntries: [{ | |
Message: JSON.stringify(body), | |
Id: newItemId, | |
}], | |
TopicArn: process.env.TODO_TOPIC_ARN | |
}) | |
promises.push(snsClient.send(snsCommand)) | |
} | |
const { status, item, itemName } = body | |
const putItemCommand = new PutCommand({ | |
TableName: process.env.TODO_TABLE_NAME, | |
Item: { | |
pk: newItemId, | |
sk: Date.now().toString(), | |
status, | |
item, | |
itemName | |
} | |
}) | |
promises.push(ddbClient.send(putItemCommand)) | |
const res = await Promise.allSettled(promises) | |
return { | |
statusCode: 200, | |
headers: { | |
"content-type": "application/json", | |
}, | |
body: JSON.stringify({ status: "pushed", body }), | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment