T0019 — FHIR model generator and model packages

Part of the roadmap. Phase 1 · wave 2 · size L.

Goal

Generate compact R4 and R5 models from the official StructureDefinitions and publish them as ModelProviders (design notes, ADR-0008).

Scope

  • tools/model-gen:
  • What the data module contains: types, base types, kinds, abstractness; elements with name, types, whether they repeat and whether they are choices; backbone types named by path; contentReference resolved; the primitive → System mapping.
  • packages/fhir-r4 (@callis/fhir-r4) and packages/fhir-r5 (@callis/fhir-r5): ModelProvider implementations over the generated data, exported as r4 and r5, with a peer dependency on @callis/core.
  • A CI job (on changes to tools/model-gen or a manual trigger) that checks regeneration gives no diff.

Design notes

  • A generator in tools/model-gen reads StructureDefinition snapshots from the official FHIR NPM packages (hl7.fhir.r4.core@4.0.1, hl7.fhir.r5.core@5.0.0) at development time.
  • For each version it emits a compact TypeScript module with:
    • types, base types, kinds and abstractness
    • elements: name, types, whether they repeat, whether they are choices
    • backbone element types named by path (Patient.contact)
    • resolved contentReferences (Questionnaire.item.item → Questionnaire.item)
    • the primitive → System type mapping
  • These are published as @callis/fhir-r4 and @callis/fhir-r5, each exporting a ready-made ModelProvider. R4B can be added cheaply later.
  • The generated output is committed, so diffs are reviewable and builds need no network. A CI job checks that regenerating gives no diff.
  • Size budget: at most 60 kB gzip per model, to be measured.
  • Richer metadata for elementDefinition(), slice(), checkModifiers() and conformsTo() (phase 2) goes in a separate entry point, so the default model stays small.

Acceptance criteria

  • All resources and data types from the core packages are present. Spot checks:
    • Patient.deceased is a choice of boolean | dateTime
    • Questionnaire.item.item resolves to Questionnaire.item
    • Bundle.entry.resource has type Resource
    • Observation.value[x] lists the correct types per version
  • Running the generator twice gives byte-identical output.
  • Gzip size per model is reported. Over 60 kB needs a justification in the PR.
  • Both generated providers pass describeModelProviderContract over all generated resource and datatype roots, including primitive and derived-primitive mappings, Quantity/Age, and choice elements (#56).