Skip to content

Phase 7: GRIT's own food database - #12

Merged
coderback merged 9 commits into
mainfrom
phase-7
Nov 7, 2026
Merged

coderback merged 9 commits into
mainfrom
phase-7

Conversation

@coderback

@coderback coderback commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

Phase 7 of docs/PLAN_PHASES_5_TO_9.md: GRIT's own food database, search and barcodes on it, and micronutrients from measured values.

What's in it

Food database and importers (packages/food-data, new)

  • Tables for foods, barcodes, servings, the 28 tracked nutrients, other names and each row's source (values per 100 g).
  • Idempotent importers (a re-run changes nothing): USDA FoodData Central (Foundation and SR Legacy as one food per NDB number, FNDDS, branded), Open Food Facts (full export, filtered, or daily changes; label values only), and the UK's CoFID.
  • The same product from two sources is one food, showing the better source.
  • A Food data workflow runs imports from GitHub, using a DIRECT_URL secret, and a food-data CI job tests the package.

Search, barcodes and logging

  • Search runs on GRIT's own data. It handles plurals, synonyms (yoghurt = yogurt) and typos, and ranks the user's own foods first, then their country's, then the best data.
  • Generic foods, dishes and products users have logged or scanned are always searched in full, and other products are sampled. Search p95 is 131 ms locally on 3.4 million foods.
  • Barcodes are looked up locally first (UPC-A, EAN-13, EAN-8 and UPC-E all match), then on Open Food Facts. A product found there is saved.
  • Open Food Facts live search stays as a fallback (FOOD_LIVE_SEARCH) until its full import.
  • App: servings and a Verified label in results, a serving picker, recent and favourite foods keep their food.
  • Smart log matches foods to the database only when the calories agree with the reading. Its estimates now count alcohol's calories.

Micronutrients (measured and label values only)

  • Each log keeps a snapshot of its amount's nutrients.
  • GET /nutrients/day and /nutrients/week against US Dietary Reference Intakes, with how much of the day's calories have data. Today is never called low.
  • The app gets a Nutrients screen, an "Often low this week" card on Stats, and a Food data sources screen. The coach gets a context line and a "running low" check-in.
  • A log only counts toward a food's shared popularity with "Improve GRIT's food data" on.

Docs: ROADMAP, the plan's as-built notes, README, trust and safety (licences, wording, consent) and the device testing checklist.

Database

Two additive migrations, both already applied to Supabase and recorded in _prisma_migrations:

  • 20261109120000_phase7_food_database
  • 20261109130000_food_core_search

The food tables are empty in production until the import runs.

Checks

  • Backend: migrations from scratch match the schema; prettier, eslint and tsc clean; 223 unit and 94 database tests; build.
  • Importers: lint and typecheck clean; 45 unit and 13 database tests.
  • App: typecheck clean; lint with 0 errors and the 5 existing warnings; 61 tests.
  • Browser:
    • new Phase 7 test 20/20, including Smart log against the live AI;
    • strength workouts 29/29;
    • earlier food-related suites pass.
  • Review: an independent code review's 16 confirmed findings are fixed.

After merging

  1. Deploy the backend before a new app build, since the app sends fields older backends reject.
  2. Add the DIRECT_URL repository secret (Supabase session pooler, port 5432), then run the Food data workflow for fdc and cofid (about 90 MB, within the Free plan).

Not in this PR:

  • The full Open Food Facts and USDA branded imports (12–14 GB, a Supabase plan decision).
  • Canada's CNF, France's CIQUAL and Finland's Fineli (their sites block servers).
  • The lawyer's review of Open Food Facts' licence (ODbL).

- Migration for GRIT's food tables (foods, barcodes, servings, nutrients,
  names, sources) with a trigram search index, nutrient snapshots on
  food logs, and food links on remembered and favourite foods
- Food search from GRIT's own data first, barcodes looked up locally
  (UPC-E aware) and saved when found live, Smart log matched to measured
  foods, and a fix for alcohol's calories in estimates
- Nutrients by day and week against US Dietary Reference Intakes, a coach
  line and a 'running low' check-in
- App: serving picker, Verified labels, Nutrients screen, often-low card
  and a food data sources screen
- Start of the importer package (writer and shared record code)
- Search keeps exact matches and the user's memories when a ranking
  query fails or times out, puts the user's own foods first after the
  typo search, and matches memories on their name
- Smart log checks calories for branded matches too (a dry pack isn't
  cooked rice), matches plain foods on whole words within 20%, links
  remembered foods to the database, and keeps the link when an older app
  doesn't send it; it now sends the phone's country
- A calorie edit scales a log's grams with its nutrient snapshot
- Logs only raise a food's shared popularity with 'Improve GRIT's food
  data' on
- Food names are capped at 200 characters, serving labels lose
  weight-first text, and small nutrient amounts keep two figures
- The coach isn't told 'nothing often low' on too little data, and the
  Nutrients screen shows sodium and saturated fat on days without
  vitamin data
- yoghurt = yogurt, courgette = zucchini, prawn = shrimp and a few more
  single-word synonyms in search and Smart log matching
- Food data sources lists USDA, Open Food Facts and the UK's CoFID; the
  other national tables join when they're imported
- ROADMAP, the Phase 7 plan's as-built notes, README, trust and safety
  (licences, nutrient wording, food-data consent) and the device testing
  checklist
A common word matches hundreds of thousands of products, and ranking them
all took over a second (p95 1.3 s on 3.4 million foods). Foods now have a
coreText column, searched in full: generic foods, dishes, and products
GRIT users have logged or scanned. Other products are sampled, and the
user's own foods are always included. Ranking gives more weight to
quality and verified data. p95 is now 131 ms on 3.4 million foods.

The food database tests use made-up barcodes, so real imported data
can't collide with them.
@vercel

vercel Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
grit-demo Ready Ready Preview Nov 7, 2026 1:25am UTC

The test logged its workout at 9am today, which the helper moves to
yesterday when run before 9am UTC, before the challenge had started.
It now logs the workout a minute ago, or at midnight UTC.
@coderback
coderback merged commit c414a2f into main Nov 7, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant