FastAPI ORM Helper
FastAPI ORM Helper helps us to work with SQLAlchemy easier with lots of useful functions
How to use
from fastapi_orm_helper import BaseRepository
from users.entities.user import UserEntity
class UserRepository(BaseRepository[UserEntity]):
_entity = UserEntity
CRUD examples
class UserService:
def __init__(self, user_repo: UserRepository = Depends()):
self.user_repo = user_repo
async def create_user(self, **kwargs):
return await self.user_repo.create(**kwargs)
async def bulk_create_users(self, **kwargs):
return await self.user_repo.inserts(**kwargs)
async def first_or_create_user(self, **kwargs):
return await self.user_repo.first_or_create(**kwargs)
async def delete_users(self, **kwargs):
return await self.user_repo.delete(**kwargs)
async def get_user_by_id(self, user_id: int):
return await self.user_repo.find_by_id(user_id)
async def find_users(self, **kwargs):
return await self.user_repo.order(UserEntity.created_at).find(**kwargs)
async def first_or_fail_user(self, **kwargs):
return await self.user_repo.first_or_fail(**kwargs)
async def find_user(self, **kwargs):
return await self.user_repo.joins((UserEntity.address, JoinType.LEFT_JOIN)).first(**kwargs)
async def find_users_by_ids(self, **kwargs):
return await self.user_repo.find_by_ids(**kwargs)
async def count_users(self, **kwargs):
return await self.user_repo.count(**kwargs)
async def is_existed_user(self, **kwargs):
return await self.user_repo.exists(**kwargs)
async def update(self, **kwargs):
return await self.user_repo.update(**kwargs)
async def update_not_change_updated_at(self, **kwargs):
return await self.user_repo.update_not_change_updated_at(**kwargs)
async def upsert(self, **kwargs):
return await self.user_repo.upsert(**kwargs)
async def update_user_by_id(self, user_id: int, **kwargs):
return await self.user_repo.update_by_id(user_id, **kwargs)
query examples
joinedload relationships
async def get_user_by_id(self, user_id: int):
return await self.user_repo.joinedload(UserEntity.avatar).find_by_id(user_id)
select_in_load relationships
async def get_user_posts(self, user_id: int):
return await self.user_repo.select_in_load(UserEntity.posts).find_by_id(user_id)
load relationships
_relationships = [
joinedload(UserEntity.avatar),
select_in_load(UserEntity.posts),
]
async def find_users(self, **kwargs):
return await self.user_repo.load(*_relationships).find(**kwargs)
scope columns in response
async def get_user_email(self, user_id: int):
return await self.user_repo.with_entities(UserEntity.email).find_by_id(user_id)
paginate query
class PaginationParams:
def __init__(
self,
page: int = Query(default=1, ge=1),
per_page: int = Query(default=10, ge=1, le=20),
):
self.page = page
self.per_page = per_page
async def paginate_users(self, **kwargs, pagination_params: PaginationParams):
return await self.user_repo.order(UserEntity.created_at).paginate(**kwargs, pagination_params=pagination_params)
async def infinite_paginate_users(self, **kwargs, pagination_params: PaginationParams):
return await self.user_repo.order(UserEntity.created_at).order_desc(UserEntity.username).infinite_paginate(**kwargs, pagination_params=pagination_params)
join query
async def get_user_posts(self, city: str):
return await self.user_repo.joins(UserEntity.address).find(AddressEntity.city.ilike('%' + city + '%'))
using raw sql
class UserRepository(BaseRepository[UserEntity]):
async def search_users(self, **kwargs):
query = (
select(
UserEntity.id,
func.count(
...
).label('...'),
)
.select_from(UserEntity)
.outerjoin(PostEntity, PostEntity.author_id == UserEntity.id)
.filter(*criteria)
.group_by(UserEntity.id)
.offset((pagination_params.page - 1) * pagination_params.per_page)
.limit(pagination_params.per_page)
.order_by(desc('username'))
)
return (await self.session.execute(query)).all()
Metadata
Release files for fastapi-orm-helper 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_orm_helper-0.1.7.tar.gz | 9.9 kB | Without provenance | Details for fastapi_orm_helper-0.1.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_orm_helper-0.1.7-py3-none-any.whl | Python 3 | none | any | Details for fastapi_orm_helper-0.1.7-py3-none-any.whl |
Total release size: 20.2 kB
Release files / fastapi_orm_helper-0.1.7.tar.gz
| Download URL | fastapi_orm_helper-0.1.7.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4ee08b8da6fc88f5f34fb1ad1ceec108510baabb00feff902927e06b57c760b7
|
|
BLAKE2b-256 checksum How to use checksums |
4d8f70eb64d1c500286b41e337553d3392c409c832f739c8f46d509e7b6ffe84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Release files / fastapi_orm_helper-0.1.7-py3-none-any.whl
| Download URL | fastapi_orm_helper-0.1.7-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
319c728936ff13d91501e5df0dfa98d31941ca9943a6374851bd894dfb1684d2
|
|
BLAKE2b-256 checksum How to use checksums |
e9c96a4ee07033517a9a51a4167e5551f08020eaf9aaf4ab810a338f9a65a243
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Security for fastapi-orm-helper 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.