Personal project · Running daily

Health OS, a pipeline for my own health data

I wanted my Apple Watch data in a database I control, so I built a small pipeline for it on Supabase. It syncs every hour, keeps every version of every day, and tells me whether to rest or push.

SupabasePostgresEdge FunctionsRow-level securityWindow functionsPoint-in-time historyCI tests
Role
Built on my own
When
Running daily
Type
Personal project · live, used every day
Stack
Supabase, Postgres, Edge Functions (Deno), Node tests, GitHub Actions
Hourlysync from Apple Health3timestamps per day: measured, sampled, received16tests run in CI on every push2FArequired for private data

Apple Watch data is exported hourly by Health Auto Export to a Supabase Edge Function that checks a key and upserts one row per day into a raw Postgres table, recording when the data was measured, sampled and received. A trigger keeps every changed version of a day. A clean view flags out-of-range values, a derived view computes 7-day trends and recovery scores with window functions, an insights view adds a stress indicator and a rest, steady or push plan, and a public view exposes only safe metrics to the dashboard and the portfolio badge. Private data sits behind row-level security and two-factor sign-in.

The problem

Apple Health exports don't de-duplicate. If the same day is sent twice the totals double, and nothing tells you where a number came from. And when a late sync corrects yesterday's numbers, the old values are simply gone, so there's no way to know what the dashboard said at the time.

What I built

  • Ingest. A Supabase Edge Function that upserts one row per day, so re-sending the same data changes nothing. It adds up totals, averages rates, counts only asleep sleep stages, and records three timestamps: the day measured, the latest sample time from the watch, and when the server received it.
  • Point-in-time history. A trigger keeps a new version of a day only when its values actually change. health_as_of(t) returns the data exactly as it was at time t, so anything computed “as of” a date can’t see corrections that arrived later.
  • Clean and derived views. The clean view flags out-of-range values without deleting them. The derived view computes 7-day rolling averages by calendar date, so a missing day doesn’t stretch the window, plus recovery and momentum scores, all with window functions.
  • A daily plan. An insights view compares today’s HRV, resting heart rate, sleep and breathing rate with my previous 30 days to give a stress indicator, then a rest, steady or push day and a step target. A day without enough signals gets no score and is never a push day.
  • Security. Row-level security and two-factor sign-in on everything private. Visitors read one public view of safe columns; breathing rate feeds the stress score but is never exposed.
  • Tests. 16 tests run every migration against an in-process Postgres and check the formulas against hand-worked inputs, the access rules and the history. GitHub Actions runs them on every push.

From the code

Point-in-time reads are one query over the version table: the latest version of each day that had arrived by the requested time. Row-level security keeps it owner-only.

migrations/007_timestamps_and_history.sql
create or replace function public.health_as_of(as_of timestamptz)
returns table (date date, version int, received_at timestamptz, vals jsonb)
language sql stable security invoker as $$
  select distinct on (v.date) v.date, v.version, v.received_at, v.vals
  from public.health_daily_versions v
  where v.received_at <= as_of          -- nothing that arrived later
  order by v.date, v.version desc;    -- newest version per day
$$;

Result

New data shows up seconds after each sync, any number can be rebuilt from the raw rows, and I can show what any day looked like at any moment. The tests earned their place early: they caught that re-running the first migration after a later one failed, before it reached the live database.

Decisions worth explaining

All the logic is in SQL views instead of the app. The SQL is heavier, but the page doesn’t calculate anything, so the numbers are always reproducible.

Missing data means no score, not a zero. Counting a night without the watch as zero stress would push me toward a hard day on no evidence, so the stress indicator needs HRV, or resting heart rate plus one more signal, before it says anything.

Stack

SupabasePostgresEdge FunctionsRow-level securityWindow functionsDenoGitHub Actions