Last active
September 13, 2019 16:13
-
-
Save apaatsio/f9f2b408fe02e415629f to your computer and use it in GitHub Desktop.
Single Lambda function for all CRUD operations with DynamoDB in REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Loading function'); | |
var doc = require('dynamodb-doc'); | |
var dynamo = new doc.DynamoDB(); | |
/** | |
* Provide an event that contains the following keys: | |
* | |
* - operation: one of the operations in the switch statement below | |
* - tableName: required for operations that interact with DynamoDB | |
* - payload: a parameter to pass to the operation being performed | |
*/ | |
exports.handler = function(event, context) { | |
//console.log('Received event:', JSON.stringify(event, null, 2)); | |
var operation = event.operation; | |
if (event.tableName) { | |
event.payload.TableName = event.tableName; | |
} | |
switch (operation) { | |
case 'create': | |
dynamo.putItem(event.payload, context.done); | |
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; | |
case 'echo': | |
context.succeed(event.payload); | |
break; | |
case 'ping': | |
context.succeed('pong'); | |
break; | |
default: | |
context.fail(new Error('Unrecognized operation "' + operation + '"')); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@apaatsio okay.. thanks for the quick response