-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a3039f
commit 64679c1
Showing
12 changed files
with
80 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import re | ||
|
||
import sqlalchemy | ||
from sqlalchemy import Integer, not_ | ||
from sqlalchemy.orm import Mapped, Query, Session, as_declarative, declared_attr, mapped_column | ||
|
||
|
||
@as_declarative() | ||
class Base: | ||
"""Base class for all database entities""" | ||
|
||
@declared_attr | ||
def __tablename__(cls) -> str: # pylint: disable=no-self-argument | ||
"""Generate database table name automatically. | ||
Convert CamelCase class name to snake_case db table name. | ||
""" | ||
return re.sub(r"(?<!^)(?=[A-Z])", "_", cls.__name__).lower() | ||
|
||
|
||
class BaseDbModel(Base): | ||
__abstract__ = True | ||
id: Mapped[int] = mapped_column(Integer, primary_key=True) | ||
|
||
@classmethod | ||
def query(cls, session: Session, with_deleted: bool = False) -> Query: | ||
objs = session.query(cls) | ||
if not with_deleted and hasattr(cls, "is_deleted"): | ||
objs = objs.filter(not_(cls.is_deleted)) | ||
return objs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.