Files
multy-stub/server/utils/mongo.ts
Primakov Alexandr Alexandrovich 95bcaf3c5e 2
2025-05-08 15:48:02 +03:00

39 lines
965 B
TypeScript

import { MongoClient as MDBClient } from 'mongodb'
import { mongoUrl } from './const'
const dbInstanses = {
}
console.log('=======================================================')
console.log('mongoUrl', mongoUrl)
console.log('=======================================================')
const mongoDBConnect = async () => {
try {
const MongoClient = new MDBClient(mongoUrl, {})
const client = await MongoClient.connect()
console.log('Подключение к MongoDB успешно')
return client
} catch (error) {
console.log('Неудачная попытка подключения к MongoDB')
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)
}
}
export {
getDB,
}