SPA_AfterTemperRejection/_db.py
Erik 979aae426b perf(db): batch the good-parts N+1 + cache the press/reason reference reads
- _good_parts_map: collapse the per-press 3-query loop (3xN) into 3 grouped
  queries across all presses; semantics preserved (Joborder JSON/substring
  matching, per-press scoping, whole-map degrade-to-{} on error).
- _press_list + /reasons: served from the kernel cached_select() TTL cache.

Part of the gateway connection-pool-saturation fix
(see fastAPI/agent_docs/db-pool-relief.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 12:06:49 +02:00

35 lines
1.3 KiB
Python

"""Kernel DB seam for the temper_rejects page-module.
Mirrors ``modules/fastpress/press/_db.py``: DB access goes ONLY through the
kernel, re-exported here so the coupling to ``app.db`` lives in one place. This
page READS tempered carts + reject reasons and performs one narrow WRITE (insert a
reject row), so it exposes both the SELECT-only proxy and a transactional
connection::
from .._db import run_select_query, transaction
The base scaffold's API (``api/routes.py``) returns SAMPLE data and does NOT import
this yet — wiring the real reads + the ``POST /reject`` write is the next step
(see the TODO in ``api/routes.py``).
NOTE: no connection happens at import — SQLAlchemy's engine is lazy.
"""
from __future__ import annotations
from contextlib import contextmanager
from app.db.mariadb import engine, run_select_query, cached_select # noqa: F401 (kernel DB layer)
__all__ = ["run_select_query", "cached_select", "transaction", "engine"]
@contextmanager
def transaction():
"""Yield a transactional connection: commit on clean exit, rollback on error.
Thin wrapper over the kernel engine's ``begin()`` — same shape as fastpress's
reject write (``modules/fastpress/press/rejects.py``), which this page's real
``POST /reject`` will follow (one INSERT into ``production.rejects``)."""
with engine.begin() as conn:
yield conn