Skip to content
Recipes

Migrate a Mirth Database Reader on SQL Server, MySQL or Oracle

Replace a Mirth Database Reader that polls a results table with a cursor-driven SQL poll, map each row to a FHIR Observation, and mark the row processed afterwards.

On this page

Many Mirth channels start with a Database Reader: a SELECT on a staging or results table every few seconds, one message per row, and an update statement that marks each row done. This recipe moves that channel to MedHook on SQL Server, MySQL (or MariaDB) or Oracle. A polling trigger reads new rows by a cursor column, each row becomes a FHIR Observation, and a post-process statement marks the row processed after its workflow ran.

Source and destination

The engine polls the database itself. Each returned row becomes one run, and each run sends one conditional create to the FHIR server. The post-process update runs after the row's run, before the cursor moves past it.

flowchart LR
    A[(lab_results table)] -->|SELECT one page| B[Polling trigger, poll action]
    B -->|one run per row| C[Data map]
    C -->|Observation, If-None-Exist| D[FHIR R4 server]
    B -->|post-process UPDATE per row| A
  • Source: a table in SQL Server, MySQL, MariaDB or Oracle with a key column and a last-changed timestamp.
  • Destination: any FHIR R4 server a FHIR REST profile can reach.
  • Result: one Observation per row. A row read again after a failure creates nothing new, and its post-process runs again.

What it uses

The SQL database profile names the dialect and the server. This one is SQL Server. Use mysql or oracle for the others; the port defaults to 1433, 3306 or 1521 to match:

json
{
  "dialect": "sqlserver",
  "host": "lab-db.example.test",
  "database": "lab_staging",
  "user": "medhook_reader",
  "password": "********",
  "tls": { "mode": "verify-full" },
  "poolSize": 5,
  "statementTimeoutMs": 30000
}

tls.mode is verify-full by default, which checks the certificate and the host name. Add tls.caPem for a private CA (on Oracle, put the CA in NODE_EXTRA_CA_CERTS on the engine instead, and set tls.serverCertDn if the certificate subject has more than CN=<host>). require encrypts without checking the certificate. disable sends no TLS and is accepted only for a host on SSRF_ALLOWLIST, such as a database reached over an IPSec tunnel. A private host needs SSRF_ALLOWLIST in any mode. Connect as a user that can only SELECT the results table and UPDATE its processed column.

The trigger reads up to 100 new rows a minute, by updated_at, and marks each row once its run is done:

json
{
  "type": "polling",
  "adapterProfileId": "lab-results-db",
  "action": "poll",
  "interval": 60000,
  "params": {
    "sql": "SELECT TOP (:limit) id, mrn, test_code, test_name, result_value, result_unit, collected_at, updated_at FROM lab_results WHERE processed = 0 AND updated_at >= :cursor ORDER BY updated_at",
    "cursor": "{{cursor}}",
    "cursorInitial": "1970-01-01",
    "limit": 100,
    "postProcess": {
      "sql": "UPDATE lab_results SET processed = 1 WHERE id = :id",
      "bind": ["id"]
    }
  },
  "itemsPath": "rows",
  "cursorField": "updated_at",
  "idField": "id",
  "startFrom": "beginning"
}

:cursor binds the last updated_at the trigger stored (cursorInitial before there is one), and :limit binds limit. The cursor is only ever a bound value, never pasted into the SQL. >= is safe: the trigger remembers which rows at the stored cursor already ran. The page limit is written the database's own way:

DialectThe SELECT
SQL ServerSELECT TOP (:limit) ... WHERE processed = 0 AND updated_at >= :cursor ORDER BY updated_at
MySQLSELECT ... WHERE processed = 0 AND updated_at >= :cursor ORDER BY updated_at LIMIT :limit
OracleSELECT ... WHERE processed = 0 AND updated_at >= :cursor ORDER BY updated_at FETCH FIRST :limit ROWS ONLY

Oracle returns unquoted column names in lower case, as Mirth's Database Reader does, so the same Data Map works on every dialect.

The poll's SELECT runs read-only, but that is a safeguard, not a guarantee. Saving the workflow warns (SQL_READ_ONLY_RISK) when the SELECT uses COMMIT, ROLLBACK, DDL, GRANT, EXEC, CALL, SET GLOBAL, xp_, sp_configure, OPENROWSET, OPENDATASOURCE, OPENQUERY, BULK, UTL_, DBMS_, LOAD DATA, LOAD_FILE or INTO OUTFILE. A plain SELECT like this one raises none.

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. For the columns above, the Data Map is:

SourceTarget path
$.item.ididentifier[0].value
$.item.mrnsubject.identifier.value
$.item.test_codecode.coding[0].code
$.item.test_namecode.text
$.item.result_valuevalueQuantity.value
$.item.result_unitvalueQuantity.unit
$.item.collected_ateffectiveDateTime

Add three rows for the fixed values: target status with default final, target identifier[0].system with default urn:example-host:lab-result, and target code.coding[0].system with default set to your lab's code system (a row's default is used when its source, such as $.item.status, is missing). Then add ops for what differs between databases:

  • a type-cast op to string on identifier[0].value, since the duplicate check needs the identifier as text and id comes back as a number;
  • a date-format op (to ISO) on effectiveDateTime, since each database writes timestamps its own way;
  • a type-cast op to number on valueQuantity.value, since MySQL returns DECIMAL columns as text. The send step's config names the resource type and turns on the duplicate check:
json
{ "resourceType": "Observation", "ifNoneExist": "identifier" }

The row's id, under the urn:example-host:lab-result system, is the Observation's identifier, so a row read again after a crash sends the same If-None-Exist header and creates no second Observation. See FHIR REST for conditional create.

A test table needs only synthetic rows:

sql
INSERT INTO lab_results (id, mrn, test_code, test_name, result_value, result_unit, collected_at, updated_at, processed)
VALUES
  (1001, 'TP-0001', '2345-7', 'Glucose TESTPATIENT', 98, 'mg/dL', '2026-10-01T08:00:00', '2026-10-01T08:05:00', 0),
  (1002, 'TP-0002', '2345-7', 'Glucose TESTPATIENT', 104, 'mg/dL', '2026-10-01T08:10:00', '2026-10-01T08:15:00', 0);

From the Mirth channel

Import the channel to start from Mirth's own settings. A Database Reader on a jdbc:sqlserver, jdbc:jtds, jdbc:mysql, jdbc:mariadb or jdbc:oracle:thin URL becomes this polling trigger. The import carries over:

  • the dialect, host, port, database and TLS mode, from the JDBC URL. The user name and password are never imported; enter them on the profile.
  • the SELECT, with each ${var} as :var.
  • the update statement as the post-process, with each ${column} bound from the row (WHERE id = ${id} becomes WHERE id = :id with bind: ["id"]).

The import does not add a cursor. Its finding says which WHERE ... >= :cursor and page limit to add for the dialect; until then the trigger relies on processed = 0 alone. Mirth ran an update in "once after all rows" mode once per poll; here it runs once per row, so check it still marks the row it is given.

Status

Available on this release for SQL Server, MySQL, MariaDB and Oracle, as well as PostgreSQL.

The post-process runs in its own transaction after the row's run and before the cursor moves, so it must be idempotent: SET processed = 1 WHERE id = :id is. If it fails, the poll stops before that row and tries again next time. A row the trigger's inbound filter rejects starts no run, but its post-process still runs, as in Mirth. See Nothing lost, nothing sent twice.

Reference