Skip to content

Instantly share code, notes, and snippets.

@apsquared
Created December 20, 2023 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apsquared/2d5981ca3de4bc1238822619b787aba7 to your computer and use it in GitHub Desktop.
Save apsquared/2d5981ca3de4bc1238822619b787aba7 to your computer and use it in GitHub Desktop.
import { MongoClient } from 'mongodb';
const uri = process.env.MONGODB_URI as string; // your mongodb connection string
const options = {//https://stackoverflow.com/questions/76645619/mongoose-creating-too-many-connections-in-nextjs/76905054#76905054
useNewUrlParser: true,
useUnifiedTopology: false,
maxPoolSize: 1,
minPoolSize: 0,
socketTimeoutMS: 1000,
serverSelectionTimeoutMS: 10000,
maxIdleTimeMS: 1000,};
let sharedPromise:Promise<MongoClient>;
export function getMongoConnection():Promise<MongoClient>{
try {
if (sharedPromise){
console.log("return sharedPromise");
return sharedPromise;
}
console.log("getMongoConnection called");
const client: MongoClient = new MongoClient(uri, options);
/* DEBUG */
client.on('error', (error) => {
if (error) {
console.error(error, `Event close client: ${error.message}`);
}
});
client.on('close', (info: any) => {
console.log('CLOSING DB CONN!', info);
});
client.on('connectionClosed', (info: any) => {
console.log('CLOSING DB CONN!', info);
});
client.on('serverClosed', (info) => {
console.warn('serverClosed!', info);
client.close(true);
sharedPromise=client.connect();
console.warn('serverClosed Complete');
});
client.on('serverHeartbeatFailed', (event) => {
console.log(event);
});
client.on('timeoout', (e) => {
console.log(`Timeout: ${JSON.stringify(e)}`);
});
/* debug*/
const clientPromise: Promise<MongoClient> = client.connect();
sharedPromise = clientPromise;
return clientPromise;
} catch (error) {
console.log("Error in getMongoConnection, try again");
const client: MongoClient = new MongoClient(uri, options);
const clientPromise: Promise<MongoClient> = client.connect();;
return clientPromise;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment