This commit is contained in:
Ashes47 2024-01-28 21:51:27 +05:30
parent 51388d5057
commit 1c108f3599
1 changed files with 8 additions and 3 deletions

View File

@ -9,15 +9,20 @@ let mongoClientSingleton: MongoClient
let mongoUrl: string let mongoUrl: string
const getMongoClient = async (newMongoUrl: string) => { const getMongoClient = async (newMongoUrl: string) => {
if (!mongoClientSingleton || newMongoUrl !== mongoUrl) { if (!mongoClientSingleton) {
// if client doesn't exists
mongoClientSingleton = new MongoClient(newMongoUrl)
mongoUrl = newMongoUrl
return mongoClientSingleton
} else if (mongoClientSingleton && newMongoUrl !== mongoUrl) {
// if client exists but url changed
mongoClientSingleton.close()
mongoClientSingleton = new MongoClient(newMongoUrl) mongoClientSingleton = new MongoClient(newMongoUrl)
mongoUrl = newMongoUrl mongoUrl = newMongoUrl
await mongoClientSingleton.connect()
return mongoClientSingleton return mongoClientSingleton
} }
return mongoClientSingleton return mongoClientSingleton
} }
class MongoDB_Memory implements INode { class MongoDB_Memory implements INode {
label: string label: string
name: string name: string