Skip to content
Recipes

Port a Mirth JavaScript transformer that looks up a database and routes to another channel

Keep a Mirth transformer script that looks a patient up in a database, edits the HL7 message and routes it to another channel, running DatabaseConnectionFactory and router.routeMessage in the MedHook sandbox.

On this page

Many Mirth channels hold a JavaScript transformer step that does three things: it looks the patient up in a database (an enterprise ID from a cross-reference table), writes the answer into the HL7 message, and hands the message to another channel with router.routeMessage. This recipe keeps that script. The import copies it into a JavaScript step, where DatabaseConnectionFactory, router and DateUtil run as they did in Mirth, through the engine. You change one line, so that the database password lives on a profile instead of in the script.

Source and destination

The channel receives ADT messages over MLLP. Its transformer queries the MPI cross-reference table, sets PID-2 and the message time, and routes the message to an archive channel. In MedHook the archive channel is a workflow in the same workspace, and the route queues a run of it.

flowchart LR
    A[ADT over MLLP] --> B[JavaScript step]
    B -->|executeCachedQuery, READ ONLY| C[(patient_xref table)]
    B -->|router.routeMessage| D[Demo Results Archive workflow]
    B --> E[Destinations of this workflow]
  • Source: HL7 v2 ADT messages, through an MLLP trigger.
  • Lookup: a table on SQL Server, PostgreSQL, MySQL, MariaDB or Oracle, read through an SQL database profile.
  • Result: the message, with PID-2 set, goes on to this workflow's destinations, and one queued run of the archive workflow gets a copy.

What it uses

  • A JavaScript step with the Mirth shims the import adds, and the Mirth APIs DatabaseConnectionFactory, router, DateUtil and SerializerFactory.
  • An SQL database adapter profile named MPI database.
  • A second workflow named Demo Results Archive in the same workspace, turned on.

The script, as it was in Mirth (synthetic names and hosts):

javascript
var mrn = msg["PID"]["PID.3"][0]["PID.3.1"].toString();
var db = DatabaseConnectionFactory.createDatabaseConnection(
  "net.sourceforge.jtds.jdbc.Driver",
  "jdbc:jtds:sqlserver://mpi-db.example-host.test:1433/mpi;ssl=require",
  "mpi_reader",
  "********",
);
try {
  var rs = db.executeCachedQuery(
    "SELECT enterprise_id, facility FROM patient_xref WHERE local_mrn = ?",
    [mrn],
  );
  if (rs.next()) {
    msg["PID"]["PID.2"]["PID.2.1"] = rs.getString("enterprise_id");
    channelMap.put("facility", rs.getString("facility"));
  } else {
    logger.warn("No enterprise id for this patient");
  }
} finally {
  db.close();
}
msg["MSH"]["MSH.7"]["MSH.7.1"] = DateUtil.getCurrentDate("yyyyMMddHHmmss");
var response = router.routeMessage(
  "Demo Results Archive",
  SerializerFactory.getSerializer("HL7V2").fromXML(msg),
);
logger.info("Archive: " + response.getStatus());

Everything in it runs in MedHook as written. Only the connection line changes: name a profile instead of a JDBC URL and password.

javascript
var db = DatabaseConnectionFactory.createDatabaseConnection(
  "profile:MPI database",
);

The profile holds the server and the password, encrypted:

json
{
  "dialect": "sqlserver",
  "host": "mpi-db.example-host.test",
  "database": "mpi",
  "user": "mpi_reader",
  "password": "********",
  "tls": { "mode": "verify-full" }
}

A JDBC URL with a user and password also works, as in Mirth: the password is used as given and never logged, audited or shown in an error. It is still text in the step, though, so a profile is the better place for it. Connect as a user that can only SELECT from patient_xref.

What each call does here:

CallIn MedHook
createDatabaseConnection('profile:MPI database')Opens the workspace's SQL database profile. The adapter's host and TLS rules apply: a private host needs SSRF_ALLOWLIST.
executeCachedQuery(sql, [mrn])Runs the SELECT read-only, with ? bound to the MRN (never pasted into the SQL). At most 1,000 rows.
rs.next(), rs.getString('enterprise_id')Read the cached rows, as Mirth's CachedRowSet does. Column names are not case-sensitive.
channelMap.put('facility', ...)Puts the value in the channel map for the next JavaScript step of this message.
DateUtil.getCurrentDate('yyyyMMddHHmmss')Formats the engine's current time with a Java date pattern.
router.routeMessage('Demo Results Archive', ...)Queues a run of that workflow with the ER7 text as its input, and returns SENT with the run's id.

To try it, send a synthetic message such as:

text
MSH|^~\&|DEMO|DEMO HEALTH|MEDHOOK|DEMO|20261001120000||ADT^A08|MSG0001|P|2.5
PID|1||TP-0001^^^MRN||TESTPATIENT^JANE||19800101|F

with a row in the test table:

sql
INSERT INTO patient_xref (local_mrn, enterprise_id, facility)
VALUES ('TP-0001', 'E-900001', 'DEMO');

The step's output message has PID|1|E-900001|TP-0001^^^MRN|..., its log shows info: Archive: SENT, and the Runs tab of Demo Results Archive lists a queued run. A dry run runs the query but starts no archive run, and the Response says so.

From the Mirth channel

Import the channel. The transformer becomes a JavaScript step that starts with the Mirth shims and keeps the script. The import:

  • redacts the password literal in createDatabaseConnection and says so in a warning. Change the line to 'profile:MPI database' and create the profile.
  • reports an INFO finding, Calls DatabaseConnectionFactory.createDatabaseConnection, router.routeMessage, which the sandbox runs through the engine. A script that also calls an API the sandbox does not have, such as SMTPConnectionFactory or java.io.File, gets a REVIEW finding naming it, and saving the step warns MIRTH_API_UNSUPPORTED.
  • does not create the archive channel. Import it too, or build the Demo Results Archive workflow, and turn it on. A name two workflows share fails the route; use router.routeMessageByChannelId with the workflow's id instead.

Status

Available on this release. router, DatabaseConnectionFactory and FileUtil reach the engine only on engines that run custom code in isolated-vm, as production images do.

A few things differ from Mirth. executeCachedQuery is read-only; write with executeUpdate, which commits on its own. routeMessage queues the archive run and does not wait for it, so the Response carries the run id rather than the archive channel's reply. A step that is retried runs its script again: the route does not queue a second run, but an executeUpdate would write again. The script's engine calls share the step's 5 seconds, so keep the lookup indexed.

Reference