mirror of
https://github.com/Steffo99/unimore-bda-6.git
synced 2024-11-22 07:54:19 +00:00
33 lines
724 B
Python
33 lines
724 B
Python
|
import pymongo
|
||
|
import contextlib
|
||
|
import typing as t
|
||
|
import logging
|
||
|
|
||
|
from ..config import MONGO_HOST, MONGO_PORT
|
||
|
|
||
|
log = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
@contextlib.contextmanager
|
||
|
def mongo_client_from_config() -> t.ContextManager[pymongo.MongoClient]:
|
||
|
"""
|
||
|
Create a new MongoDB client and yield it.
|
||
|
"""
|
||
|
log.debug("Opening connection to MongoDB...")
|
||
|
client: pymongo.MongoClient = pymongo.MongoClient(
|
||
|
host=MONGO_HOST.__wrapped__,
|
||
|
port=MONGO_PORT.__wrapped__,
|
||
|
)
|
||
|
log.info("Opened connection to MongoDB!")
|
||
|
|
||
|
yield client
|
||
|
|
||
|
log.info("Closing connection to MongoDB...")
|
||
|
client.close()
|
||
|
log.debug("Closed connection to MongoDB!")
|
||
|
|
||
|
|
||
|
__all__ = (
|
||
|
"mongo_client_from_config",
|
||
|
)
|