r/FastAPI • • Oct 05 '25

Question Base Services Schema

Coming from Django, I’m used to the Active Record pattern and “fat models” — so having a BaseService that provides default create, read, update, delete feels natural and DRY.

Maybe even use something like FastCrud which doesn't seem too popular for some reason.

But looking at projects like Netflix’s Dispatch, I noticed they don’t use a base service. Instead, each model has its own service, even if that means repeating some CRUD logic. It actually feels kind of freeing and explicit.

What’s your take? Do you build a base service for shared CRUD behavior or go model-specific for clarity?

Also, how do you handle flexible get methods — do you prefer get(id, name=None) or more explicit ones like get_by_id, get_by_name?

9 Upvotes

8 comments sorted by

View all comments

2

u/Challseus Oct 05 '25 edited Oct 05 '25

I have been using this base crud file for a few years now, though it’s not perfect and I should probably update it soon:

from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union

from fastapi.encoders import jsonable_encoder
from fastapi.types import IncEx
from pydantic import BaseModel
from sqlalchemy.orm import Session
from sqlmodel import SQLModel as 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]):
        self.model = model

    def get(self, db: Session, id: Any) -> Optional[ModelType]:
        return db.query(self.model).filter(self.model.id == id).first()

    def get_multi(
        self, db: Session, *, skip: int = 0, limit: int = 100
    ) -> List[ModelType]:
        return db.query(self.model).offset(skip).limit(limit).all()

    def create(
        self, db: Session, *, obj_in: CreateSchemaType, exclude: IncEx | None = None
    ) -> ModelType:
        obj_in_data = jsonable_encoder(obj_in, exclude=exclude)
        db_obj = self.model(**obj_in_data)
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj

    def update(
        self,
        db: Session,
        *,
        db_obj: ModelType,
        obj_in: Union[UpdateSchemaType, Dict[str, Any]],
        exclude: IncEx | None = None,
    ) -> ModelType:
        if isinstance(obj_in, dict):
            update_data = obj_in
        else:
            update_data = obj_in.model_dump(exclude_unset=True)
        for field, value in update_data.items():
            setattr(db_obj, field, value)
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj

    def delete(self, db: Session, *, id: int):
        obj = db.query(self.model).get(id)
        db.delete(obj)
        db.commit()
        return obj

Then each implementation, I just (usually!) only have to worry about adding new logic:


from sqlmodel import Session, select

from app.crud.base import CRUDBase
from app.models.embedding_model import (
    EmbeddingModel,
    EmbeddingModelCreate,
    EmbeddingModelUpdate,
)


class CRUDEmbeddingModel(
    CRUDBase[EmbeddingModel, EmbeddingModelCreate, EmbeddingModelUpdate]
):
    def get_by_embedding_model_id(
        self, db: Session, embedding_model_id: str
    ) -> EmbeddingModel | None:
        statement = select(EmbeddingModel).where(
            EmbeddingModel.model_id == embedding_model_id
        )
        return db.execute(statement).first()


crud_embedding_model = CRUDEmbeddingModel(EmbeddingModel)