Async_ORM_Postgresql_Wrapper/asyncdb/base.py

75 lines
2.4 KiB
Python

"""
See the Base class docstring for details on this module
"""
import sqlalchemy.orm
from sqlalchemy.dialects.postgresql import UUID
@sqlalchemy.orm.as_declarative()
class Base:
"""
Standard base class to define a table in a postgresql database
Default Columns:
-> id
- Defines a UUID generated via postgresql's in-built "uuid_generate_v4" function,
requires the "uuid-ossp" to be installed to the given database. The extension can be installed via
"CREATE EXTENSION IF NOT EXISTS "uuid-ossp";"
- May never be null
-> creation
- Defines the creation date in ISO 8601 format based on the sqlalchemy DateTime class and generated by the
database using func.now()
- May never be null
-> Modification
- Defines a ISO 8601 timestamp that is updated anytime the data in a column is modified for a given row
- Can be null
Usage:
This class, "Base", must be inherited by subclasses to define tables within a database. A subclass must define
a single string with a variable name of "__tablename__" which defines the actual table name within the database.
-> Example Implementation
>>> class SomeTable(Base):
>>> __tablename__ = "Some Table"
>>> data = sqlalchemy.Column(sqlalchemy.String)
"""
__table__: sqlalchemy.Table
# This ID column expects the extension "uuid-ossp" to be installed on the postgres DB
# Can be done via "CREATE EXTENSION IF NOT EXISTS "uuid-ossp";"
id = sqlalchemy.Column(
UUID,
default=sqlalchemy.text("uuid_generate_v4()"),
primary_key=True,
)
creation: sqlalchemy.Column = sqlalchemy.Column(
sqlalchemy.DateTime(timezone=True),
key="creation",
name="creation",
index=True,
quote=True,
unique=False,
default=None,
nullable=False,
primary_key=False,
autoincrement=False,
server_default=sqlalchemy.func.now(),
)
modification: sqlalchemy.Column = sqlalchemy.Column(
sqlalchemy.DateTime(timezone=True),
key="Modification",
name="modification",
index=True,
quote=True,
unique=False,
default=None,
nullable=True,
onupdate=sqlalchemy.func.now(),
primary_key=False,
autoincrement=False
)