Forked from raphaellondner-mongodb/mongodb-atlas-lambda-step1.js
Last active
August 25, 2017 17:23
-
-
Save aercolino/cfc072f52684c94c40638964b0071e47 to your computer and use it in GitHub Desktop.
Revisions
-
aercolino revised this gist
Aug 25, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -47,6 +47,6 @@ function createDoc(db, data, done) { } console.log("... Done", result.insertedId); done(null); // done() should work, but it doesn't in lambda-local (not in master) }); }; -
aercolino revised this gist
Aug 25, 2017 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ function processEvent(event, context, callback) { createDoc(cachedDb, storableEvent, callback); return; } console.log('- Connecting to database'); MongoClient.connect(atlasConnectionUri, (err, db) => { if (err) { @@ -37,16 +37,16 @@ function makeEventStorable(event) { return data; } function createDoc(db, data, done) { console.log('- Creating document'); db.collection('restaurants') .insertOne(data, (err, result) => { if (err) { console.error('... Error: ', err); return done(err); } console.log("... Done", result.insertedId); done(null); }); }; -
aercolino revised this gist
Aug 25, 2017 . 1 changed file with 36 additions and 29 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,44 +2,51 @@ function processEvent(event, context, callback) { const storableEvent = makeEventStorable(event); storableEvent.created_at = new Date(); console.log('Calling MongoDB Atlas from AWS Lambda: ', JSON.stringify(storableEvent)); if (cachedDb) { console.log('- Reusing cached connection'); createDoc(cachedDb, storableEvent, callback); return; } console.log('- Connecting to database'); MongoClient.connect(atlasConnectionUri, (err, db) => { if (err) { console.error('... Error: ', err); callback(err); return; } console.log('... Done'); cachedDb = db; createDoc(cachedDb, storableEvent, callback); }); } function makeEventStorable(event) { const data = {}; Object.keys(event).forEach((key) => { if (key !== 'grades') { data[key] = event[key]; return; } data[key] = event[key].map((grade) => { grade.date = new Date(grade.date); // use real JS dates return grade; }); }); return data; } function createDoc(db, json, done) { console.log('- Creating document'); db.collection('restaurants') .insertOne(json, (err, result) => { if (err) { console.error('... Error: ', err); return done(err); } console.log("... Done", result.insertedId); done(); }); }; -
aercolino revised this gist
Aug 25, 2017 . 1 changed file with 0 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,4 @@ function processEvent(event, context, callback) { const storableEvent = makeEventStorable(event); storableEvent.created_at = new Date(); console.log('Calling MongoDB Atlas from AWS Lambda: ', JSON.stringify(storableEvent)); @@ -37,10 +33,6 @@ function makeEventStorable(event) { function createDoc (db, json, callback) { db.collection('restaurants') .insertOne( json, function(err, result) { if(err!=null) { console.error("an error occurred in createDoc", err); callback(null, JSON.stringify(err)); -
aercolino revised this gist
Aug 25, 2017 . 1 changed file with 19 additions and 16 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,8 @@ function processEvent(event, context, callback) { if (reuseDb) { context.callbackWaitsForEmptyEventLoop = false; } const storableEvent = makeEventStorable(event); storableEvent.created_at = new Date(); console.log('Calling MongoDB Atlas from AWS Lambda: ', JSON.stringify(storableEvent)); @@ -34,17 +35,19 @@ function makeEventStorable(event) { } function createDoc (db, json, callback) { db.collection('restaurants') .insertOne( json, function(err, result) { if (!reuseDb) { db.close(); } if(err!=null) { console.error("an error occurred in createDoc", err); callback(null, JSON.stringify(err)); } else { console.log("Kudos! You just created an entry into the restaurants collection with id: " + result.insertedId); callback(null, "SUCCESS"); } }); }; -
aercolino revised this gist
Aug 25, 2017 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,8 +2,9 @@ function processEvent(event, context, callback) { //the following line is critical for performance reasons to allow re-use of database connections across calls to this Lambda function and avoid closing the database connection. The first call to this lambda function takes about 5 seconds to complete, while subsequent, close calls will only take a few hundred milliseconds. context.callbackWaitsForEmptyEventLoop = false; const storableEvent = makeEventStorable(event); storableEvent.created_at = new Date(); console.log('Calling MongoDB Atlas from AWS Lambda: ', JSON.stringify(storableEvent)); try { if (cachedDb == null) { @@ -22,10 +23,10 @@ function processEvent(event, context, callback) { } } function makeEventStorable(event) { const cloned = JSON.parse(JSON.stringify(event)); if(cloned.grades != null) { for(var i = 0, len = cloned.grades.length; i < len; i++) { cloned.grades[i].date = new Date(cloned.grades[i].date); } } -
aercolino revised this gist
Aug 25, 2017 . 1 changed file with 15 additions and 16 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,38 +1,37 @@ function processEvent(event, context, callback) { //the following line is critical for performance reasons to allow re-use of database connections across calls to this Lambda function and avoid closing the database connection. The first call to this lambda function takes about 5 seconds to complete, while subsequent, close calls will only take a few hundred milliseconds. context.callbackWaitsForEmptyEventLoop = false; const storableEvent = convertDates(event); console.log('Calling MongoDB Atlas from AWS Lambda with event: ' + JSON.stringify(storableEvent)); try { if (cachedDb == null) { console.log('=> connecting to database'); MongoClient.connect(atlas_connection_uri, function (err, db) { cachedDb = db; return createDoc(db, storableEvent, callback); }); } else { createDoc(cachedDb, storableEvent, callback); } } catch (err) { console.error('an error occurred', err); } } function convertDates(event) { const cloned = JSON.parse(JSON.stringify(event)); if(cloned.grades != null) { for(var i = 0, len=cloned.grades.length; i < len; i++) { cloned.grades[i].date = new Date(cloned.grades[i].date); } } return cloned; } function createDoc (db, json, callback) { db.collection('restaurants').insertOne( json, function(err, result) { if(err!=null) { -
raphaellondner-mongodb revised this gist
Mar 30, 2017 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -43,6 +43,8 @@ function createDoc (db, json, callback) { console.log("Kudos! You just created an entry into the restaurants collection with id: " + result.insertedId); callback(null, "SUCCESS"); } //we don't need to close the connection thanks to context.callbackWaitsForEmptyEventLoop = false (above) //this will let our function re-use the connection on the next called (if it can re-use the same Lambda container) //db.close(); }); }; -
raphaellondner-mongodb revised this gist
Mar 30, 2017 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,6 +13,9 @@ function processEvent(event, context, callback) { } } //the following line is critical for performance reasons to allow re-use of database connections across calls to this Lambda function and avoid closing the database connection. The first call to this lambda function takes about 5 seconds to complete, while subsequent, close calls will only take a few hundred milliseconds. context.callbackWaitsForEmptyEventLoop = false; try { if (cachedDb == null) { console.log('=> connecting to database'); -
raphaellondner-mongodb revised this gist
Mar 29, 2017 . 1 changed file with 13 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,15 +13,19 @@ function processEvent(event, context, callback) { } } try { if (cachedDb == null) { console.log('=> connecting to database'); MongoClient.connect(atlas_connection_uri, function (err, db) { cachedDb = db; return createDoc(db, jsonContents, callback); }); } else { createDoc(cachedDb, jsonContents, callback); } } catch (err) { console.error('an error occurred', err); } } -
raphaellondner-mongodb revised this gist
Mar 8, 2017 . No changes.There are no files selected for viewing
-
raphaellondner-mongodb revised this gist
Feb 24, 2017 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ function processEvent(event, context, callback) { { MongoClient.connect(atlas_connection_uri, function(err, db) { createDoc(db, jsonContents, callback); }); } catch(err) @@ -26,14 +26,16 @@ function processEvent(event, context, callback) { } } function createDoc (db, json, callback) { db.collection('restaurants').insertOne( json, function(err, result) { if(err!=null) { console.error("an error occurred in createDoc", err); callback(null, JSON.stringify(err)); } else { console.log("Kudos! You just created an entry into the restaurants collection with id: " + result.insertedId); callback(null, "SUCCESS"); } db.close(); }); }; -
raphaellondner-mongodb created this gist
Feb 21, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ function processEvent(event, context, callback) { console.log('Calling MongoDB Atlas from AWS Lambda with event: ' + JSON.stringify(event)); var jsonContents = JSON.parse(JSON.stringify(event)); //date conversion for grades array if(jsonContents.grades != null) { for(var i = 0, len=jsonContents.grades.length; i < len; i++) { //use the following line if you want to preserve the original dates //jsonContents.grades[i].date = new Date(jsonContents.grades[i].date); //the following line assigns the current date so we can more easily differentiate between similar records jsonContents.grades[i].date = new Date(); } } try { MongoClient.connect(atlas_connection_uri, function(err, db) { createDoc(db, jsonContents); }); } catch(err) { console.error('an error occurred', err); } } function createDoc (db, json) { db.collection('restaurants').insertOne( json, function(err, result) { if(err!=null) { console.error("an error occurred in createDoc", err); } else { console.log("Kudos! You just created an entry into the restaurants collection with id: " + result.insertedId); } db.close(); }); };