Skip to content

Commit 38c7d59

Browse files
committed
πŸ“– [docs][backend] crud rules
1 parent 94b6aba commit 38c7d59

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
paths:
3+
- "backend/kayman/crud/**/*.py"
4+
---
5+
6+
# Backend CRUD
7+
8+
Pure query/persistence helpers over SQLModel. No business logic, no orchestration β€” that lives in `logics/`.
9+
10+
## Organize by entity answered about, not table read
11+
12+
- Place helpers in `crud/<entity>.py` by the entity the caller is asking about. Cross-table reads are fine.
13+
- Naming follows the entity too: `read_account_balance` (in `account.py`) even though it sums `Transaction.amount`.
14+
15+
## Push ordering and filtering down here
16+
17+
- If a `logics/` caller needs sorted, filtered, or limited data, extend the CRUD helper rather than post-processing the result in Python.
18+
- Expose `order_by` as a `Literal[...]` column whitelist plus a `descending: bool` flag. See `TransactionOrderBy` in `crud/transaction.py`.
19+
- DB-side ordering is faster and keeps `logics/` focused on business meaning, not data shaping.
20+
21+
## Signatures
22+
23+
- `session: Session` is always the first parameter.
24+
- For batch/mutating writes, accept `commit: bool = True`. When `True`, commit and `refresh` the rows. When `False`, `flush` instead so a `logics/` caller can compose multiple writes in one transaction.
25+
- For reads that precede a write, accept `for_update: bool = False` and apply `.with_for_update()` to take a row lock (see `read_accounts`).
26+
- Convert `*Base` / `*Create` inputs to the table model with `Model.model_validate(...)` before `session.add`.
27+
28+
## Verify-then-mutate
29+
30+
- Bulk updates fetch the rows with `for_update=True`, check for missing ids, then mutate. See `_verify_account_ids` in `crud/account.py`. Raise `ValueError` on missing ids rather than silently skipping.

0 commit comments

Comments
Β (0)