UNPKG

serverless-sam

Version:

Serverless framework plugin to export AWS SAM templates for a service

53 lines (46 loc) 1.28 kB
'use strict'; const uuid = require('uuid'); const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies const dynamoDb = new AWS.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(null, { statusCode: 400, headers: { 'Content-Type': 'text/plain' }, body: '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) => { // handle potential errors if (error) { console.error(error); callback(null, { statusCode: error.statusCode || 501, headers: { 'Content-Type': 'text/plain' }, body: 'Couldn\'t create the todo item.', }); return; } // create a response const response = { statusCode: 200, body: JSON.stringify(params.Item), }; callback(null, response); }); };