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.
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_atonce its run is recorded.
What it uses
- A polling trigger with the
pollaction of an SQL database profile. - A field mapping step (the destination's Data Map).
- Adapter
fhir-rest, actioncreatewithifNoneExist.
The source table in this recipe:
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:
{
"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:
| Source | Target | Rule |
|---|---|---|
$.item.id_system | identifier[0].system | |
$.item.id | identifier[0].value | |
$.item.result_status | status | lookupValues: F to final, C to corrected, P to preliminary |
$.item.loinc_code | code.coding[0].code | |
$.item.test_name | code.text | |
$.item.mrn | subject.identifier.value | |
$.item.value_numeric | valueQuantity.value | type-cast to number (PostgreSQL numeric arrives as text) |
$.item.units | valueQuantity.unit | |
$.item.effective | effectiveDateTime |
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:
{ "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:
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
SELECTruns in aREAD ONLYtransaction. Give the database userSELECTon the table andUPDATEonprocessed_atonly. - The default
startFromisnow: the first poll records the cursor and runs nothing. Usebeginning, 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
- SQL database adapter
- Polling trigger
- Field mapping
- CSV roster to FHIR, the same conditional create for files
- Migrating from Mirth Connect