@compassdigital/provider
Advanced tools
Comparing version 0.1.12 to 0.2.0
@@ -124,9 +124,84 @@ 'use strict'; | ||
{ | ||
var body = typeof event.body == "string" ? JSON.parse(event.body) : event.body; | ||
// This is a stream event | ||
if(event.Records && event.Records.length > 0) | ||
{ | ||
var total_handler_calls = 0; | ||
var completed_handler_payloads = []; | ||
event.Records.forEach(function(record) | ||
{ | ||
if(record.dynamodb) | ||
{ | ||
var new_data = null; | ||
var old_data = null; | ||
if(record.dynamodb.NewImage && record.dynamodb.NewImage.data && record.dynamodb.NewImage.data.S) | ||
{ | ||
try | ||
{ | ||
new_data = JSON.parse(record.dynamodb.NewImage.data.S); | ||
} | ||
catch(err) | ||
{ | ||
console.warn(err); | ||
} | ||
} | ||
this.call_handler.call(this, this.get_handler_this(body), body.method, body.params, body.id, function(err, res) | ||
{ | ||
next(err, Provider.lambda_response_format(res)); | ||
}); | ||
if(record.dynamodb.OldImage && record.dynamodb.OldImage.data && record.dynamodb.OldImage.data.S) | ||
{ | ||
try | ||
{ | ||
old_data = JSON.parse(record.dynamodb.OldImage.data.S); | ||
} | ||
catch(err) | ||
{ | ||
console.warn(err); | ||
} | ||
} | ||
if(new_data || old_data) | ||
{ | ||
total_handler_calls++; | ||
this.call_handler.call(this, this.get_handler_this(), "event_dynamodb", { | ||
new: new_data, | ||
old: old_data | ||
}, null, function(err, res) | ||
{ | ||
if(err) | ||
{ | ||
completed_handler_payloads.push(err); | ||
console.error(err); | ||
} | ||
else if(res.result) | ||
{ | ||
completed_handler_payloads.push(res.result); | ||
} | ||
else if(res.error) | ||
{ | ||
completed_handler_payloads.push(err); | ||
console.error(err); | ||
} | ||
if(completed_handler_payloads.length >= total_handler_calls) | ||
{ | ||
next(err, completed_handler_payloads); | ||
} | ||
}); | ||
} | ||
} | ||
}.bind(this)); | ||
if(total_handler_calls == 0) | ||
{ | ||
next(err, "No handler calls were made"); | ||
} | ||
} | ||
else | ||
{ | ||
var body = ( typeof event.body == "string" ? JSON.parse(event.body) : event.body ) || {}; | ||
this.call_handler.call(this, this.get_handler_this(body), body.method, body.params, body.id, function(err, res) | ||
{ | ||
next(err, Provider.lambda_response_format(res)); | ||
}); | ||
} | ||
} | ||
@@ -137,3 +212,3 @@ | ||
return { | ||
context: jsonrpc.id && jsonrpc.id.toString().split(".").length === 3 ? this.context.bind(this, jsonrpc.id) : undefined | ||
context: jsonrpc && jsonrpc.id && jsonrpc.id.toString().split(".").length === 3 ? this.context.bind(this, jsonrpc.id) : undefined | ||
} | ||
@@ -140,0 +215,0 @@ } |
{ | ||
"name": "@compassdigital/provider", | ||
"version": "0.1.12", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -268,3 +268,38 @@ var assert = require("assert"), | ||
}); | ||
}); | ||
it("should handle a Dynamodb event", function(done) | ||
{ | ||
provider.on("event_dynamodb", function(data, next) | ||
{ | ||
next(null, "I got this: " + JSON.stringify(data)); | ||
}); | ||
provider.lambda.handler({ | ||
Records: | ||
[ { eventID: '549cc49fc879804872b01bbda59eece9', | ||
eventName: 'INSERT', | ||
eventVersion: '1.1', | ||
eventSource: 'aws:dynamodb', | ||
awsRegion: 'us-east-1', | ||
dynamodb: { | ||
NewImage: { | ||
data: { | ||
S: JSON.stringify({ | ||
foo: "bar" | ||
}) | ||
} | ||
} | ||
}, | ||
eventSourceARN: 'arn:aws:dynamodb:us-east-1:428358352287:table/cache-dev-menu/stream/2017-10-11T16:04:21.043' } ] | ||
}, null, function(err, res) | ||
{ | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.should.eql(['I got this: {"new":{"foo":"bar"},"old":null}']) | ||
done(); | ||
}); | ||
}); | ||
}); |
25944
510