adding docstring to classes
This commit is contained in:
@@ -1,14 +1,12 @@
|
|||||||
# utils/database.py
|
# utils/database.py
|
||||||
"""Database utilities ....
|
"""Database utilities for database operations.
|
||||||
|
|
||||||
This module provides classes and functions for:
|
This module provides utility functions and classes to interact with a database using SQLAlchemy. It includes:
|
||||||
- ....
|
- Creating a database engine with connection pooling.
|
||||||
- ....
|
- Generating a session factory for creating sessions.
|
||||||
- ....
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from sqlalchemy.pool import QueuePool
|
from sqlalchemy.pool import QueuePool
|
||||||
@@ -17,14 +15,21 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
def get_db_engine(database_url: str):
|
def get_db_engine(database_url: str):
|
||||||
"""Create database engine with connection pooling."""
|
"""Create a database engine with connection pooling.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
database_url (str): The URL string to connect to the database. It can be for SQLite or PostgreSQL databases.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
sqlalchemy.engine.Engine: A SQLAlchemy Engine instance configured according to the provided database URL.
|
||||||
|
"""
|
||||||
if database_url.startswith("sqlite"):
|
if database_url.startswith("sqlite"):
|
||||||
# SQLite configuration
|
# Configuration for SQLite database
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
database_url, connect_args={"check_same_thread": False}, echo=False
|
database_url, connect_args={"check_same_thread": False}, echo=False
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# PostgreSQL configuration with connection pooling
|
# Configuration for PostgreSQL databases with connection pooling
|
||||||
engine = create_engine(
|
engine = create_engine(
|
||||||
database_url,
|
database_url,
|
||||||
poolclass=QueuePool,
|
poolclass=QueuePool,
|
||||||
@@ -39,5 +44,12 @@ def get_db_engine(database_url: str):
|
|||||||
|
|
||||||
|
|
||||||
def get_session_local(engine):
|
def get_session_local(engine):
|
||||||
"""Create session factory."""
|
"""Create a session factory to generate database sessions.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
engine (sqlalchemy.engine.Engine): The SQLAlchemy Engine instance created by `get_db_engine`.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
sqlalchemy.orm.sessionmaker: A session factory bound to the provided engine, suitable for creating database sessions.
|
||||||
|
"""
|
||||||
return sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
return sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|||||||
Reference in New Issue
Block a user