Skip to content

Commit

Permalink
V0.2.8 (#24)
Browse files Browse the repository at this point in the history
* add SoftDeleteQuerySet
imp bulk_create

* update version
  • Loading branch information
taogeYT authored Dec 17, 2024
1 parent aed15aa commit 2fb45db
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
12 changes: 2 additions & 10 deletions appboot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from appboot import timezone
from appboot.conf import settings
from appboot.db import Base, ScopedSession
from appboot.repository import QuerySet, QuerySetProperty
from appboot.repository import QuerySet, QuerySetProperty, SoftDeleteQuerySet
from appboot.utils import camel_to_snake

ModelT = TypeVar('ModelT', bound='Model')
Expand Down Expand Up @@ -40,19 +40,11 @@ class OperatorMixin:


class DeletedAtMixin:
query_set_class = SoftDeleteQuerySet
deleted_at: Mapped[Optional[datetime]] = mapped_column(
DateTime(timezone=True), default=None
)

# @declared_attr.directive # noqa
@classmethod
def __delete_condition__(cls):
return cls.deleted_at.is_(None)

@classmethod
def __delete_value__(cls) -> dict[str, typing.Any]:
return {'deleted_at': timezone.now()}

async def delete(self):
self.deleted_at = timezone.now()
return self
Expand Down
17 changes: 15 additions & 2 deletions appboot/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import typing
from typing import Any, Generic, Optional

from sqlalchemy import insert
from sqlalchemy.ext.asyncio.session import AsyncSession
from sqlalchemy.orm import Query
from sqlalchemy.util import greenlet_spawn

from appboot import timezone
from appboot.exceptions import DoesNotExist
from appboot.pagination import PaginationResult

Expand Down Expand Up @@ -151,8 +153,10 @@ async def create(self, **kwargs) -> ModelT:
await self._session.refresh(instance)
return instance

async def bulk_create(self, **kwargs):
raise NotImplementedError
async def bulk_create(self, records: list[dict[str, Any]]):
stmt = insert(self.model).values(records)
result = await self._session.execute(stmt)
return result.rowcount

async def update(
self,
Expand Down Expand Up @@ -209,3 +213,12 @@ async def generator(step):
yield item

return generator(self._step)


class SoftDeleteQuerySet(QuerySet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._query = self._query.filter_by(deleted_at=None)

async def delete(self) -> int:
return await self.update({'deleted_at': timezone.now()})
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "appboot"
version = "0.2.7"
version = "0.2.8"
description = "Use FastAPI like Django"
authors = ["liyatao <liyatao001@gmail.com>"]
readme = "README.md"
Expand Down

0 comments on commit 2fb45db

Please sign in to comment.