How isd-sgcu/fdrpkm2026-backend works
1. This repository is a single Bun + TypeScript backend built on Elysia. The runtime entrypoint is src/index.ts, which boots the app defined in src/app.ts and serves the entire API under /v1 via src/routes/index.ts.
2. The backend is intentionally multi-tenant at the HTTP layer: two frontends call the same service, but the project context is derived from the request Host header. fd-api.* maps to the First Date domain, and rpkm-api.* maps to the RPKM domain. That host-based split is especially important in src/routes/auth, where the return URL and project selection are computed from the incoming host rather than from client-provided data.
3. The main API surface is organized by feature area under src/routes/. There is a shared health endpoint, a shared auth module, and two project-specific route trees: src/routes/firstdate/ and src/routes/rpkm/. The rpkm routes are further split into users, groups, and houses, reflecting the domain boundaries of that project.
4. The code follows a controller/service/model layering pattern. Route handlers live in src/routes, business logic is in src/services, and data-shaping/request/response contracts are in src/models. This makes the diagram shape fairly clean: HTTP routes call services, services orchestrate database reads/writes and domain rules, and models define the payload boundaries.
5. Database access is centralized in src/db/ and uses Drizzle ORM. The schema is split into domain-focused files: identity.schema.ts for shared student/registration/travel data, houses.schema.ts for RPKM grouping and house-choice data, games.schema.ts for QR/checkpoint flows, firstdate.schema.ts for First Date entry scans, and auth.schema.ts for Better Auth tables.
6. The shared data model is a key architectural boundary. students, registrations, and travel_legs are written by both projects, so they function as shared identity/registration infrastructure rather than project-local tables. The README calls out the important invariant that students are upserted by student_id, and there is one registration row per (student, project).
7. RPKM-specific workflows center on groups and house assignment. The groups service/route layer manages group state, house choices, and the transactional parts of group updates. The repo notes that group operations must run in a transaction with row-level locking on the group row, which is the main concurrency boundary in the system.
8. Auth is handled separately from domain data but still integrated into the same backend. The repository uses Better Auth tables in the database, and the auth routes rely on Google OAuth / Chula SSO for login. The docs indicate the SSO flow carries no custom project data; the backend reconstructs the correct return target from host context.
9. The database runtime is environment-sensitive. In development, when DATABASE_URL is absent, the backend falls back to a local PGlite database file (DATABASE_FILE); in non-development or when configured, it uses real Postgres. That means the same schema and Drizzle layer support both a lightweight local loop and production Postgres deployments.
10. The repository includes generated SQL migrations in drizzle/ and the Drizzle configuration in drizzle.config.ts. Schema changes are meant to start in src/db/schema/, then be materialized into migrations. The docs/db/ folder documents the canonical data model and is the best source for drawing the ER/data-flow side of the architecture.
11. There are supporting cross-cutting utilities under src/utils/ for auth, error handling, flags, response formatting, group helpers, storage, and student-related logic. These are not separate subsystems, but they are the shared glue that keeps route handlers thin and keeps domain logic consistent across both projects.
12. Tests are organized by feature and database behavior under test/, with focused suites for health checks, groups, houses, and multiple registration flows. This mirrors the runtime boundaries: shared DB schema, RPKM group logic, First Date registration logic, and API smoke tests.
13. Operationally, the service exposes a simple health contract (GET /v1/health) and also supports OpenAPI documentation in development per the README. The project also includes Docker, CI checks, and git hooks, but these are auxiliary; the core architecture is still one API process, one shared database, and two host-routed product domains.
Main entry points: schema/index.ts Schemas · routes/index.ts API Root · db/index.ts DB · rpkm/index.ts RPKM
Components 26
Runtime
Process bootstrap and app assembly
API
HTTP routes organized by domain
- API Root · Mounts the versioned API under /v1
src/routes/index.ts - Health · Simple readiness and smoke endpoint
src/routes/health.ts - Auth Routes · Login and return-target logic derived from Host header
src/routes/auth/index.ts - First Date · First Date feature routes and registration entrypoints
src/routes/firstdate/index.ts - RPKM · RPKM feature routes grouped by domain area
src/routes/rpkm/index.ts - RPKM Users · User-facing RPKM endpoints
src/routes/rpkm/users.ts - RPKM Groups · Group state and house-choice endpoints
src/routes/rpkm/groups.ts - RPKM Houses · House listing and selection endpoints
src/routes/rpkm/houses.ts - FD Users · First Date user and registration endpoints
src/routes/firstdate/users.ts
Domain
Service and model layer for business workflows
- Registration · Shared registration orchestration across projects
src/services/registration.service.ts - FD Service · First Date registration and scan workflow logic
src/services/firstdate.service.ts - FD Reg · First Date registration persistence operations
src/services/fd-registration.service.ts - RPKM Reg · RPKM registration persistence operations
src/services/rpkm-registration.service.ts - Groups · Transactional group updates and house-choice rules
src/services/groups.service.ts - Houses · House lookup and allocation logic
src/services/houses.service.ts - Models · Request and response shapes for registrations and groups
src/models
Data
Drizzle schema and database runtime
- DB · Database connection and runtime selection for Postgres or local PGlite
src/db/index.ts - Schemas · Central schema barrel for all tables and relations
src/db/schema/index.ts - Identity · Students, registrations, and travel legs shared by both projects
src/db/schema/identity.schema.ts - Houses Schema · RPKM group and house-choice tables
src/db/schema/houses.schema.ts - Games Schema · QR and checkpoint flow tables
src/db/schema/games.schema.ts - FD Schema · First Date entry and scan tables
src/db/schema/entries.schema.ts - Auth Schema · Authentication tables for Better Auth
src/db/schema/auth.schema.ts
Support
Shared utilities and helpers
- Utils · Auth, error, response, student, group, and storage helpers
src/utils/index.ts
Connections 36
- Entry → App: boots · Process entry constructs the application
- App → API Root: mounts · App registers the versioned API router
- API Root → Health: includes · Shared health endpoint is exposed under the API
- API Root → Auth Routes: includes · Shared auth router is mounted
- API Root → First Date: includes · First Date route tree is mounted
- API Root → RPKM: includes · RPKM route tree is mounted
- RPKM → RPKM Users: splits · RPKM routes fan out into feature modules
- RPKM → RPKM Groups: splits · RPKM routes fan out into feature modules
- RPKM → RPKM Houses: splits · RPKM routes fan out into feature modules
- First Date → FD Users: includes · First Date API exposes user and registration handlers
- RPKM Users → Registration: calls · User endpoints delegate shared registration logic
- FD Users → FD Service: calls · First Date handlers delegate workflow logic
- FD Service → FD Reg: uses · First Date workflow persists via its registration service
- Registration → RPKM Reg: uses · Shared registration orchestration writes RPKM data
- RPKM Groups → Groups: calls · Group routes are backed by transactional group logic
- RPKM Houses → Houses: calls · House routes delegate to house logic
- Groups → Models: shapes · Group operations use shared request/response contracts
- FD Service → Models: shapes · First Date workflow uses model contracts
- Registration → Models: shapes · Shared registration flow uses payload models
- Auth Routes → Utils: uses · Auth flow depends on host parsing and response helpers
- RPKM Groups → Utils: uses · Group handlers rely on shared helpers
- FD Users → Utils: uses · First Date routes use shared helpers
- DB → Schemas: loads · Database runtime is built from the central schema barrel
- Schemas → Identity: includes · Shared identity and registration tables
- Schemas → Houses Schema: includes · RPKM-specific tables
- Schemas → Games Schema: includes · Checkpoint and QR flow tables
- Schemas → FD Schema: includes · First Date tables
- Schemas → Auth Schema: includes · Better Auth tables
- FD Reg → DB: persists · First Date registration data is stored through Drizzle
- RPKM Reg → DB: persists · RPKM registration data is stored through Drizzle
- Groups → DB: transactions · Group updates require locked transactional writes
- Houses → DB: reads · House lookup and assignment read from the database
- Auth Routes → Auth Schema: auth data · Authentication routes rely on Better Auth tables
- FD Service → FD Schema: reads/writes · First Date workflow uses its domain tables
- RPKM Groups → Houses Schema: reads/writes · Group logic manages house-choice state
- Registration → Identity: shared data · Registrations and student identity are shared across both domains