Skip to content

Add US Core profiles TypeScript example (CSV to FHIR Bundle)#30

Open
ryukzak wants to merge 3 commits intomainfrom
example/atomic-ehr-codegen-us-core-profiles
Open

Add US Core profiles TypeScript example (CSV to FHIR Bundle)#30
ryukzak wants to merge 3 commits intomainfrom
example/atomic-ehr-codegen-us-core-profiles

Conversation

@ryukzak
Copy link
Copy Markdown
Contributor

@ryukzak ryukzak commented Apr 22, 2026

Summary

New example at developer-experience/atomic-ehr-codegen-typescript-us-core-profiles/ — a working CSV-to-FHIR-Bundle converter using @atomic-ehr/codegen profile class generation for US Core. Companion code to the blog tutorial Tutorial: US Core Profiles in TypeScript with @atomic-ehr/codegen.

What the example does:

  1. Generates profile classes for US Core Patient and US Core Blood Pressure plus base Bundle from hl7.fhir.r4.core.
  2. Parses patients.csv (5 patients, one BP reading each — race-coded with OMB categories).
  3. Converts each row into a validated USCorePatientProfile + USCoreBloodPressureProfile.
  4. Packages them as a transaction Bundle with urn:uuid cross-references.
  5. Reads the bundle back with typed getters (USCoreBloodPressureProfile.from(), getSystolic(), getDiastolic()) to compute an average BP.

Verified

  • npx tsc passes under --strict.
  • npx tsx load.ts prints Avg BP: 125.2/82.0 mmHg (n=5).
  • bundle.json output has correct meta.profile stamps, auto-populated category/component discriminators, and urn:uuid cross-refs.

Files

File Purpose
generate.ts Runs codegen to produce fhir-types/
load.ts Full converter (parse → Patient + BP → Bundle → read back avg)
patients.csv 5-row sample input
fhir-types/ Generated output (committed so readers browse without running the generator, 55 files / ~1.2M)
tsconfig.json --strict, types: ["node"] for node:fs/node:crypto resolution
package.json Dev deps: @atomic-ehr/codegen@canary (for the widened Reference type from codegen#144), tsx, typescript, @types/node
README.md Run instructions + POST-to-Aidbox pointer
.gitignore node_modules/, .codegen-cache/, bundle.json

Dependency note

@atomic-ehr/codegen is pinned to the current canary (which includes PR #144 widening Reference.reference to accept urn:uuid placeholders used in transaction bundles). Once 0.0.14 ships with that fix, this pin should move to plain ^0.0.14.

Test plan

  • npm install succeeds
  • npx tsx generate.ts regenerates fhir-types/ identically to what's committed
  • npx tsx load.ts prints Loaded 5 rows, Wrote bundle with 10 entries, Avg BP: 125.2/82.0 mmHg (n=5)
  • npx tsc exits with 0
  • (Optional) POST bundle.json to a local Aidbox and verify the urn:uuid references resolved to real Patient IDs

ryukzak added 3 commits April 21, 2026 16:42
Companion to the "Tutorial: US Core Profiles in TypeScript with
@atomic-ehr/codegen" blog post. Demonstrates:

- Generating typed profile classes for US Core Patient and US Core
  Blood Pressure plus base Bundle from hl7.fhir.r4.core
- Parsing a small CSV (5 patients, each with one BP reading)
- Converting rows to validated USCorePatientProfile and
  USCoreBloodPressureProfile instances
- Packaging them as a transaction Bundle with urn:uuid
  cross-references
- Reading the bundle back with typed getters to compute average BP

Code verified end-to-end: tsc --strict passes, npx tsx load.ts
prints "Avg BP: 125.2/82.0 mmHg (n=5)".

Generated fhir-types/ is committed so readers can browse the output
without running the generator.
Switches the example to @atomic-ehr/codegen@canary (0.0.13-canary with
PR #144 merged). Reference.reference now accepts all FHIR literal
reference forms -- urn:uuid:<uuid>, http://..., urn:oid:..., #fragment,
in addition to Patient/<id>.

Drops the inline cast: subject.reference takes the urn:uuid placeholder
directly. rowToBP's patientRef parameter is narrowed to
\`urn:uuid:\${string}\` so the typing carries the bundle placeholder
semantics all the way through.

Regenerate fhir-types/ with the canary. README note on urn:uuid
updated to reflect the fix.

Closes atomic-ehr/codegen#141.
rowToPatient now builds a plain Patient resource first and then calls
USCorePatientProfile.apply(basePatient) to wrap it with the profile.
Reads cleaner than the previous create() + Object.assign pattern and
mirrors the tutorial narrative.

tsconfig.json added so tsc picks up @types/node (node:fs / node:crypto
imports fail to resolve without "types": ["node"]).
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