Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aercolino/cfc072f52684c94c40638964b0071e47 to your computer and use it in GitHub Desktop.
Save aercolino/cfc072f52684c94c40638964b0071e47 to your computer and use it in GitHub Desktop.

Revisions

  1. aercolino revised this gist Aug 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb-atlas-lambda-step1.js
    Original 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(null); // done() should work, but it doesn't in lambda-local (not in master)
    });
    };
  2. aercolino revised this gist Aug 25, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions mongodb-atlas-lambda-step1.js
    Original 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, json, done) {
    function createDoc(db, data, done) {
    console.log('- Creating document');
    db.collection('restaurants')
    .insertOne(json, (err, result) => {
    .insertOne(data, (err, result) => {
    if (err) {
    console.error('... Error: ', err);
    return done(err);
    }

    console.log("... Done", result.insertedId);
    done();
    done(null);
    });
    };
  3. aercolino revised this gist Aug 25, 2017. 1 changed file with 36 additions and 29 deletions.
    65 changes: 36 additions & 29 deletions mongodb-atlas-lambda-step1.js
    Original 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;
    }

    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);
    console.log('- Connecting to database');
    MongoClient.connect(atlasConnectionUri, (err, db) => {
    if (err) {
    console.error('... Error: ', err);
    callback(err);
    return;
    }
    }
    catch (err) {
    console.error('an error occurred', err);
    }
    console.log('... Done');
    cachedDb = db;
    createDoc(cachedDb, storableEvent, 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);
    const data = {};
    Object.keys(event).forEach((key) => {
    if (key !== 'grades') {
    data[key] = event[key];
    return;
    }
    }
    return cloned;
    data[key] = event[key].map((grade) => {
    grade.date = new Date(grade.date); // use real JS dates
    return grade;
    });
    });
    return data;
    }

    function createDoc (db, json, callback) {
    function createDoc(db, json, done) {
    console.log('- Creating document');
    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");
    .insertOne(json, (err, result) => {
    if (err) {
    console.error('... Error: ', err);
    return done(err);
    }

    console.log("... Done", result.insertedId);
    done();
    });
    };
  4. aercolino revised this gist Aug 25, 2017. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,4 @@
    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));
    @@ -37,10 +33,6 @@ 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));
  5. aercolino revised this gist Aug 25, 2017. 1 changed file with 19 additions and 16 deletions.
    35 changes: 19 additions & 16 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    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;

    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(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");
    }
    //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();
    });
    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");
    }
    });
    };
  6. aercolino revised this gist Aug 25, 2017. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions mongodb-atlas-lambda-step1.js
    Original 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 = convertDates(event);
    console.log('Calling MongoDB Atlas from AWS Lambda with event: ' + JSON.stringify(storableEvent));
    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 convertDates(event) {
    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++) {
    for(var i = 0, len = cloned.grades.length; i < len; i++) {
    cloned.grades[i].date = new Date(cloned.grades[i].date);
    }
    }
  7. aercolino revised this gist Aug 25, 2017. 1 changed file with 15 additions and 16 deletions.
    31 changes: 15 additions & 16 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,37 @@
    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();
    }
    }

    //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, jsonContents, callback);
    return createDoc(db, storableEvent, callback);
    });
    }
    else {
    createDoc(cachedDb, jsonContents, callback);
    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) {
  8. @raphaellondner-mongodb raphaellondner-mongodb revised this gist Mar 30, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion mongodb-atlas-lambda-step1.js
    Original 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");
    }
    db.close();
    //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();
    });
    };
  9. @raphaellondner-mongodb raphaellondner-mongodb revised this gist Mar 30, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions mongodb-atlas-lambda-step1.js
    Original 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');
  10. @raphaellondner-mongodb raphaellondner-mongodb revised this gist Mar 29, 2017. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions mongodb-atlas-lambda-step1.js
    Original file line number Diff line number Diff line change
    @@ -13,15 +13,19 @@ function processEvent(event, context, callback) {
    }
    }

    try
    {
    MongoClient.connect(atlas_connection_uri, function(err, db)
    {
    createDoc(db, jsonContents, callback);
    });
    }
    catch(err)
    {
    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);
    }
    }
  11. @raphaellondner-mongodb raphaellondner-mongodb revised this gist Mar 8, 2017. No changes.
  12. @raphaellondner-mongodb raphaellondner-mongodb revised this gist Feb 24, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions mongodb-atlas-lambda-step1.js
    Original 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);
    createDoc(db, jsonContents, callback);
    });
    }
    catch(err)
    @@ -26,14 +26,16 @@ 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();
    });
    };
    };
  13. @raphaellondner-mongodb raphaellondner-mongodb created this gist Feb 21, 2017.
    39 changes: 39 additions & 0 deletions mongodb-atlas-lambda-step1.js
    Original 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();
    });
    };