add postgres examples

This commit is contained in:
2025-09-18 15:43:33 +08:00
parent 093d8efd3b
commit 5d5fa5e347
4 changed files with 283 additions and 0 deletions

View File

@@ -14,6 +14,12 @@ Reusable SQLModel + PostgreSQL kit with src layout, sync/async engines, and gene
- Editable install: `uv pip install -e .`
- Run tests: `uv pip install pytest && pytest -q`
## Local Postgres (Docker)
- Start the container: `docker compose -f docker/docker-compose.yml up -d`
- Stop when done: `docker compose -f docker/docker-compose.yml down`
- Default credentials match `DatabaseConfig`: user `appuser`, password `changeme`, database `appdb`
- Export `SQL_SSLMODE=disable` (container does not use TLS by default)
## Usage
- Configure environment (either `SQL_*` or Postgres `PG*` vars). Example for container ADDR:
- `export SQL_HOST=192.168.64.8`
@@ -142,6 +148,14 @@ with get_session() as s:
ok = repo.delete(s, h.id)
```
For a container-backed workflow, run `uv run python examples/07_postgres_minimal.py` (paired with `notebooks/07_postgres_minimal.ipynb`). That walkthrough now mirrors a REST-style request cycle:
- wipes existing demo rows so each run is deterministic
- upserts seed rows via `Repository.bulk_insert`
- paginates filtered results with `Repository.list(... where=..., order_by=..., page=..., size=...)`
- performs fuzzy search using `select(...).where(Model.name.ilike(...))`
- counts inventory with `session.exec(select(func.count(...))).scalar()` (compatible with SQLAlchemy 1.4/2.x)
- updates and deletes with the repository helpers you would call from PATCH/DELETE handlers
### Bulk insert + filters/pagination
```bash