As part of a lambda function I would like to reuse the connection from a previous context. The way our functions are written currently is using connect
so I would like to run a few options by you.
I am currently using the second and the connection is not being picked up, so I was wondering if this is possible.
1: Choosing when to use connect
if (!mongoose.connection.readyState) {
log(`Connecting to ${DB_URI}.`);
mongoose.connect(DB_URI, options);
log('Connected to the database.');
}
- Reusing a variable and assigning that to
connection
let conn;
if (!conn) {
log(`Connecting to ${DB_URI}.`);
conn = mongoose.createConnection(DB_URI, options);
mongoose.connection = conn;
log('Connected to the database.');
} else {
log(`Using old connection to ${DB_URI}.`);
mongoose.connection = conn;
log('Connected to the database.');
}
And my connection options are:
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
autoIndex: false,
poolSize: 50,
serverSelectionTimeoutMS: 10000,
socketTimeoutMS: 30000,
family: 4,
};
With context.callbackWaitsForEmptyEventLoop = false;
Thank you.