FastAPI ORM Helper
Project description
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()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi_orm_helper-0.1.5.tar.gz
.
File metadata
- Download URL: fastapi_orm_helper-0.1.5.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2b8e3cba770585b8224cbbbe456857c5d98675c9ecd0f33536d2bdc2f795a32 |
|
MD5 | 1ddf64e2ddae42884193c74d1ea2357c |
|
BLAKE2b-256 | 409efc520326a611642a3d9d572f022c3ec9cd2785d293736c4409dd8ef38766 |
File details
Details for the file fastapi_orm_helper-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: fastapi_orm_helper-0.1.5-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bc71cb323dee618588d422988978df1c4a3e60c9e78d246103eed2967687ac5 |
|
MD5 | b84690b6d7e2596953e2d05377b5f4c0 |
|
BLAKE2b-256 | 157250fe4ffc4ad0393d7ee0179aeb6779fcc0f50264446997df014fac71693c |