Overview

Business logic should live in:

  • Model properties (with some exceptions).
  • Model validate method for additional validations (with some exceptions).
  • Services - functions, that mostly take care of writing things to the database.
  • Selectors - functions, that mostly take care of fetching things from the database.

Business logic should not live in:

  • Handlers (APIs).
  • Serializers (Pydantic models).

Model properties vs selectors or services:

  • If the property spans multiple relations, it should better be a selector.
  • If the property is non-trivial & can easily cause N + 1 query problems when serialized, it should better be a selector or a service.