Last active
December 5, 2023 07:38
-
-
Save astuyve/2e7fe4b39a7ffcfa0646deb9e147802d to your computer and use it in GitHub Desktop.
Eager loading todo list
This file contains hidden or 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); | |
const { SNSClient, PublishBatchCommand } = require("@aws-sdk/client-sns"); | |
const snsClient = new SNSClient({ region: process.env.AWS_REGION }) | |
const { v4: uuidv4 } = require("uuid"); | |
module.exports.addItem = async (event) => { | |
const body = JSON.parse(event.body); | |
const promises = [] | |
const newItemId = uuidv4() | |
if (body.userId === 'aj') { | |
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) | |
console.log(res) | |
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