Shared database models used by multiple PlanExe services (e.g., frontend_multi_user, future worker_plan_database). Models live here to keep them out of worker_plan_api, which stays worker-focused and lightweight.
model_event.py:EventTypeenum andEventItemSQLAlchemy model.model_planitem.py:PlanStateenum andPlanItemSQLAlchemy model.model_worker.py:WorkerItemSQLAlchemy model for worker heartbeats.model_nonce.py:NonceItemSQLAlchemy model for nonce tracking.model_user_account.py:UserAccountfor OAuth users, credits, and profile data.model_user_provider.py:UserProviderlinks OAuth providers to users.model_user_api_key.py:UserApiKeyfor MCP credits and attribution.model_credit_history.py:CreditHistoryappend-only credit ledger.model_payment_record.py:PaymentRecordfor Stripe/Telegram payments.
Add the repo root (containing database_api/) to PYTHONPATH, then:
from database_api.planexe_db_singleton import db
from database_api.model_event import EventType, EventItem
from database_api.model_planitem import PlanItem, PlanState
from database_api.model_worker import WorkerItem
from database_api.model_nonce import NonceItem
from database_api.model_user_account import UserAccount
from database_api.model_user_provider import UserProvider
from database_api.model_user_api_key import UserApiKey
from database_api.model_credit_history import CreditHistory
from database_api.model_payment_record import PaymentRecordEach model expects a db instance to be available in the module namespace (e.g., via from database_api.planexe_db_singleton import db in your service). Keep the models as-is to avoid divergence across services.
- I dislike that
database_apihas a dependency onFlask. Ideally it should only beSQLAlchemyand no flask. However I have tried getting rid ofFlask+flask_sqlalchemyand make it only depend onSQLAlchemy, but it complicate things, and I don't want to maintain complicated code. So I choose the simple way, accepting thatFlaskis a dependency. - The shared
dbobject is aflask_sqlalchemy.SQLAlchemyinstance; models subclassdb.Model, and bothfrontend_multi_user(admin UI) andworker_plan_databaseexpect to initialize it viadb.init_app(app). - Flask’s app/request contexts drive session lifecycle (create/teardown, rollback on errors) and the
.queryhelper that the models and admin views rely on. - The admin UI registers views using
db.session; switching to a plainSQLAlchemybase would break those until session wiring and teardown hooks were rebuilt. - Even the UI-free workers spin up a minimal Flask app purely to bootstrap
dband context handling; a newBasewould need equivalent scoped-session setup and teardown hooks. - Removing
Flaskwould require defining a new declarative base, custom scoped session management, and updating every service (workers + frontend) to use the new base consistently—risky until all consumers are migrated together.