-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
33 lines (32 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const uuidv4 = require('uuid/v4');
const processResponse = require('./process-response.js');
const TABLE_NAME = process.env.TABLE_NAME;
const IS_CORS = process.env.IS_CORS;
const PRIMARY_KEY = process.env.PRIMARY_KEY;
exports.handler = (event) => {
if (event.httpMethod === 'OPTIONS') {
return Promise.resolve(processResponse(IS_CORS));
}
if (!event.body) {
return Promise.resolve(processResponse(IS_CORS, 'invalid', 400));
}
let item = JSON.parse(event.body);
item[PRIMARY_KEY] = uuidv4();
let params = {
TableName: TABLE_NAME,
Item: item
}
return dynamoDb.put(params)
.promise()
.then(() => (processResponse(IS_CORS)))
.catch(dbError => {
let errorResponse = `Error: Execution update, caused a Dynamodb error, please look at your logs.`;
if (dbError.code === 'ValidationException') {
if (dbError.message.includes('reserved keyword')) errorResponse = `Error: You're using AWS reserved keywords as attributes`;
}
console.log(dbError);
return processResponse(IS_CORS, errorResponse, 500);
});
};