import { MongoClient as MDBClient } from 'mongodb'

import { mongoUrl } from './const'

const dbInstanses = {
}

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,
}
