mirror of
https://github.com/kevin-DL/full-stack-fastapi-postgresql.git
synced 2026-01-12 02:05:14 +00:00
* Use Pydantic BaseSettings for config settings * Update fastapi dep to >=0.47.0 and email_validator to email-validator * Fix deprecation warning for Pydantic >=1.0 * Properly support old-format comma separated strings for BACKEND_CORS_ORIGINS Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
from typing import List, Optional, Generic, TypeVar, Type
|
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.base_class import Base
|
|
|
|
ModelType = TypeVar("ModelType", bound=Base)
|
|
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
|
|
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)
|
|
|
|
|
|
class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
|
def __init__(self, model: Type[ModelType]):
|
|
"""
|
|
CRUD object with default methods to Create, Read, Update, Delete (CRUD).
|
|
|
|
**Parameters**
|
|
|
|
* `model`: A SQLAlchemy model class
|
|
* `schema`: A Pydantic model (schema) class
|
|
"""
|
|
self.model = model
|
|
|
|
def get(self, db_session: Session, id: int) -> Optional[ModelType]:
|
|
return db_session.query(self.model).filter(self.model.id == id).first()
|
|
|
|
def get_multi(self, db_session: Session, *, skip=0, limit=100) -> List[ModelType]:
|
|
return db_session.query(self.model).offset(skip).limit(limit).all()
|
|
|
|
def create(self, db_session: Session, *, obj_in: CreateSchemaType) -> ModelType:
|
|
obj_in_data = jsonable_encoder(obj_in)
|
|
db_obj = self.model(**obj_in_data)
|
|
db_session.add(db_obj)
|
|
db_session.commit()
|
|
db_session.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def update(
|
|
self, db_session: Session, *, db_obj: ModelType, obj_in: UpdateSchemaType
|
|
) -> ModelType:
|
|
obj_data = jsonable_encoder(db_obj)
|
|
update_data = obj_in.dict(exclude_unset=True)
|
|
for field in obj_data:
|
|
if field in update_data:
|
|
setattr(db_obj, field, update_data[field])
|
|
db_session.add(db_obj)
|
|
db_session.commit()
|
|
db_session.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def remove(self, db_session: Session, *, id: int) -> ModelType:
|
|
obj = db_session.query(self.model).get(id)
|
|
db_session.delete(obj)
|
|
db_session.commit()
|
|
return obj
|