Skip to main content

Data model

One Postgres database is the system of record for every tenant: relational tables, the Apache AGE knowledge graph, and the pgvector indexes. 58 tables across twelve schema files in packages/db/src/schema/. Roughly a third of them are deliberately not tenant-scoped — see Multi-Tenancy for why that distinction exists and how it is enforced.

Schema files​

FileTablesPurpose
tenancy.ts6Tenants, module entitlements, module metadata, custom fields, audit log, platform admins
org-structure.ts9Departments, programmes, branches, batches, staff categories, class sections, subjects, subject prerequisites, subject outcomes
persons.ts11The canonical Person entity plus addresses, sensitive identifiers, enrollment, employment, relationships, qualifications, experience, credentials, skills, projects
attendance.ts4Student per-class attendance, staff biometric attendance, faculty assignments, delegation requests
assessment.ts3Assessment records, cached semester GPA/arrears summary, course feedback
timetable.ts2Rooms, timetable slots
assets.ts3Vehicles, hostel rooms, library books
financial.ts4Budget heads, suppliers, financial requests, generic approval steps
platform-support.ts2Polymorphic file attachments, notifications
reference-data.ts6Global demographic lookups (nationality, religion, community, caste, mother tongue, blood group)
auth.ts7Better Auth's own tables, generated by better-auth generate
legacy.ts1legacy_records — the raw imported CampusStack browser (see Getting Started)

Tenancy and metadata​

tenants — one row per institution. Onboarding a new institution is one row here, one Better Auth organization row, and one module_entitlements row. No schema change, no new deployment. Carries regulatoryFramework (defaults to naac_nba_nirf) and betterAuthOrgId, which is the single link between this table and Better Auth's own organization model.

module_entitlements — which of the 20 modules a tenant has licensed. This is the packaging/pricing mechanism, not a separate system. Has a read API and no write API yet.

module_definitions — the metadata-driven engine's core table. Each of M1–M20 is a row whose definition JSONB holds entities, fields, relationships, and permission rules. Seeded by seed.ts. The storage layer is real; the generic consumer that renders routes and forms from this metadata does not exist yet — see What's Built Today.

custom_field_definitions — the mechanism that makes the schema genuinely non-restrictive. An admin adds a row through the settings UI (no migration, no deploy) and it becomes an editable field on that entity's form, stored in the target row's attributes JSONB column and validated at write time against a Zod schema generated from this definition. Supports four entity types (person, studentEnrollment, staffEmployment, attendanceRecord), five field types (text, number, date, boolean, enum), a required flag, and a visibility of public / institution / private — the field-level privacy control M10 promises.

audit_log — populated by mutation-service.ts, never by database triggers. Every row records entityType, entityId, action, before/after JSONB, and the acting person or auth user. Written inside the same transaction as the data write it describes, so the two cannot diverge. The choice of an app-level chokepoint over triggers is deliberate and documented in the source: triggers would be fully automatic but are PL/pgSQL magic outside a TypeScript-first stack, while a shared chokepoint stays consistent with every other enforcement mechanism here and is still atomic.

The canonical Person entity​

persons is the one entity every module reads and writes — students, faculty, staff, alumni, guests, and parents all live in it, discriminated by person_type. The design principle is that typed columns are kept to what is genuinely universal and constantly queried; everything long-tail flows through attributes JSONB validated against that tenant's custom_field_definitions.

Two details worth knowing:

  • search_vector is a generated, stored tsvector column over first/middle/last name, with a GIN index. Postgres full-text search is effectively free to add now versus reaching for a search service later for something this basic. There is no native tsvector type in drizzle-orm 0.38, so it is declared via customType.
  • person_sensitive_identifiers is split into its own table (Aadhaar, ABC ID, passport, PAN) specifically to make field-level privacy possible. Full field-level ACLs are a fast-follow; what matters is that the table boundary exists now, because retrofitting it later is painful.

The demographic reference FKs are all nullable on purpose — not every institution tracks all of them, and the custom-field mechanism covers whatever the typed columns do not.

Enrollment and employment​

student_enrollments links a person to a branch, an optional batch, and an optional class section. Note that batch_id is nullable, and that is a data-driven decision rather than a loose one: the real legacy student_admission_master has it nullable too, and 9,021 of 12,954 real historical rows have it NULL. Making it required would have silently dropped 70% of real enrollment history on import. student_status carries the ten legacy statuses (Continuing, Discontinued, Deceased, Debarred, Completed, Long Absent, Drop Out, Withheld, Break of Study, Wrong Entry).

staff_employment is one row per person (unique person_id) with department, staff category, designation, joining/relieving dates, and a working_status of working / relieved / wrong_entry.

Both are soft-deleted — institutional records are almost never physically removed, for compliance and audit reasons ("we need last year's roster for NAAC evidence"). There is deliberately no hard-delete helper in mutation-service.ts.

Academic structure​

departments → programmes → branches → class_sections → subjects, all modeled on their legacy counterparts (master_department, master_branch, master_batch, etc.).

class_sections is the concrete "class" unit that attendance and subjects hang off: branch + batch

  • semester + section label + academic year, unique on all five within a tenant. It is implicit in the legacy schema's time_table_master_* tables rather than being a distinct table there — this build makes it explicit.

subjects started as a lightweight curriculum entry — code, name, credits, core/elective — to make M0's "add curriculum" work as base data. The M0 canonical-schema expansion below added the data shape (category, regulation year, marks thresholds, a prerequisite graph, CO1–CO6 outcome mapping) M1 Curriculum's UI will eventually read; it is still not full M1 — no what-if simulator, no version control/diff view on syllabus revisions.

Attendance​

There are two independent attendance concepts here, and conflating them is a common mistake:

attendance_records — a student's presence in a specific class. One row per student per class section per date per subject (periodRef holds the subject code). This is period-level, not a collapsed daily status: attendance follows the real timetable, so a student marked present in period 1 and absent in period 4 has two rows. Where the legacy data has multiple periods of the same subject on the same day, the importer collapses them into one row by majority vote, with ties resolving to present. Uniqueness is enforced on (personId, classSectionId, date, periodRef).

staff_attendance_records — a staff member's own presence, biometric in/out, not tied to any class section. One row per (person, date, session) where session is FN/AN (forenoon/afternoon). The legacy source table only logs presence events — there is no explicit "absent" row for a missed session — so the importer never produces absent; that status exists only for a future manual-correction write path.

Authorization data also lives here. faculty_subject_assignments is not just a scheduling convenience: whether a faculty member may mark attendance for a class section is an authorization fact in the database, checked by the attendance route before allowing a write. It is unique on (tenantId, facultyPersonId, subjectId, classSectionId, academicTerm).

attendance_delegations is the "ask another faculty to take my attendance" feature — a plain table with a simple accept/decline API. It is deliberately not routed through the workflow engine: a same-day delegation request is not a multi-month durable process, and forcing it through DBOS/XState this early would couple M0 to a service that is not proven yet. It would graduate to the workflow service only if delegation grows into multi-level approval chains.

Reference data​

Six small lookup tables (ref_nationalities, ref_religions, ref_communities, ref_castes, ref_mother_tongues, ref_blood_groups) that are global, not tenant-scoped — they are platform-managed and rarely change. This is why they are excluded from RLS and why /reference-data is served from an in-memory cache rather than being re-queried per request.

The knowledge graph​

The graph is derived from these tables and kept in sync, not hand-maintained:

  • Vertices: Person, plus an allowlisted set of org vertices — Department, Branch, Batch, ClassSection, Subject
  • Edges: Person -[:ENROLLED_IN]-> Branch, Person -[:TEACHES]-> Subject (with classSectionId as an edge property)

Every vertex and edge carries tenantId as a property, and every query matches on it inside the pattern rather than filtering afterwards. Cypher labels cannot be parameterized, so upsertOrgVertex's label is constrained to a compile-time allowlist rather than accepting an arbitrary string — a correct constraint for a stable ontology, not a limitation.

The M0 canonical-schema expansion​

A field-by-field check against KIOT's Master Requirements document (derived from 64 real KIOT paper forms and ERP exports — docs/10 260721 MasterRequirements (V2.0).docx.pdf) found the schema above covered roughly 8% of its 58-item M0 section, with the rest split between partial coverage and outright gaps. This section closed that gap — 22 new tables plus nullable columns on subjects, persons, and student_enrollments — while deliberately not transcribing the source document literally. It proposes one table per paper form (separate IAT/ESE/arrear tables; five separate tables for skills/achievements/participation/certifications/publications); building that literally means rebuilding near-identical plumbing five more times as more modules land. The design principle throughout is generic and reusable over narrow and literal:

  • assessment_records is one table for every exam type (assessmentType: iat1/iat2/ iat3/ese/assignment/other), with attemptNumber covering re-attempts/arrears directly — not three separate tables. student_semester_summary is a cached rollup (SGPA/CGPA/arrears count), written once per semester-close rather than computed live on every read.
  • person_credentials is one table for what the source document asks as five separate feature areas (skills, achievements, participation, certifications, publications) — a credentialType enum, verificationStatus, and a verifiedCredentialId column that is a deliberate forward reference to apps/credentials (this repo's W3C Verifiable Credentials issuer — see Credentials — currently real but unwired to anything). The "verification badges" the requirements document asks for are literally what that service exists to produce; this column is the join point once it's wired up. person_skills is kept separate — self-declared, structured facts meant to be queried directly (an AI matching or placement-eligibility filter reads this table directly, not JSONB), not evidence records.
  • approval_steps is one table, polymorphic on (requestType, requestId), not a hardcoded approval-chain column sequence per form. Eight of the source document's eleven Phase-1 modules explicitly route through "the workflow engine" with slightly different role chains (Incharge → HoD → Principal → Dir-Admin → Secretary → Treasurer → Exec. Chairman, for the financial forms) — a "no-code workflow builder... configurable without engineering support" (the document's own words) is only possible if the chain is data in a table like this, not columns. Today it points at financial_requests.id only; any future approvable entity (a guest visit, a MAP milestone) reuses the same table rather than inventing its own chain.
  • person_relationships models a guardian as a real persons row (PERSON_TYPES already includes "parent") linked via relationshipType, not father_name/mother_name text columns on persons. This is what lets a parent eventually log into a parent portal as themselves.
  • documents and notifications aren't asked for by the source document at all, but are implied everywhere it needs them — guest-logistics receipts, admission uploads, and MAP deliverables all need a file reference (nothing in the stack handles one today, no object storage is wired up anywhere); alert-driven modules from bus delays to fee reminders all assume something logs that a notification fired. Both are polymorphic/generic for the same one-table-not-many reason as approval_steps.

New tables, grouped by what they'll eventually back:

GroupTablesBacks
Person completenessperson_relationships, person_qualifications, person_experience, person_credentials, person_skills, person_projectsM10 Digital Profile, M11 Parent Module, M9 Placement
Curriculum enrichmentsubject_prerequisites, subject_outcomes (+ columns on subjects)M1 Curriculum, M6 Accreditation
Assessmentassessment_records, student_semester_summary, course_feedbackM1's assessment half, M6 evidence
Timetablerooms, timetable_slotsM2 Calendar & Timetable
Campus assetsvehicles, hostel_rooms, library_booksM13 Bus, M14 Hostel, M16 Library (inventory only — issue/return/allotment workflows are each module's own build)
Financialbudget_heads, suppliers, financial_requests, approval_stepsM4 Workflow Engine (data shape only — SLA timers, auto-escalation, mobile approval, and the no-code form builder itself remain M4's own build, not conjured by these tables existing)
Cross-cuttingdocuments, notificationsEvery module that uploads a file or sends an alert

All of it is purely additive: new tables and nullable columns only, nothing existing renamed, dropped, or made required. Every new table follows mutation-service.ts's shape requirement (id, tenant_id) and is in TENANT_SCOPED_TABLES, so RLS covers it identically to every table above — verified directly against the real database (pg_tables/pg_policies row counts match, and a SET LOCAL app.current_tenant_id to a nonexistent tenant returns zero rows from a new table the same way it does for departments in rls.test.ts).

What this pass deliberately does not include: write APIs, admin UI, or RBAC statements (apps/api/src/auth.ts) for any of these tables. That's real, separate work per module — this pass only closes the schema gap, on the theory that getting the shared data model right once is worth doing before building the eleven modules that will each read and write it.

Rules to follow when extending the schema​

  1. Every new tenant-scoped table carries tenant_id, and gets added to TENANT_SCOPED_TABLES in schema/index.ts. That one array drives RLS setup, force, hardening, and the policy expression — adding a table anywhere else means it silently has no isolation.
  2. Writes go through mutation-service.ts. Direct inserts skip both the audit trail and graph sync, and the audit trail is the only reason the system can answer "who changed this".
  3. Reads go through withTenant(). Reading through the raw db object means no app.current_tenant_id is set, and RLS denies every row — which fails closed, but shows up as an empty result set rather than an error, so it is worth checking first when a query "returns nothing".
  4. Prefer nullable over required when modeling legacy data. Two schema decisions here exist solely because a real institution had NULLs where the design expected values.