← Back to Projects
FoodTracker

FoodTracker

A food diary that builds a personal daily target from a few questions, then shows how your meals measure up across calories, macros, vitamins and minerals.

Next.jsReactTypeScriptTailwind CSSZodDrizzle ORMPostgreSQLBetter AuthDockerGoogle Cloud RunPlaywright
11
browser tests, run in CI against a real database
13
unit test files covering plans, meals and reports
5
database migrations, run before each deploy

The problem

Most food trackers ask you to log everything before they give you anything back, and they feel like a test you can fail. FoodTracker starts with a short guided plan, lets you try it as a guest, and then turns the meals you log into weekly, monthly and yearly views against your own targets.

What it does

A Plan Built Around You

Six short steps turn your goal, weight and activity into daily calorie, protein, carbohydrate and fat targets, with an estimated target date.

Detailed Onboarding

A guided setup covers your body basics, goal and pace, how the journey has felt, and the nutrients you want to focus on. You can do it as a guest and register afterwards.

Meal Logging

Search for foods, pick a serving or enter grams, build a meal from several foods, and edit or delete it later.

Nutrition Reports

See fibre, fats, vitamins and minerals for a week, month or year against targets, with a trend and the foods that contributed most.

Progress Dashboard

Daily averages, period totals and your direction towards your target weight, without streaks or scores.

Accounts and Settings

Choose kg or lb, kcal or kJ and a colour theme, change your password, or schedule your account for deletion.

See it working

FoodTracker's weekly overview showing a daily calorie average against a 1,200 kcal target, period totals, protein, carbohydrate and fat progress bars, and a weight direction line towards a goal weight.
The weekly overview: calories, macros and progress towards your goal weight.
The nutrition detail page with a list of fats, a selected fibre nutrient showing a daily bar chart across the week against a 30 g goal, and top food sources.
Nutrition detail: every nutrient against its target, with a daily chart for the one you pick.
An Add food dialog with the search term chicken breast and a list of matching foods.
Searching for a food to add to a meal.
The Add meal page with three foods (oats, banana and soy yogurt) with gram amounts, and a summary of the meal's calories, protein, carbohydrate and fat that has not been saved yet.
Building a meal: amounts in grams, with the meal's totals updating before you save.
The goal step of FoodTracker's guided setup, with options to lose, maintain or gain weight, a dream weight field and a choice of weekly pace.
Setup asks about your goal and how fast you want to go.
FoodTracker's plan summary showing daily calories, protein, carbohydrate and fat, an estimated target date and a note that this is an estimate.
The plan summary shows daily targets and says plainly that they are an estimate.

How it's built

Frontend

  • Next.js App Router and React, with Tailwind CSS and accessible component primitives.
  • react-hook-form with shared Zod schemas, so the browser and the server check input the same way.
  • Every page has a loading skeleton, and slow food lookups keep what you typed on screen.

Backend

  • Server actions call services, which call repositories. A shared wrapper turns thrown errors into typed results the forms can show.
  • Better Auth handles email and password sign-in, and every service call starts by requiring the signed-in user.
  • Meal queries are filtered by the signed-in user's id.

Data

  • PostgreSQL with Drizzle ORM and 5 migrations.
  • Nutrient amounts are stored per 100 g, so any serving size can be worked out from them.
  • Food search and details come from third-party food databases, including Open Food Facts for packaged products.

Delivery

  • A multi-stage Docker image that runs as a non-root user.
  • Cloud Build runs the database migrations as a Cloud Run job, and only then deploys the app to Cloud Run with Cloud SQL.

Key decisions and tradeoffs

  • The server works out the nutrition, not the browser

    The browser only says which food and how much. The server looks up the nutrition itself and checks the chosen serving exists, so a changed request cannot invent calories.

    The cost: Saving a meal needs the food service to answer, so if it is down the save fails with an error instead of working offline.

    Technical detail

    The meal service loads details for each food from the provider and resolves every entry to grams before writing anything. Failures from the provider are turned into one friendly error.

  • Keep a copy of each food the first time it is logged

    Reports are worked out from FoodTracker's own database, so they stay fast and do not depend on an outside service being up.

    The cost: The saved copy is never refreshed. If the source later corrects a food's nutrition, existing logs keep the old numbers.

    Technical detail

    A food is inserted once for each source and external id, with its nutrients stored per 100 g and its servings alongside. Later inserts of the same food do nothing.

  • Editing a meal replaces its foods in one step

    Saving an edited meal swaps the old list of foods for the new one together, so a meal is never left half-updated.

    The cost: Individual entries are not tracked over time, so there is no history of what a meal used to contain.

    Technical detail

    The update, the delete of old entries and the insert of new ones run in one database transaction, and the meal is matched on both its id and the signed-in user.

  • Let people try the app before signing up

    Asking for an account before showing any value loses people. Guests answer the setup questions first and see their plan before they register.

    The cost: Guest answers live only in that browser, so clearing site data or switching device loses them.

    Technical detail

    The draft is kept in local storage, validated with the same schema when it is read back, and carried into registration.

    Code: draft.ts

Hard problems I solved

  • Turning a few answers into a sensible target

    The problem
    People need a daily target, but asking for a lot of detail puts them off, and an aggressive target can be unhealthy.
    What I did
    The plan uses a standard formula for resting energy, scaled by activity level, then adjusts for the chosen weekly pace. Calories never drop below 1,200, protein is set from body weight, and a fast pace triggers a warning.
    The result
    A plan that is clearly labelled as an estimate, with the formula and its limits covered by unit tests.
  • Browser tests that do not depend on the internet

    The problem
    Food search relies on outside services, which would make end-to-end tests slow and flaky.
    What I did
    A switch replaces the real food provider with a fixed set of fixture foods. CI turns it on and runs the browser tests against a real Postgres database.
    The result
    The add, replace and delete meal flows are tested end to end, including validation of gram amounts.

Testing and quality

13 unit test files (about 35 cases) and 11 Playwright browser tests across 5 files, covering sign-in screens, guest setup, meals, and nutrition reports.

GitHub Actions runs two jobs on each pull request: lint, type check, unit tests and a production build; and Chromium browser tests against PostgreSQL with seeded data.

  • 5 database migrations, run as a separate Cloud Run job before each deploy.
  • Input is checked with Zod on the server, and meal changes are limited to the signed-in user's own meals.
  • The container image is multi-stage and runs as a non-root user.

Known limitations

  • Barcode scanning is not built yet. The barcode tab is a placeholder.
  • Foods can only be added from search. There are no custom foods, favourites or saved meals yet.
  • Food search and details depend on one outside service, with no fallback if it is down.
  • Deleting a meal is permanent, and there is no undo.

Use and further reading

Read more in the repo: CI workflowCloud Run deployment guide