# isd-sgcu/fdrpkm2026-backend architecture

> Architecture diagram of the GitHub repository isd-sgcu/fdrpkm2026-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/isd-sgcu/fdrpkm2026-backend
- Repository: https://github.com/isd-sgcu/fdrpkm2026-backend

## Overview

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: [`src/db/schema/index.ts`](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/index.ts) (Schemas), [`src/routes/index.ts`](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/index.ts) (API Root), [`src/db/index.ts`](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/index.ts) (DB), [`src/routes/rpkm/index.ts`](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/index.ts) (RPKM)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_src_index["Entry<br/>bun bootstrap<br/>[index.ts]"]
  node_src_app["App<br/>[app.ts]"]
end

subgraph group_api["API"]
  node_src_routes_index["API Root<br/>route mount<br/>[index.ts]"]
  node_src_health["Health<br/>[health.ts]"]
  node_src_auth_routes["Auth Routes<br/>host-routed auth<br/>[index.ts]"]
  node_src_firstdate_routes["First Date<br/>project routes<br/>[index.ts]"]
  node_src_rpkm_routes["RPKM<br/>project routes<br/>[index.ts]"]
  node_src_rpkm_users["RPKM Users<br/>route module<br/>[users.ts]"]
  node_src_rpkm_groups["RPKM Groups<br/>route module<br/>[groups.ts]"]
  node_src_rpkm_houses["RPKM Houses<br/>route module<br/>[houses.ts]"]
  node_src_fd_users["FD Users<br/>route module<br/>[users.ts]"]
end

subgraph group_domain["Domain"]
  node_src_registration_svc["Registration"]
  node_src_fd_svc["FD Service"]
  node_src_fd_reg_svc["FD Reg"]
  node_src_rpkm_reg_svc["RPKM Reg"]
  node_src_groups_svc["Groups<br/>[groups.service.ts]"]
  node_src_houses_svc["Houses<br/>[houses.service.ts]"]
  node_src_models["Models<br/>contracts"]
end

subgraph group_data["Data"]
  node_src_db_index[("DB<br/>drizzle runtime<br/>[index.ts]")]
  node_src_db_schema["Schemas<br/>drizzle schema<br/>[index.ts]"]
  node_identity_schema["Identity<br/>shared tables<br/>[identity.schema.ts]"]
  node_houses_schema["Houses Schema<br/>rpkm tables<br/>[houses.schema.ts]"]
  node_games_schema["Games Schema<br/>checkpoint tables<br/>[games.schema.ts]"]
  node_firstdate_schema["FD Schema<br/>firstdate tables<br/>[entries.schema.ts]"]
  node_auth_schema["Auth Schema<br/>better-auth tables<br/>[auth.schema.ts]"]
end

subgraph group_support["Support"]
  node_utils{{"Utils<br/>shared helpers<br/>[index.ts]"}}
end

node_src_index -->|"boots"| node_src_app
node_src_app -->|"mounts"| node_src_routes_index
node_src_routes_index -->|"includes"| node_src_health
node_src_routes_index -->|"includes"| node_src_auth_routes
node_src_routes_index -->|"includes"| node_src_firstdate_routes
node_src_routes_index -->|"includes"| node_src_rpkm_routes
node_src_rpkm_routes -->|"splits"| node_src_rpkm_users
node_src_rpkm_routes -->|"splits"| node_src_rpkm_groups
node_src_rpkm_routes -->|"splits"| node_src_rpkm_houses
node_src_firstdate_routes -->|"includes"| node_src_fd_users
node_src_rpkm_users -->|"calls"| node_src_registration_svc
node_src_fd_users -->|"calls"| node_src_fd_svc
node_src_fd_svc -->|"uses"| node_src_fd_reg_svc
node_src_registration_svc -->|"uses"| node_src_rpkm_reg_svc
node_src_rpkm_groups -->|"calls"| node_src_groups_svc
node_src_rpkm_houses -->|"calls"| node_src_houses_svc
node_src_groups_svc -->|"shapes"| node_src_models
node_src_fd_svc -->|"shapes"| node_src_models
node_src_registration_svc -->|"shapes"| node_src_models
node_src_auth_routes -->|"uses"| node_utils
node_src_rpkm_groups -->|"uses"| node_utils
node_src_fd_users -->|"uses"| node_utils
node_src_db_index -->|"loads"| node_src_db_schema
node_src_db_schema -->|"includes"| node_identity_schema
node_src_db_schema -->|"includes"| node_houses_schema
node_src_db_schema -->|"includes"| node_games_schema
node_src_db_schema -->|"includes"| node_firstdate_schema
node_src_db_schema -->|"includes"| node_auth_schema
node_src_fd_reg_svc -->|"persists"| node_src_db_index
node_src_rpkm_reg_svc -->|"persists"| node_src_db_index
node_src_groups_svc -->|"transactions"| node_src_db_index
node_src_houses_svc -->|"reads"| node_src_db_index
node_src_auth_routes -->|"auth data"| node_auth_schema
node_src_fd_svc -->|"reads/writes"| node_firstdate_schema
node_src_rpkm_groups -->|"reads/writes"| node_houses_schema
node_src_registration_svc -->|"shared data"| node_identity_schema

click node_src_index "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/index.ts"
click node_src_app "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/app.ts"
click node_src_routes_index "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/index.ts"
click node_src_health "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/health.ts"
click node_src_auth_routes "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/auth/index.ts"
click node_src_firstdate_routes "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/firstdate/index.ts"
click node_src_rpkm_routes "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/index.ts"
click node_src_rpkm_users "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/users.ts"
click node_src_rpkm_groups "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/groups.ts"
click node_src_rpkm_houses "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/houses.ts"
click node_src_fd_users "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/firstdate/users.ts"
click node_src_registration_svc "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/registration.service.ts"
click node_src_fd_svc "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/firstdate.service.ts"
click node_src_fd_reg_svc "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/fd-registration.service.ts"
click node_src_rpkm_reg_svc "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/rpkm-registration.service.ts"
click node_src_groups_svc "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/groups.service.ts"
click node_src_houses_svc "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/houses.service.ts"
click node_src_models "https://github.com/isd-sgcu/fdrpkm2026-backend/tree/main/src/models"
click node_src_db_index "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/index.ts"
click node_src_db_schema "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/index.ts"
click node_identity_schema "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/identity.schema.ts"
click node_houses_schema "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/houses.schema.ts"
click node_games_schema "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/games.schema.ts"
click node_firstdate_schema "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/entries.schema.ts"
click node_auth_schema "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/auth.schema.ts"
click node_utils "https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/utils/index.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_index,node_src_app toneBlue
class node_src_routes_index,node_src_health,node_src_auth_routes,node_src_firstdate_routes,node_src_rpkm_routes,node_src_rpkm_users,node_src_rpkm_groups,node_src_rpkm_houses,node_src_fd_users toneAmber
class node_src_registration_svc,node_src_fd_svc,node_src_fd_reg_svc,node_src_rpkm_reg_svc,node_src_groups_svc,node_src_houses_svc,node_src_models toneMint
class node_src_db_index,node_src_db_schema,node_identity_schema,node_houses_schema,node_games_schema,node_firstdate_schema,node_auth_schema toneRose
class node_utils toneIndigo
```

## Components

### Runtime

Process bootstrap and app assembly

- [Entry](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/index.ts): bun bootstrap · Starts the Bun process and loads the app · `src/index.ts`
- [App](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/app.ts): elysia app · Creates the Elysia application and middleware wiring · `src/app.ts`

### API

HTTP routes organized by domain

- [API Root](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/index.ts): route mount · Mounts the versioned API under /v1 · `src/routes/index.ts`
- [Health](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/health.ts): health route · Simple readiness and smoke endpoint · `src/routes/health.ts`
- [Auth Routes](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/auth/index.ts): host-routed auth · Login and return-target logic derived from Host header · `src/routes/auth/index.ts`
- [First Date](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/firstdate/index.ts): project routes · First Date feature routes and registration entrypoints · `src/routes/firstdate/index.ts`
- [RPKM](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/index.ts): project routes · RPKM feature routes grouped by domain area · `src/routes/rpkm/index.ts`
- [RPKM Users](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/users.ts): route module · User-facing RPKM endpoints · `src/routes/rpkm/users.ts`
- [RPKM Groups](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/groups.ts): route module · Group state and house-choice endpoints · `src/routes/rpkm/groups.ts`
- [RPKM Houses](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/rpkm/houses.ts): route module · House listing and selection endpoints · `src/routes/rpkm/houses.ts`
- [FD Users](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/routes/firstdate/users.ts): route module · First Date user and registration endpoints · `src/routes/firstdate/users.ts`

### Domain

Service and model layer for business workflows

- [Registration](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/registration.service.ts): service · Shared registration orchestration across projects · `src/services/registration.service.ts`
- [FD Service](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/firstdate.service.ts): service · First Date registration and scan workflow logic · `src/services/firstdate.service.ts`
- [FD Reg](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/fd-registration.service.ts): service · First Date registration persistence operations · `src/services/fd-registration.service.ts`
- [RPKM Reg](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/rpkm-registration.service.ts): service · RPKM registration persistence operations · `src/services/rpkm-registration.service.ts`
- [Groups](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/groups.service.ts): service · Transactional group updates and house-choice rules · `src/services/groups.service.ts`
- [Houses](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/services/houses.service.ts): service · House lookup and allocation logic · `src/services/houses.service.ts`
- [Models](https://github.com/isd-sgcu/fdrpkm2026-backend/tree/main/src/models): contracts · Request and response shapes for registrations and groups · `src/models`

### Data

Drizzle schema and database runtime

- [DB](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/index.ts): drizzle runtime · Database connection and runtime selection for Postgres or local PGlite · `src/db/index.ts`
- [Schemas](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/index.ts): drizzle schema · Central schema barrel for all tables and relations · `src/db/schema/index.ts`
- [Identity](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/identity.schema.ts): shared tables · Students, registrations, and travel legs shared by both projects · `src/db/schema/identity.schema.ts`
- [Houses Schema](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/houses.schema.ts): rpkm tables · RPKM group and house-choice tables · `src/db/schema/houses.schema.ts`
- [Games Schema](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/games.schema.ts): checkpoint tables · QR and checkpoint flow tables · `src/db/schema/games.schema.ts`
- [FD Schema](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/entries.schema.ts): firstdate tables · First Date entry and scan tables · `src/db/schema/entries.schema.ts`
- [Auth Schema](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/db/schema/auth.schema.ts): better-auth tables · Authentication tables for Better Auth · `src/db/schema/auth.schema.ts`

### Support

Shared utilities and helpers

- [Utils](https://github.com/isd-sgcu/fdrpkm2026-backend/blob/main/src/utils/index.ts): shared helpers · Auth, error, response, student, group, and storage helpers · `src/utils/index.ts`

## Connections

- 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
