Skip to content

Add dynamic orm session (read-write) #45

Description

@lesnik512

Port of modern-python/fastapi-sqlalchemy-template#30 (implemented in modern-python/fastapi-sqlalchemy-template#71) so the two templates stay in step.

Behaviour

  • New DB_REPLICA_DSN setting, empty by default. Empty means no replica engine and no change in behaviour.
  • When set, GET/HEAD requests use the replica engine; every other method, and any resolution without a request, uses the primary.

Shape

  • app/resources/db.py: create_sa_engine(url), create_primary_sa_engine, create_replica_sa_engine (returns None when unset), close_sa_engine tolerating None, choose_sa_engine(primary_engine, replica_engine, request: litestar.Request | None = None).
  • app/ioc.py: database_engine, database_replica_engine (both bound_type=None), request-scoped dynamic_engine; session wired to dynamic_engine via explicit kwargs.
  • tests/conftest.py overrides dynamic_engine with the rolled-back connection.
  • app/resources/*.py exempt from ruff TC rules, since modern-di evaluates creator annotations at runtime.
  • Tests for method routing, no-request and no-replica fallbacks, and building the replica engine from the DSN.
  • AGENTS.md updated.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions