Skip to content
Recipes

Database Reader to FHIR

Poll a PostgreSQL results table with a cursor, the way a Mirth Database Reader does, write each row to a FHIR server as an Observation, and mark the row as processed.

On this page

A lab system or an interface staging database often writes results to a table, and a Mirth Database Reader polls it: it selects the rows that have not been sent, sends each one, and runs an update that marks it as processed. This recipe does the same in MedHook with a PostgreSQL table, and writes each row to a FHIR R4 server as an Observation.

Source and destination

The engine polls the table itself. Each new row becomes one run, each run sends one conditional create to the FHIR server, and then the post-process statement marks the row.

flowchart LR
    A[(lab_results table)] -->|SELECT new rows| B[Polling trigger]
    B -->|one run per row| C[Data map]
    C -->|Observation, If-None-Exist| D[FHIR R4 server]
    B -.->|UPDATE processed_at| A
  • Source: a PostgreSQL table with one result per row and an increasing key.
  • Destination: any FHIR R4 server a FHIR REST profile can reach.
  • Result: one Observation per row, identified by the row's key, so a row read twice does not create a second Observation. Each row is stamped processed_at once its run is recorded.

What it uses

The source table in this recipe:

sql
CREATE TABLE lab_results (
  id            bigserial PRIMARY KEY,
  mrn           text NOT NULL,
  loinc_code    text NOT NULL,
  test_name     text,
  value_numeric numeric,
  units         text,
  result_status text NOT NULL,  -- F, C or P
  collected_at  timestamptz NOT NULL,
  processed_at  timestamptz
);

The trigger polls once a minute. The id column is the cursor, the post-process statement stamps each row, and startFrom: "beginning" runs the rows already waiting when the trigger is first deployed:

json
{
  "type": "polling",
  "interval": 60000,
  "adapterProfileId": "results-db",
  "action": "poll",
  "params": {
    "sql": "SELECT id, 'urn:example:lab-results' AS id_system, mrn, loinc_code, test_name, value_numeric, units, result_status, to_char(collected_at, 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') AS effective FROM lab_results WHERE id > :cursor AND processed_at IS NULL ORDER BY id LIMIT :limit",
    "cursor": "{{cursor}}",
    "cursorInitial": 0,
    "limit": 100,
    "postProcess": {
      "sql": "UPDATE lab_results SET processed_at = now() WHERE id = :id AND processed_at IS NULL",
      "bind": ["id"]
    }
  },
  "itemsPath": "rows",
  "cursorField": "id",
  "idField": "id",
  "startFrom": "beginning"
}

:cursor and :limit are bound values, never pasted into the SQL. The engine's database session runs in UTC, so to_char writes the timestamptz column as a FHIR dateTime in UTC. The post-process statement must be safe to run twice, because a row can be read again after a failure; this one changes nothing the second time.

Each run's input carries the row under item. To pick the sources instead of typing them, save one row as JSON as the workflow's sample: with the flat field-path view on (off by default), the sample panel's Fields view lists each column as $.item.<column> with Copy path. The Data Map is:

SourceTargetRule
$.item.id_systemidentifier[0].system
$.item.ididentifier[0].value
$.item.result_statusstatuslookupValues: F to final, C to corrected, P to preliminary
$.item.loinc_codecode.coding[0].code
$.item.test_namecode.text
$.item.mrnsubject.identifier.value
$.item.value_numericvalueQuantity.valuetype-cast to number (PostgreSQL numeric arrives as text)
$.item.unitsvalueQuantity.unit
$.item.effectiveeffectiveDateTime

To set the LOINC system on code.coding[0].system, return it from the SELECT as a constant column, as id_system does, and map it. A status the lookup table does not hold passes through unchanged, so add a row for every code the table uses. The send step's config names the resource type and turns on the duplicate check:

json
{ "resourceType": "Observation", "ifNoneExist": "identifier" }

With ifNoneExist set to identifier, the create sends the Observation's identifier (urn:example:lab-results|<id>, URL-encoded) in an If-None-Exist header, so a row that runs again finds the Observation it created the first time.

A test row needs only synthetic data:

sql
INSERT INTO lab_results (mrn, loinc_code, test_name, value_numeric, units, result_status, collected_at)
VALUES ('TP-0001', '2345-7', 'Glucose', 98, 'mg/dL', 'F', '2026-10-20 13:00:00+00');

Status

Available on this release. This recipe uses PostgreSQL; for SQL Server, MySQL and Oracle (paging syntax, TLS and the column-name case) see Dialect notes and Migrate a Mirth Database Reader on SQL Server, MySQL or Oracle.

How a row moves through, and what to watch:

  • The row's run is recorded first, then the post-process runs, then the cursor moves past the row. A crash between them reads the row again, finds the run it already recorded, and runs only the post-process. See Nothing lost, nothing sent twice.
  • If the post-process fails, the poll stops before that row and records the error. The next poll tries again.
  • A row the trigger's inbound filter rejects starts no run, but its post-process still runs, as in a Mirth Database Reader.
  • The poll's SELECT runs in a READ ONLY transaction. Give the database user SELECT on the table and UPDATE on processed_at only.
  • The default startFrom is now: the first poll records the cursor and runs nothing. Use beginning, as here, when the table already holds rows you want sent.

The Mirth importer turns a PostgreSQL Database Reader into this trigger, with the SELECT carried over and the update statement as the post-process. Its findings ask you to add the cursor. See Import from Mirth.

Reference