- Initialize project with uv, FastAPI, and libsql-client for Turso persistence - Implement local SQLite fallback (`file:local.db`) for zero-friction development and testing - Create schema and seeder with 9 therapy services from Armonía del Ser flyer - Implement POST /bookings with automatic WhatsApp deep-link generation and SPEI instructions - Implement POST /payments/verify 1-tap admin acknowledgment flow - Add automated test suite with 5 passing tests and verification gates (ruff, pyright, pytest)
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import contextlib
|
|
from collections.abc import AsyncIterator
|
|
from typing import Any
|
|
|
|
import libsql_client
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
def get_client() -> libsql_client.Client:
|
|
"""Create a new async Turso/libSQL client based on current settings."""
|
|
if settings.database_url.startswith("file:"):
|
|
return libsql_client.create_client(settings.database_url)
|
|
return libsql_client.create_client(
|
|
url=settings.database_url,
|
|
auth_token=settings.database_auth_token or "",
|
|
)
|
|
|
|
|
|
@contextlib.asynccontextmanager
|
|
async def get_db() -> AsyncIterator[libsql_client.Client]:
|
|
"""Dependency / context manager for acquiring a database client."""
|
|
client = get_client()
|
|
try:
|
|
yield client
|
|
finally:
|
|
await client.close()
|
|
|
|
|
|
def row_to_dict(result_set: libsql_client.ResultSet, row: libsql_client.Row) -> dict[str, Any]:
|
|
"""Helper to convert a libsql Row into a Python dictionary."""
|
|
return {col: val for col, val in zip(result_set.columns, row, strict=False)}
|
|
|
|
|
|
def result_to_dicts(result_set: libsql_client.ResultSet) -> list[dict[str, Any]]:
|
|
"""Helper to convert all rows of a ResultSet into dictionaries."""
|
|
return [row_to_dict(result_set, row) for row in result_set.rows]
|