Skip to content

Instantly share code, notes, and snippets.

@sh0tt
Created November 2, 2015 23:12
Show Gist options
  • Save sh0tt/401469478dd6e8c1f225 to your computer and use it in GitHub Desktop.
Save sh0tt/401469478dd6e8c1f225 to your computer and use it in GitHub Desktop.
console.log('Loading function');
var AWS = require('aws-sdk');
AWS.config.region = 'ap-northeast-1';
var db = new AWS.DynamoDB();
exports.handler = function(event, context) {
console.log(event);
var ddbRequest = null;
var formatOutput = function(data) {
var output = [];
for ( var i = 0; i < data.Items.length; i++ ) {
var item = data.Items[i];
output.push({
'id' : item.id.N,
'title' : item.title.S,
'description' : item.description.S,
'done' : item.done.BOOL,
'createdAt' : item.createdAt.S,
'updatedAt' : item.updatedAt.S
});
}
return output;
}
console.log("hogehogehofoo")
console.log(event.id)
if (event.id !== null) {
console.log("here......")
var params = {
TableName : 'Tasks',
KeyConditions : {
"id" : {
"AttributeValueList" : [
{
"N" : String(event.id)
}
],
"ComparisonOperator" : "EQ"
}
}
}
ddbRequest = db.query(params, function(err, data) {
err && console.log(err);
console.log('query');
context.succeed(formatOutput(data));
});
} else {
console.log("request fail");
context.fail("required id is null");
}
ddbRequest.send();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment