1.0.2 • Published 5 years ago
mongodb-lambda v1.0.2
mongodb-lambda
Used to cache mongodb database instances for AWS Lambda function, following this best practice
Installation
$ npm install --save mongodb-lambda
Use
- Require the mongodb-lambda module outside of your lambda handler and pass the mongodb connection string to the constructor
- Set
context.callbackWaitsForEmptyLoop = false
inside your lambda handler - Call
connect()
method to connect to database. This will return aPromise
that resolves to the mongodb db instance.
const mongo = require('mongodb-lambda')(process.env.MONGODB_CONNECTION_STRING)
exports.handler = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
return mongo.connect()
.then(db => db.collection(process.env.COLLECTION_NAME).find().toArray())
};