ComuniTEA’s backend runs on Supabase — a hosted PostgreSQL platform with built-in Auth, Storage, and Row Level Security (RLS). The database stores everything that must survive app reinstalls or sync across devices: user profiles, custom pictogram libraries, sentence logs for the personalised predictor, guided activity sequences, child developmental profiles, and multi-user team memberships. All static vocabulary and exercise content lives exclusively in the TypeScript bundle (offline-first), so Supabase is only contacted for personalisation, reporting, and multi-role collaboration features.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ElthonJohan/comunitea/llms.txt
Use this file to discover all available pages before exploring further.
Core Tables
profiles
Created in init_schema.sql. Every authenticated Supabase user gets a matching row here, automatically created by the handle_new_user() trigger.
SELECT— user can only read their own row (auth.uid() = id)UPDATE— user can only update their own row (auth.uid() = id)
INSERT policy on profiles for regular users — rows are created exclusively by the handle_new_user() trigger running as SECURITY DEFINER. This prevents orphan profiles and duplicate rows.categories
User-defined pictogram categories. The app’s built-in vocabulary categories are hard-coded in TypeScript; this table stores only the parent labels for custom pictogram collections.
ALL operations allowed only when auth.uid() = user_id.
custom_pictograms
Stores pictograms uploaded by the family or therapist as part of the “Mi mundo en fotos” feature. Images are stored in the pictograms Storage bucket; image_url is the public URL.
20260303000002 adds a second category reference that maps directly to the app’s built-in vocabulary category IDs:
ALL operations allowed only when auth.uid() = user_id.
sentence_log
Records every sentence the child plays. The pictogram_ids array is unnested by get_usage_report() to find the child’s most-used pictograms.
20260331000001): A purge_old_sentence_logs() function deletes rows older than 90 days. When pg_cron is available on the Supabase project it is scheduled to run at 03:00 UTC daily; otherwise it must be called manually or via an Edge Function.
- Base policy (own data):
ALLoperations whenauth.uid() = user_id - Team read policy (Phase 7):
SELECTalso allowed whenhas_report_access(user_id)returnstrue
tasks
User-defined routine tasks with ordering and completion tracking.
updated_at trigger (set_updated_at()) automatically refreshes the timestamp on every UPDATE. RLS: ALL for own user.
usage_stats
Event log for reporting. Tracks four event types that drive the usage report.
- Base policy:
ALLfor own user - Team read policy (Phase 7):
SELECTalso allowed viahas_report_access(user_id)
parental_settings
One row per user. Stores the daily screen-time limit and a rolling counter of seconds used today. The counter resets automatically in application logic when date_today differs from the current date.
ALL for own user.
child_profiles
Clinical and communicative profile of the child, captured during onboarding. This is the central table for child-specific configuration.
20260423120000 adds a free-text array for important people’s names (used to populate the PERSONAS_ROW with personalised pictograms):
ALL for own user (auth.uid() = user_id).
child_progress
Gamification progress table — one row per child_profile. Tracks XP, level, daily sentence count, activity streak, and overall accuracy.
ALL when a child_profiles row exists with cp.user_id = auth.uid() — access is checked through the parent profile, not directly.
guided_activities and activity_sessions
Adults create structured activity sequences (guided_activities); each run is recorded in activity_sessions for the progress report.
ALL for own user on both tables.
child_team and share_tokens (Phase 7 — Multi-role)
Supports multiple adults (parents, therapists, teachers) collaborating on a single child profile.
child_team:
- The
invited_byowner can perform all operations. - The invited
user_idcan read their own pending/active row.
share_tokens: ALL for own user_id.
Row Level Security (RLS)
Every table in ComuniTEA has RLS enabled. The guiding principle is strict isolation by default, opt-in sharing through explicit team membership.RLS is always on
CREATE TABLE statement is followed by ALTER TABLE ... ENABLE ROW LEVEL SECURITY. No table is left without policies.Own-data baseline
auth.uid() matches the user_id (or id for profiles). Anonymous or mismatched users get zero rows.Team read escalation
has_report_access(p_owner_id) — a STABLE SECURITY DEFINER helper function that checks whether the calling user is an active team member with can_view_reports = true for any child profile belonging to p_owner_id. This function is used in SELECT policies on usage_stats, game_sessions, and sentence_log.Storage Buckets
pictograms bucket is public — all uploaded images are accessible via their public URL without authentication. Upload, update, and delete are restricted to the owning user via auth.uid() = owner policies on storage.objects.
Triggers and Functions
handle_new_user() — Auto-profile creation
Fires AFTER INSERT ON auth.users and inserts a matching row into public.profiles, copying first_name, last_name, and avatar_url from the user’s raw_user_meta_data. The function runs as SECURITY DEFINER so it can insert without an INSERT RLS policy being needed for end-users.
set_updated_at() — Automatic timestamp trigger
Used on the tasks table to keep updated_at current on every update:
get_usage_report(p_days) — Reporting aggregate
Returns a jsonb object with usage statistics for the authenticated user over the last p_days days. Includes event-type counts, hourly distribution, top 10 pictograms by frequency (derived from sentence_log), and a human-readable Spanish description.
get_daily_usage(p_days) — Daily breakdown
Returns one row per day with session and interaction counts — used by the report chart component:
accept_team_invite(p_token) — Team membership
Accepts a pending child_team invitation by token, setting user_id = auth.uid(), status = 'active', and accepted_at = now(). Raises an exception if the token is invalid or already used.
has_report_access(p_owner_id) — Team RLS helper
purge_old_sentence_logs() — TTL cleanup
Deletes sentence_log rows older than 90 days. Scheduled via pg_cron at 0 3 * * * (03:00 UTC) when the extension is available.
Migration History
init_schema.sql — Foundation
profiles, categories, custom_pictograms. Adds the pictograms Storage bucket and its access policies. Defines the handle_new_user() trigger.20260303000001 — Phase 1: Tasks & Usage Level
level column to profiles (default 'BASICO'). Creates tasks table with ordering and completion tracking. Creates usage_stats table for the four core event types.20260303000002 — Phase 4: Custom Pictograms Category Key
category_key text column to custom_pictograms so custom pictograms can be associated with built-in vocabulary category IDs (e.g., 'comida', 'baño').20260303000003 — Phase 7: AI Expand Stat
'ai_expand' event type to the usage_stats.event_type CHECK constraint.20260304000001 — Phase 8: Sentence Log
sentence_log table with pictogram_ids text[] and a user/date compound index.20260307000001 — Parental Settings
parental_settings with daily screen-time limit and rolling usage counter.20260307000002 — Report Usage Function
get_usage_report(p_days) — the main reporting stored procedure.20260331000001 — Phase 0: Sentence Log TTL
idx_sentence_log_created_at index. Defines purge_old_sentence_logs() and optionally schedules it via pg_cron.20260331000002 — Phase 0: Fix Storage Policies
20260331000003 — Phase 1: Child Profiles
role column to profiles. Creates child_profiles table with DSM-5 diagnosis, communication level, environments, and sensory configuration.20260331000004 — Phase 3: Parental Sensory Config
20260331000005 — Phase 4: Guided Activities
guided_activities (JSONB steps) and activity_sessions (completion records).20260331000006 — Phase 5: Game Sublevel
game_sessions table for tracking sublevel results and accuracy percentages.20260331000007 — Phase 6: Report Improvements
daily_usage view and get_daily_usage(p_days) / get_game_history(p_limit) functions.20260331000008 — Phase 7: Multi-role
child_team and share_tokens. Defines accept_team_invite() and has_report_access(). Adds team-read SELECT policies to usage_stats, game_sessions, and sentence_log.20260331000009 — Phase 8: AI Engine
20260423120000 — Child Profiles: Important People
important_people text[] column to child_profiles for storing the names of the child’s key relationships (used to populate PERSONAS_ROW with personalised pictograms).Quick Reference — Table Index
| Table | Primary concern | RLS scope |
|---|---|---|
profiles | Authenticated user identity & settings | Own row |
categories | Custom pictogram category labels | Own user |
custom_pictograms | Uploaded pictogram images & audio | Own user |
tasks | Daily routine task list | Own user |
usage_stats | Event log (taps, sentences, sessions) | Own user + team read |
parental_settings | Screen-time limit & daily counter | Own user |
sentence_log | Phrase history for predictor & reports | Own user + team read |
child_profiles | Child’s clinical & communicative profile | Own user |
child_progress | Child XP, streak, achievements | Via child profile |
guided_activities | Adult-created activity sequences | Own user |
activity_sessions | Activity run records | Own user |
child_team | Multi-adult collaboration memberships | Owner + invited member |
share_tokens | Time-limited report share links | Own user |