Schema Overview
SECTION 29 — DATABASE SCHEMA OVERVIEW
29.1 Core Tables (Neon / PostgreSQL via Prisma)
The following table list covers all primary data entities. This is a schema overview — the developer should implement the full Prisma schema based on these definitions. The primary isolation key for all tenant-scoped data isorg_id (not editor_id alone). All tables containing document, user, or billing data must include org_id. The editor_id on the organisations table is the tenancy assignment link — queries are scoped via JOIN or predicated on org_id IN (SELECT id FROM organisations WHERE editor_id = current_editor_id).
super_admins
Stores the Super Admin account(s). Provisioned at deployment time — no in-app registration.
There is typically only one Super Admin record. The schema supports multiple if needed (e.g. platform co-owners), but additional records must be provisioned via script, never through the app UI.
organisations
The tenant container. Created by a Teacher on self-registration. Approved and mapped by the Super Admin.
editors
Stores Editor accounts. Created by Super Admin — no self-registration.
teachers
Stores Teacher (subscriber) accounts. Self-registered — no Editor invite link. Status gated by Editor authorisation after Super Admin approves the Organisation.
sub_members
Stores sub-member accounts (Teacher’s team members). Pending Teacher authorisation upon registration.
documents
Master table for all document records (Newsletter, Compilation, Magazine, Mindmap).
Unique constraint: (org_id, publication_id, doc_type_name, document_date) — enforces duplicate document prevention (Section 9A.7).
document_versions
Version history records for each document.
document_comments
Linear comment thread per document for communication and dispute resolution (see Section 4.11.3).
Indexing: Composite index on(document_id, created_at ASC)for efficient chronological retrieval. Index onauthor_idfor per-user comment queries.
pipeline_events
Tracks all SLA pipeline events for each document.
pdf_outputs
Records of generated PDF files.
billing_cycles
Monthly billing periods per Teacher. Managed by the Super Admin.
invoices
Invoice records. Generated and approved by the Super Admin.
images
Image metadata records.
ad_banners
Teacher’s ad banner library.
templates
Organisation-wide template library. Templates are shared across the organisation — not personal documents of individual roles. Teachers, Editors, and Sub-members see a unified template view from their RBAC dashboard.
team_activity_logs
Sub-member action logs visible to parent Teacher.
translations
Parallel translation document records.
audit_logs
System-wide audit trail with Super Admin managed deletion capability. Every significant event at every role level is recorded here. Records can only be deleted by the Super Admin (manually or via automated archival schedule). The deletion process archives logs to Google Drive and downloads a CSV to the Super Admin’s browser before final deletion from the database.
Deletion policy: Only the Super Admin can delete audit log records. Deletion options:
last-hour, last-day, last-week, last-month, last-year, or a specific date range. Before deletion: (1) archive to Google Drive as CSV, (2) download CSV to Super Admin’s browser, (3) then delete from Neon database. Automated deletion can be configured in Super Admin Settings with archival-before-delete as a mandatory pre-condition.
editor_org_assignments
Historical record of all Organisation → Editor assignment and reassignment events. Allows Super Admin to see the full assignment history of any Organisation.
atomic_uid_log
Backend-maintained log of all generated Atomic UIDs to ensure uniqueness across the system. See Section 11A for UID format specification.
prepaid_invoices
Tracks PREPAID platform fee invoices issued by the Super Admin. Separate from usage-based POSTPAID invoices.
billing_prepaid_config
Per-Teacher PREPAID billing configuration managed by the Super Admin.
publications
Publication streams per Organisation. Each org has at least one publication (default: CURRENT-AFFAIRS).
Constraint: (org_id, name) must be unique. At least one publication per org (default cannot be deleted).
publication_document_types
Custom document types configured within each publication stream.
Constraint: (org_id, name) must be unique across all publications. This enforces cross-publication naming uniqueness within an org.
support_tickets
Customer support tickets raised by tenants. See Section 22A.
ticket_messages
Conversation thread messages within a support ticket.
Indexing: Composite index on (ticket_id, created_at ASC) for chronological retrieval.
ticket_attachments
File attachments on tickets (initial submission attachments).
compliance_records
Per-publication-stream compliance tracking for evader detection (Section 23.8).
discount_commitments
Tracks 6-month and 12-month PREPAID discount commitments per Organisation.
29.2 v5 Additions
The tables below were added in PRD v5.0.0. Existing tables also gained columns — see §29.3.qa_items
Q&A items generated via the OpenRouter service (§22B). One row per accepted Q&A item.
revision_screenshots
PNG screenshots attached to revision flags via the §13.3.4A wizard. Stored as base64 in Neon DB. Auto-purged 30 days after resolution.
Constraint: per(document_id, revision_id),COUNT(screenshot_id) <= 5. Enforced via insert trigger.
openrouter_config
Single-row table (or system-settings JSON) holding global model resolution and fallback chain. Per-Org overrides live in organisations (see §29.3).