mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
refactor base model crud meths
This commit is contained in:
parent
8a87f15ff5
commit
63014e11db
@ -4,7 +4,7 @@ from asyncpg import UniqueViolationError
|
|||||||
from fastapi import HTTPException, status
|
from fastapi import HTTPException, status
|
||||||
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
|
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
from sqlalchemy.orm import as_declarative, declared_attr
|
||||||
|
|
||||||
|
|
||||||
@as_declarative()
|
@as_declarative()
|
||||||
@ -53,16 +53,19 @@ class Base:
|
|||||||
except SQLAlchemyError as ex:
|
except SQLAlchemyError as ex:
|
||||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||||
|
|
||||||
async def update(self, db_session: AsyncSession, **kwargs):
|
async def update(self, db: AsyncSession, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
:param db_session:
|
:param db:
|
||||||
:param kwargs:
|
:param kwargs
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
for k, v in kwargs.items():
|
try:
|
||||||
setattr(self, k, v)
|
for k, v in kwargs.items():
|
||||||
await self.save(db_session)
|
setattr(self, k, v)
|
||||||
|
return await db.commit()
|
||||||
|
except SQLAlchemyError as ex:
|
||||||
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||||
|
|
||||||
async def save_or_update(self, db: AsyncSession):
|
async def save_or_update(self, db: AsyncSession):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user