1.0.2 • Published 5 years ago

mongodb-lambda v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

mongodb-lambda

Used to cache mongodb database instances for AWS Lambda function, following this best practice

Installation

$ npm install --save mongodb-lambda

Use

  1. Require the mongodb-lambda module outside of your lambda handler and pass the mongodb connection string to the constructor
  2. Set context.callbackWaitsForEmptyLoop = false inside your lambda handler
  3. Call connect() method to connect to database. This will return a Promise 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())
};