Skip to Content
FFitDatadraft
Specification

Nutrition

Food and nutrient definitions, historical intake snapshots, serving calculations, meals, goals, and capture provenance.

@fitdata/nutrition defines portable nutrition contracts without coupling to a commercial food database, provider, or application schema.

Record kinds

KindLifecycleMeaning
nutrition.foodversionVersioned generic, branded, restaurant, prepared, or custom food with servings and compositions
nutrition.nutrientversionVersioned nutrient concept, preferred unit, and external codes
nutrition.intakerevisionLogged consumption carrying an immutable food snapshot or directly reported nutrient totals
nutrition.mealrevisionNamed group of exact nutrition.intake revisions
nutrition.goalrevisionTarget nutrient amounts starting on an effective calendar date

Meal and daily rollups without item breakdowns use core.summary from @fitdata/core. subject, recordedAt, and provenance are optional on standalone entities.

Food identity, compositions, and servings

  • A nutrition.food carries localized names, foodType, sources, identifiers (barcodes stored as strings), servings, and compositions, plus optional brand and derivedFrom.
  • Each composition has a local id, preparation state (raw, cooked, as_sold, prepared, unknown), basis (mass, volume, or named serving), and nutrients.
  • Each serving declares amount, compositionIds (the exact compositions it applies to), and optional equivalent (mass or volume). serving.amount is the count represented by equivalent—for example, amount: 2 with equivalent: 30 g means 1 serving = 15 g.
  • Converting between mass and volume requires an explicit composition.massVolumeEquivalent with a supporting source. No default water density is assumed.

Units and portion codes

Mass and volume fields use explicit case-sensitive UCUM codes from @fitdata/core:

  • Mass: kg, g, mg, ug, [lb_av], [oz_av]
  • Volume: L, mL, [foz_us], [foz_br], [cup_us], [cup_m], [tbs_us], [tsp_us], [tbs_m], [tsp_m]
  • Energy: kcal, kJ, J

Unqualified cup, oz, or spoon codes are rejected because mass ounces, US fluid ounces, and British fluid ounces differ.

Historical intake snapshots

nutrition.intake uses a discriminated intake union:

intake.kindContents
foodFull embedded snapshot of the nutrition.food at logging time, compositionId, original consumed portion, and nutrition result
nutrientsDisplay name and nutrient totals, without inventing a fake food item

Embedding the food snapshot guarantees that later catalogue edits never silently mutate historical diary entries. Food intake nutrition takes one of three explicit forms:

  • calculated — method fitdata:food_scaling@1, resolvedAmount, and scaled totals.
  • reported — source totals plus source reference (used when imported labels or historical totals differ from fresh scaling).
  • unavailable — coded reason; missing conversions or nutrients are never coerced to zero.

Omitted nutrients remain unknown. The calculator never substitutes salt for sodium, infers calories from macros, or merges incompatible carbohydrate definitions.

Serving calculation contract

createFoodIntake({ food, compositionId, consumed }) builds and validates a complete food intake payload offline:

import { createFoodIntake, validateNutritionRecord } from "@fitdata/nutrition";

const intakeBody = createFoodIntake({
  food: yogurtFood,
  compositionId: "as-sold",
  consumed: { kind: "serving", servingId: "small-pot", amount: 1.5 },
});

if (intakeBody.ok) {
  const checked = validateNutritionRecord({
    kind: "nutrition.intake",
    id: "example:intake-yogurt",
    revision: 1,
    time: { kind: "instant", at: "2026-11-01T08:30:00+01:00" },
    intake: intakeBody.value,
  });
  console.log(checked.ok);
}

Semantic validation recalculates calculated totals from the saved snapshot and checks matching nutrient IDs/versions, units, comparators, and numeric values within a 1e-12 relative tolerance (with zero required to remain zero).

Meals and capture provenance

  • nutrition.meal owns the relationship via an ordered array of exact { id, revision } intake references. Intakes carry no mealId field that could disagree.
  • validateMealEntries({ meal, entries }) checks every referenced revision, rejects duplicate IDs, extra unreferenced entries, self-references, and subject mismatches.
  • Optional capture records how an entry was logged (fitdata:manual, fitdata:barcode_scan, fitdata:meal_photo, fitdata:nutrition_label_photo, fitdata:text, fitdata:import), source evidence, optional algorithm ID/version, and review state (unreviewed, confirmed, corrected).

On this page