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
| Kind | Lifecycle | Meaning |
|---|---|---|
nutrition.food | version | Versioned generic, branded, restaurant, prepared, or custom food with servings and compositions |
nutrition.nutrient | version | Versioned nutrient concept, preferred unit, and external codes |
nutrition.intake | revision | Logged consumption carrying an immutable food snapshot or directly reported nutrient totals |
nutrition.meal | revision | Named group of exact nutrition.intake revisions |
nutrition.goal | revision | Target 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.foodcarries localizednames,foodType,sources,identifiers(barcodes stored as strings),servings, andcompositions, plus optionalbrandandderivedFrom. - Each composition has a local
id, preparationstate(raw,cooked,as_sold,prepared,unknown),basis(mass, volume, or named serving), andnutrients. - Each serving declares
amount,compositionIds(the exact compositions it applies to), and optionalequivalent(mass or volume).serving.amountis the count represented byequivalent—for example,amount: 2withequivalent: 30 gmeans 1 serving = 15 g. - Converting between mass and volume requires an explicit
composition.massVolumeEquivalentwith 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.kind | Contents |
|---|---|
food | Full embedded snapshot of the nutrition.food at logging time, compositionId, original consumed portion, and nutrition result |
nutrients | Display 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— methodfitdata: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.mealowns the relationship via an ordered array of exact{ id, revision }intake references. Intakes carry nomealIdfield that could disagree.validateMealEntries({ meal, entries })checks every referenced revision, rejects duplicate IDs, extra unreferenced entries, self-references, and subject mismatches.- Optional
capturerecords 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).