Last active
August 14, 2019 17:03
-
-
Save ppshein/85fc7485fd1d2fc94c428f5111b0261e to your computer and use it in GitHub Desktop.
This file contains 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
"use strict"; | |
const MongoClient = require('mongodb').MongoClient; | |
const MONGODB_URI = process.env.MONGODB_URI; | |
let cachedDb = null; | |
function connectToDatabase (uri) { | |
if (cachedDb) { | |
return Promise.resolve(cachedDb); | |
} | |
const dbName = process.env.MONGODB; | |
return MongoClient.connect(uri, { useNewUrlParser: true }).then(client => { | |
cachedDb = client.db(dbName); | |
return cachedDb; | |
}); | |
} | |
function queryDatabase (db) { | |
return new Promise(function(resolve, reject) { | |
return db.command({ "replSetGetStatus": 1 }, (err, result) => { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(result); | |
} | |
}); | |
}); | |
} | |
exports.handler = async function(event, context) { | |
context.callbackWaitsForEmptyEventLoop = false; | |
return connectToDatabase(MONGODB_URI) | |
.then(db => queryDatabase(db)) | |
.then(result => { | |
return result.members; | |
}).catch(err => { | |
return err | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment