Skip to content

Instantly share code, notes, and snippets.

@raphaellondner-mongodb
Last active March 11, 2020 02:37
Show Gist options
  • Save raphaellondner-mongodb/95a0e08b4a916fe8868928a1ba3134d5 to your computer and use it in GitHub Desktop.
Save raphaellondner-mongodb/95a0e08b4a916fe8868928a1ba3134d5 to your computer and use it in GitHub Desktop.

Revisions

  1. raphaellondner-mongodb revised this gist Mar 8, 2017. No changes.
  2. raphaellondner-mongodb renamed this gist Feb 24, 2017. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions gistfile1.txt → lambda-mongodb-example
    Original 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);
    createDoc(db, jsonContents, callback);
    });
    }
    catch(err)
    @@ -51,13 +51,15 @@ function processEvent(event, context, callback) {
    }
    }

    function createDoc (db, json) {
    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();
    });
  3. raphaellondner-mongodb revised this gist Feb 18, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.txt
    Original 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');
    console.log('the Atlas connection string is ' + atlas_connection_uri);
    processEvent(event, context, callback);
    });
    }
  4. raphaellondner-mongodb created this gist Feb 17, 2017.
    65 changes: 65 additions & 0 deletions gistfile1.txt
    Original 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();
    });
    };