-
-
Save ymulenll/31a79a0e694bf2e42c42f668b83a4244 to your computer and use it in GitHub Desktop.
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
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