Skip to content

Instantly share code, notes, and snippets.

@robertofrontado
Last active May 22, 2020 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertofrontado/7df8d1e4504661612b936818f94ca714 to your computer and use it in GitHub Desktop.
Save robertofrontado/7df8d1e4504661612b936818f94ca714 to your computer and use it in GitHub Desktop.
serverless-todo-createtodo
import * as AWS from 'aws-sdk'
import 'source-map-support/register'
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
import * as uuid from 'uuid'
import { TodoItem } from '../../models/TodoItem'
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const { name } = JSON.parse(event.body);
const id = uuid.v4();
const todo: TodoItem = { id, done: false, createdAt: new Date().toISOString(), name };
const docClient = new AWS.DynamoDB.DocumentClient();
await docClient.put({
TableName: process.env.TODOS_TABLE,
Item: todo
}).promise();
return {
statusCode: 201,
body: JSON.stringify({
item: todo
})
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment