Skip to content

Commit

Permalink
feat: Implement Singleton pattern for Database class and add logging …
Browse files Browse the repository at this point in the history
…for initialization and table creation
  • Loading branch information
jmcerrejon committed Nov 18, 2024
1 parent b45e4a1 commit f4cc7b1
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
from functools import lru_cache as singleton
from typing import Generator

from sqlalchemy import create_engine
from sqlalchemy import create_engine, inspect
from sqlalchemy.orm import Session, declarative_base, sessionmaker

from src.utils.logger import logger

@singleton
class Database:

class SingletonMeta(type):
"""
Metaclass to implement the Singleton pattern.
"""

_instances: dict[type, object] = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs)
return cls._instances[cls]


class Database(metaclass=SingletonMeta):
__DATABASE_URL = "sqlite:///./DB.db"

def __init__(self):
Expand All @@ -16,9 +29,14 @@ def __init__(self):
self.SessionLocal = sessionmaker(
autocommit=False, autoflush=False, bind=self.engine
)
logger.info("Database initialized.")

def create_tables(self) -> None:
if inspect(self.engine).has_table("tasks"):
return None

self.Base.metadata.create_all(bind=self.engine)
logger.info("Tables created.")

def get_db(self) -> Generator[Session, None, None]:
db = self.SessionLocal()
Expand Down

0 comments on commit f4cc7b1

Please sign in to comment.