Mongoose Connector
yarn add middy-mongoose-connector
This is a mongoose.js connection middleware for the middy.js middleware framework.
const log = shouldLog ? console : null; /** A database connection middleware that creates or persists a connection * to MongoDB via Mongoose. * * Database connections in Lambda can persist across multiple function calls, * and for performance you likely do not want to close it after each use. * * @param * @param * @param * @param * @param * @param * * @return */ mongoose databaseURI connectionOpts = useCreateIndex: true useFindAndModify: false useNewUrlParser: true useUnifiedTopology: true shouldClose = false shouldLog = true before: async { if mongooseconnectionreadyState === 1 ; else ; await mongoose; } after: async { if shouldClose && mongooseconnectionreadyState !== 0 ; await mongooseconnection; };
Help Wanted!
Currently the middleware requires you to import mongoose and pass it in as an option to properly establish the connection. This is a side effect of how NPM manages dependencies. Ideally This middleware would specify mongoose as a peer dependency and share the same mongoose instance with the project it's used in. I could not get this to work with webpack, but if you are familiar then please submit a PR!