I have this code that connects to the Mongol bank. My old code was creating about 30 connections, now this is creating between 4 and 8 connections. I got this code on mongodb's own youtube channel. Is it normal for him to create these 4-8 connections? I left some console.log to see where the code is passing through, and console.log("bbbbb") is executed only once, but when I look at the mongo it says that it has 4-8 connections.
import { MongoClient } from "mongodb";
let cached = global.mongo;
if (!cached) {
cached = global.mongo = { conn: null, promise: null };
}
export default async function connect() {
if (cached.conn) {
console.log("aaaaaaaa");
return cached.conn;
}
if (!cached.promise) {
const opts = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
cached.promise = MongoClient.connect(process.env.DATABASE_URL, opts).then(
(client) => {
console.log("bbbbbbbbb");
return {
client,
db: client.db("test"),
};
}
);
}
cached.conn = await cached.promise;
return cached.conn;
}