-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhandler.js
44 lines (37 loc) · 1.17 KB
/
handler.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
34
35
36
37
38
39
40
41
42
43
44
'use strict';
var doc = require('dynamodb-doc');
var dynamo = new doc.DynamoDB();
// Require Serverless ENV vars
var ServerlessHelpers = require('serverless-helpers-js').loadEnv();
// Require Logic
var lib = require('../lib');
// Lambda Handler
module.exports.handler = function(event, context) {
console.log('Received event:',JSON.stringify(event,null,2));
console.log('Context:',JSON.stringify(context,null,2));
var operation = event.operation;
if(event.tableName) {
event.payload.TableName = event.tableName;
}
switch(operation) {
case 'create':
var uuid = require('node-uuid');
event.payload.Item.postId = uuid.v1();
dynamo.putItem(event.payload,context.succeed({"postId":event.payload.Item.postId}));
break;
case 'read':
dynamo.getItem(event.payload,context.done);
break;
case 'update':
dynamo.updateItem(event.payload, context.done);
break;
case 'delete':
dynamo.deleteItem(event.payload, context.done);
break;
case 'list':
dynamo.scan(event.payload, context.done);
break;
default:
context.fail(new Error('Unrecognized operation "' + operation + '"'));
}
};