MacroMath
Reference

Data model

The ten tables of the PostgreSQL schema, relationships, food_ref_id and RLS isolation.

The relational schema lives in PostgreSQL (Supabase) and was designed incrementally through thirteen structured migrations (001 to 013). Every table is protected by Row-Level Security policies. The later migrations (011–013) added the Premium subscription and Stripe columns and a unit field on meal items — see Backend & Supabase.

The tables

TablePurposeKeysRelationship
usersProfiles, targets and settingsid (PK)System base
food_itemsCatalog and nutrient density per 100 gid (PK)Referenced by meal_items
mealsChronological log of daily mealsid (PK), user_id (FK)N → 1 with users
meal_itemsFoods in each meal and their weightid (PK), meal_id (FK), food_id (FK)N → 1 with meals
pantry_itemsInventory, quantities and thresholdsid (PK), user_id (FK)N → 1 with users
diet_plansAI-generated meal plansid (PK), user_id (FK)N → 1 with users
weight_historyWeight time seriesid (PK), user_id (FK)N → 1 with users
shopping_listShopping items (manual or predictive)id (PK), user_id (FK)N → 1 with users
water_logDaily hydration logid (PK), user_id (FK)N → 1 with users
feedbackBug reports and suggestionsid (PK), user_id (FK)N → 1 with users

How they relate

  • users is the base — nearly every table links to it via user_id.
  • Each meals groups several meal_items; each item references a food_items through food_id.
  • pantry_items feeds the shopping_list and gives context to the diet_plans.
  • weight_history and water_log feed the Progress tab.

Canonical food identifier

Integrity across modules (Pantry, Diet, Foods) rests on the food_ref_id field, whose prefix makes the data's origin explicit:

off:<barcode>    → Open Food Facts (international)
local:<slug>     → national catalog

See Food sources.

RLS isolation

Every table is shielded by Row-Level Security. The expression evaluated on the server absolutely isolates access to each user's records:

user_id = auth.uid()

This guarantees multitenant isolation at the database engine level — one user can never access another's data. See Security.

Where the data lives

LayerRole
Supabase (PostgreSQL)Single source of truth.
Hive (local)Instant cache and offline state.
External sourcesLocal PT database, FatSecret and Open Food Facts for foods.

On this page