Last active
March 11, 2020 02:37
-
-
Save raphaellondner-mongodb/95a0e08b4a916fe8868928a1ba3134d5 to your computer and use it in GitHub Desktop.
Revisions
-
raphaellondner-mongodb revised this gist
Mar 8, 2017 . No changes.There are no files selected for viewing
-
raphaellondner-mongodb renamed this gist
Feb 24, 2017 . 1 changed file with 4 additions and 2 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 @@ -42,7 +42,7 @@ function processEvent(event, context, callback) { { MongoClient.connect(atlas_connection_uri, function(err, db) { createDoc(db, jsonContents, callback); }); } catch(err) @@ -51,13 +51,15 @@ 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 revised this gist
Feb 18, 2017 . 1 changed file with 0 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 @@ -18,7 +18,6 @@ exports.handler = (event, context, callback) => { return callback(err); } atlas_connection_uri = data.Plaintext.toString('ascii'); processEvent(event, context, callback); }); } -
raphaellondner-mongodb created this gist
Feb 17, 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,65 @@ 'use strict' const AWS = require('aws-sdk'); var MongoClient = require('mongodb').MongoClient; let atlas_connection_uri; exports.handler = (event, context, callback) => { var uri = process.env['MONGODB_ATLAS_CLUSTER_URI']; if (atlas_connection_uri != null) { processEvent(event, context, callback); } else { const kms = new AWS.KMS(); kms.decrypt({ CiphertextBlob: new Buffer(uri, 'base64') }, (err, data) => { if (err) { console.log('Decrypt error:', err); return callback(err); } atlas_connection_uri = data.Plaintext.toString('ascii'); console.log('the Atlas connection string is ' + atlas_connection_uri); processEvent(event, context, callback); }); } }; 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(); }); };