Skip to content

Instantly share code, notes, and snippets.

@astuyve
Last active December 5, 2023 07:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astuyve/2e7fe4b39a7ffcfa0646deb9e147802d to your computer and use it in GitHub Desktop.
Save astuyve/2e7fe4b39a7ffcfa0646deb9e147802d to your computer and use it in GitHub Desktop.
Eager loading todo list
'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