serverless-sam
Version:
Serverless framework plugin to export AWS SAM templates for a service
46 lines (38 loc) • 1.01 kB
text/typescript
import * as uuid from 'uuid'
import { DynamoDB } from 'aws-sdk'
const dynamoDb = new DynamoDB.DocumentClient()
module.exports.create = (event, context, callback) => {
const timestamp = new Date().getTime()
const data = JSON.parse(event.body)
if (typeof data.text !== 'string') {
console.error('Validation Failed')
callback(new Error('Couldn\'t create the todo item.'))
return
}
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
id: uuid.v1(),
text: data.text,
checked: false,
createdAt: timestamp,
updatedAt: timestamp
}
}
// write the todo to the database
dynamoDb.put(params, (error, result) => {
// handle potential errors
if (error) {
console.error(error)
callback(new Error('Couldn\'t create the todo item.'))
return
}
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(result.Item)
}
callback(null, response)
})
}