multy-stub/server/utils/mongo.js

33 lines
654 B
JavaScript
Raw Permalink Normal View History

2023-08-01 13:14:02 +03:00
const MDBClient = require('mongodb').MongoClient
2024-10-16 11:06:23 +03:00
const { mongoUrl } = require('./const')
2023-08-01 13:14:02 +03:00
const dbInstanses = {
}
2024-10-16 11:06:23 +03:00
2023-08-01 13:14:02 +03:00
const mongoDBConnect = async () => {
try {
2024-10-16 11:06:23 +03:00
const MongoClient = new MDBClient(mongoUrl, {
2023-08-01 13:14:02 +03:00
useUnifiedTopology: true,
})
return await MongoClient.connect()
} catch (error) {
console.error(error)
}
}
const client = mongoDBConnect()
const getDB = async (dbName) => {
try {
const cl = await client
dbInstanses[dbName] = await cl.db(dbName)
return dbInstanses[dbName]
} catch (error) {
console.error(error)
}
}
module.exports = {
getDB,
}