Skip to content

Commit

Permalink
feat: add soft deletion mixin for model handling
Browse files Browse the repository at this point in the history
Generated-by: aiautocommit
  • Loading branch information
iloveitaly committed Dec 20, 2024
1 parent 53d1d65 commit 2e24e26
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion activemodel/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sqlmodel as sm
from sqlalchemy import Connection, event
from sqlalchemy.orm import Mapper, declared_attr
from sqlmodel import Session, SQLModel, select
from sqlmodel import Field, Session, SQLModel, select

from .logger import logger
from .query_wrapper import QueryWrapper
Expand Down Expand Up @@ -123,6 +123,19 @@ def __tablename__(cls) -> str:
"""
return pydash.strings.snake_case(cls.__name__)

@classmethod
def foreign_key(cls):
"""
Returns a Field object referencing the foreign key of the model.
"""

return Field(
# TODO id field is hard coded
sa_type=cls.model_fields["id"].sa_column.type, # type: ignore
foreign_key=f"{cls.__tablename__}.id",
nullable=False,
)

@classmethod
def select(cls, *args):
return QueryWrapper[cls](cls, *args)
Expand Down
1 change: 1 addition & 0 deletions activemodel/mixins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .soft_delete import SoftDeletionMixin
from .timestamps import TimestampsMixin
from .typeid import TypeIDMixin
17 changes: 17 additions & 0 deletions activemodel/mixins/soft_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from datetime import datetime

import sqlalchemy as sa
from sqlmodel import Field


class SoftDeletionMixin:
deleted_at: datetime = Field(
default=None,
nullable=True,
# TODO https://github.com/fastapi/sqlmodel/discussions/1228
sa_type=sa.DateTime(timezone=True), # type: ignore
)

def soft_delete(self):
self.deleted_at = datetime.now()
raise NotImplementedError("Soft deletion is not implemented")

0 comments on commit 2e24e26

Please sign in to comment.