Refactor V3 CRUD: Extract helper functions and unify sanitization logic.

- Created app/lib_api_crud_v3.py to house core security, filtering, and sanitization logic.
- Implemented reusable sanitize_payload() to generically strip virtual lookup fields (*_id_random) and view-only fields (fields_to_exclude_from_db).
- Updated app/routers/api_crud_v3.py to use the new library and consolidated sanitization across all Create/Update endpoints.
- Documented Phase 1 completion in documentation/REFACTOR_API_CRUD_V3.md.
This commit is contained in:
Scott Idem
2026-01-09 16:16:44 -05:00
parent 2ff211f2c2
commit 8459b57e1b
3 changed files with 180 additions and 122 deletions

View File

@@ -0,0 +1,35 @@
# Refactoring Plan: API CRUD V3
**Goal:** Modularize `app/routers/api_crud_v3.py` to improve maintainability, readability, and reusability. The file currently mixes route definitions, security enforcement, data sanitization, and helper utilities.
## Phase 1: Extract Helpers & Core Logic (Safest) - COMPLETED
**Objective:** Move pure functions and business logic out of the router file.
1. **Create `app/lib_api_crud_v3.py`**: DONE
2. **Update `app/routers/api_crud_v3.py`**: DONE (All endpoints now use `sanitize_payload`).
## Phase 2: Separate Child/Nested Routes - PLANNED
1. **Create `app/routers/api_crud_v3_nested.py`**:
* Move `get_child_obj_li`
* Move `post_child_obj`
* Move `get_child_obj`
* Move `patch_child_obj`
* Move `delete_child_obj`
2. **Update `app/main.py` (or router inclusion)**:
* Ensure the new router is included, OR include it within `api_crud_v3.py` if preferred to keep a single import point.
## Phase 3: Schema Introspection
**Objective:** Isolate database introspection logic.
1. **Create `app/lib_schema_v3.py` (or similar)**:
* Move the logic inside `get_obj_schema` (SQL `DESCRIBE` parsing, Pydantic introspection) to a helper function.
## Execution Strategy
We will execute **Phase 1** first as it provides immediate value (removing code duplication for sanitization) with minimal risk to routing logic.
### Testing
After each move:
1. Run `tests/test_v3_router_filtering.py` (requires update to import from new location if we test the lib directly).
2. Verify application startup.