forked from AntonioMrtz/SpotifyElectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseConnectionManager.py
82 lines (66 loc) · 2.8 KB
/
DatabaseConnectionManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Database connection provider"""
from pymongo.collection import Collection
from app.common.app_schema import AppEnvironmentMode
from app.database.database_schema import (
BaseDatabaseConnection,
DatabaseCollection,
DatabasePingFailedException,
)
from app.database.DatabaseProductionConnection import DatabaseProductionConnection
from app.database.DatabaseTestingConnection import DatabaseTestingConnection
from app.logging.logging_constants import LOGGING_DATABASE_MANAGER
from app.logging.logging_schema import SpotifyElectronLogger
class DatabaseConnectionManager:
"""Manages the unique database connection and exposes it to the app"""
connection: type[BaseDatabaseConnection]
"""Connection instance of database"""
database_connection_mapping: dict[AppEnvironmentMode, type[BaseDatabaseConnection]] = {
AppEnvironmentMode.PROD: DatabaseProductionConnection,
AppEnvironmentMode.DEV: DatabaseProductionConnection,
AppEnvironmentMode.TEST: DatabaseTestingConnection,
}
"""Mapping between environment mode and database connection"""
_logger = SpotifyElectronLogger(LOGGING_DATABASE_MANAGER).getLogger()
@classmethod
def get_collection_connection(cls, collection_name: DatabaseCollection) -> Collection:
"""Get a connection to a collection
Args:
collection_name (DatabaseCollection): collection name
Returns:
Collection: the connection to the selected collection
"""
assert cls.connection is not None, "DatabaseConnectionManager connection is not init"
return cls.connection.get_collection_connection(collection_name)
@classmethod
def init_database_connection(
cls, environment: AppEnvironmentMode, connection_uri: str
) -> None:
"""
Initializes the database connection.
Args:
environment (AppEnvironmentMode): The current environment mode.
connection_uri (str): The URI for connecting to the database.
Returns:
None
"""
from pymongo import MongoClient
database_connection_class = cls.database_connection_mapping.get(
environment, DatabaseProductionConnection
)
database_connection_class.init_connection(connection_uri)
cls.connection = database_connection_class
cls.client = MongoClient(connection_uri)
@classmethod
def ping_database(cls) -> bool:
"""Pings the database to check the connection
Raises:
DatabasePingFailedException: if ping has failed
Returns:
bool: True if the database is reachable, False otherwise
"""
try:
cls.client.admin.command("ping")
except DatabasePingFailedException:
return False
else:
return True