Skip to content

Commit

Permalink
Merge pull request #46 from Ed-XCF/feature/dict-support-pagination
Browse files Browse the repository at this point in the history
add _asdict to support fastapi-pagination
  • Loading branch information
JoshYuJump authored Nov 25, 2021
2 parents 5bb04e8 + 95b3439 commit 61bc16f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 11 additions & 1 deletion bali/db/connection.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Dict, Optional, Any, Type, TypeVar, Union, List
from typing import Dict, Optional, Any, Type, TypeVar, Union, List, Tuple

from sqlalchemy.orm.query import Query
from sqlalchemy.orm.session import Session
Expand All @@ -17,8 +17,12 @@ class BaseModel:
updated_time: _DateTimeField
is_active: _BooleanField

def _asdict(self) -> Dict[str, Any]: ...

def to_dict(self) -> Dict[str, Any]: ...

def dict(self) -> Dict[str, Any]: ...

@classmethod
def exists(cls: Type[_M], **attrs) -> bool: ...

Expand Down Expand Up @@ -51,6 +55,12 @@ class BaseModel:
@classmethod
def get_fields(cls: Type[_M]) -> List[str]: ...

@classmethod
def get_or_create(cls: Type[_M], defaults: Dict[str, Any], **attrs) -> Tuple[_M, bool]: ...

@classmethod
def update_or_create(cls: Type[_M], defaults: Dict[str, Any], **attrs) -> Tuple[_M, bool]: ...


class DB(SQLAlchemy):
BaseModel: BaseModel
Expand Down
5 changes: 2 additions & 3 deletions bali/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,10 @@ def delete(self):
db.session.delete(self)
db.session.commit() if context_auto_commit.get() else db.session.flush()

def to_dict(self):
def _asdict(self):
return {c.name: getattr(self, c.name, None) for c in self.__table__.columns}

def dict(self):
return self.to_dict()
dict = to_dict = _asdict

@classmethod
def count(cls, **attrs) -> int:
Expand Down

0 comments on commit 61bc16f

Please sign in to comment.