Last active
August 29, 2015 14:19
-
-
Save matjack1/8c176f7f96c10b843863 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
node_modules/ | |
.DS_Store |
This file contains hidden or 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
{ | |
"Records": [ | |
{"Sns": { | |
"Message": "{\"toMaterialise\": \"stocknotinaccess.json\"}" | |
} } | |
] | |
} |
This file contains hidden or 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
var async = require('async'); | |
var AWS = require('aws-sdk'); | |
var mysql = require('mysql'); | |
var config, connection; | |
var s3 = new AWS.S3(); | |
exports.handler = function( event, context ) { | |
async.waterfall( [ | |
function( callback ) { | |
var toMaterialise; | |
try { | |
toMaterialise = JSON.parse(event.Records[0].Sns.Message).toMaterialise; | |
} catch( e ) { | |
toMaterialise = 'stockincheckout.json'; | |
} | |
getConfigurationFromS3( toMaterialise, callback ); | |
}, | |
function( callback ) { | |
connectToDb( callback ); | |
}, | |
function( callback ) { | |
connection.query( config.queryToMaterialise, function( err, rowsToMaterialise, fields ) { | |
callback( err, rowsToMaterialise ); | |
} ); | |
}, | |
function( rowsToMaterialise, callback ) { | |
connection.query( config.materialisationQuery, function( err, r, f ) { | |
callback( err, rowsToMaterialise ); | |
} ); | |
}, | |
function( rowsToMaterialise, callback ) { | |
for( var i = 0; i < rowsToMaterialise.length; i++ ) { | |
connection.query( config.materialisedTableUpdate, rowsToMaterialise[i] ); | |
} | |
callback( null ); | |
} | |
], function( err, result ) { | |
if( err ) { | |
console.log( result ); | |
} | |
connection.end( function( err ) { | |
context.done(); | |
} ); | |
} ); | |
} | |
function getConfigurationFromS3( toMaterialise, callback ) { | |
s3.getObject( | |
{ | |
Bucket: 'vitsoe-materialiser', | |
Key: toMaterialise | |
}, | |
function( errS3, data ) { | |
if( errS3 ) { | |
callback( errS3, errS3.stack ); | |
} else { | |
config = JSON.parse( data.Body.toString( 'utf-8' ) ); | |
} | |
callback( null ); | |
} | |
); | |
} | |
function connectToDb( callback ) { | |
connection = mysql.createConnection( { | |
host: config.host, | |
user: config.user, | |
password: config.password, | |
database: config.database, | |
multipleStatements: true, | |
connectTimeout: 60000 | |
} ); | |
connection.connect( function( err ) { | |
if (err) throw err; | |
console.log( 'connected as id ' + connection.threadId ); | |
callback( null ); | |
} ); | |
} |
This file contains hidden or 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
{ | |
"name": "node-lambda-template", | |
"version": "0.0.1", | |
"description": "The bare minimum for a node.js app running on Amazon Lambda. Uses node-lambda under the hood to locally run and also deploy your node.js Amazon Lambda application.", | |
"main": "index.js", | |
"repository": { | |
"type": "git", | |
"url": "git://github.com/rebelmail/node-lambda-template.git" | |
}, | |
"author": "motdotla", | |
"license": "ISC", | |
"bugs": { | |
"url": "https://github.com/rebelmail/node-lambda-template/issues" | |
}, | |
"homepage": "https://github.com/rebelmail/node-lambda-template", | |
"dependencies": { | |
"async": "0.2.5", | |
"aws-sdk": "2.1.24", | |
"mysql": "2.6.2", | |
"node-lambda": "0.1.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment