Skip to content

Instantly share code, notes, and snippets.

@ymulenll
Created March 6, 2024 01:00
Show Gist options
  • Save ymulenll/31a79a0e694bf2e42c42f668b83a4244 to your computer and use it in GitHub Desktop.
Save ymulenll/31a79a0e694bf2e42c42f668b83a4244 to your computer and use it in GitHub Desktop.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb";
const ddbDocClient = DynamoDBDocumentClient.from(new DynamoDBClient({}));
export const handler = async (event) => {
try {
const book = JSON.parse(event.body);
const newBook = {
...book,
id: book.id ?? crypto.randomUUID(),
};
await ddbDocClient.send(
new PutCommand({
TableName: process.env.BOOKS_TABLE_NAME,
Item: newBook,
})
);
return {
statusCode: 201,
body: JSON.stringify(newBook),
};
} catch (error) {
console.error(error);
return {
statusCode: 500,
body: JSON.stringify({ message: error.message }),
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment