Skip to content

Database

lightapi.database

Attributes

SQLALCHEMY_DATABASE_URL module-attribute

SQLALCHEMY_DATABASE_URL = getenv('DATABASE_URL', 'sqlite:///./lightapi.db')

engine module-attribute

engine = create_engine(SQLALCHEMY_DATABASE_URL)

SessionLocal module-attribute

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Classes

Base

Custom SQLAlchemy base class that provides automatic __tablename__ generation and a method to convert model instances to dictionaries.

Attributes:

Name Type Description
id Column

Primary key column automatically added to all derived models.

Methods:

Name Description
__tablename__

Automatically generates the table name from the class name, converted to lowercase.

as_dict

Converts the model instance into a dictionary where keys are the column names and values are the corresponding data.

Source code in lightapi/database.py
@as_declarative()
class Base:
    """
    Custom SQLAlchemy base class that provides automatic `__tablename__` generation
    and a method to convert model instances to dictionaries.

    Attributes:
        id (Column): Primary key column automatically added to all derived models.

    Methods:
        __tablename__(cls):
            Automatically generates the table name from the class name, converted to lowercase.

        as_dict(self):
            Converts the model instance into a dictionary where keys are the column names and values are the corresponding data.
    """

    pk = Column(Integer, primary_key=True, autoincrement=True, unique=True)
    __table__ = None

    @property
    def table(self):
        return self.__table__

    @declared_attr
    def __tablename__(cls):
        """
        Generates the table name based on the class name.

        The table name is derived by converting the class name to lowercase.

        Returns:
            str: The generated table name.
        """
        return cls.__name__.lower()

    def serialize(self) -> dict:
        """
        Converts the model instance into a dictionary representation.

        Each key in the dictionary corresponds to a column name, and the value is the data stored in that column.

        Returns:
            dict: A dictionary representation of the model instance.
        """
        return {
            column.name: getattr(self, column.name) for column in self.table.columns
        }
Attributes
pk class-attribute instance-attribute
pk = Column(Integer, primary_key=True, autoincrement=True, unique=True)
__table__ class-attribute instance-attribute
__table__ = None
table property
table
Functions
__tablename__
__tablename__()

Generates the table name based on the class name.

The table name is derived by converting the class name to lowercase.

Returns:

Name Type Description
str

The generated table name.

Source code in lightapi/database.py
@declared_attr
def __tablename__(cls):
    """
    Generates the table name based on the class name.

    The table name is derived by converting the class name to lowercase.

    Returns:
        str: The generated table name.
    """
    return cls.__name__.lower()
serialize
serialize() -> dict

Converts the model instance into a dictionary representation.

Each key in the dictionary corresponds to a column name, and the value is the data stored in that column.

Returns:

Name Type Description
dict dict

A dictionary representation of the model instance.

Source code in lightapi/database.py
def serialize(self) -> dict:
    """
    Converts the model instance into a dictionary representation.

    Each key in the dictionary corresponds to a column name, and the value is the data stored in that column.

    Returns:
        dict: A dictionary representation of the model instance.
    """
    return {
        column.name: getattr(self, column.name) for column in self.table.columns
    }