web stats
Mirth Community - View Single Post - Using JavaScript Objects for Transformations
View Single Post
Old 01-09-2019, 09:05 AM
sphelan sphelan is offline
What's HL7?
Join Date: Mar 2017
Posts: 3
sphelan is on a distinguished road
Default Using JavaScript Objects for Transformations

I'm attempting to cleanup a bunch of our channels that have duplicate code and consolidate our logic.

Most of our interfaces are HL7 which we transform into a standard JSON message that gets passed to our application for processing. The logic for 95% of the channels is the same (map PID.5.1 to first name, etc ...).

We have moved a large portion of our code base to code template functions. While this has reduced a large portion of duplicate code, I'm not completely satisfied with the results. Because there are some interfaces that do something different, we have to account for it by adding some if/then logic in the functions or duplicate the function to be use just by that channel.

I'm looking to move to a more object oriented approach and was wondering if anyone has done anything similar. I'm looking to move our logic to a JavaScript object stored as code template. You create this object in your channel transformation, pass the message and call a method to return the transformed message.

For those interfaces that like to be different, I could override the particular method that maps the field which is different. No duplicate code is create.

In my sample code, I have a base object PatientStay which has a method to create my standard JSON message. The PatientStayHL7 message inherits from PatientStay and provides methods to populate the JSON (map first name to PID.5.1).
// Base class (used by HL7, delimiter, etc ..)
function PatientStay() {};

PatientStay.prototype.toJSON = function() {
    var patientStayJSON = {
        patientFirstName: this.patientFirstName(),
        patientLastName: this.patientLastName(),
        mrn: this.getMedicalRecordNumber()
    return JSON.stringify(patientStayJSON);

PatientStay.prototype.patientFirstName = function() {
    return null;
PatientStay.prototype.patientLastName = function() {
    return null;
PatientStay.prototype.getMedicalRecordNumber = function() {
    return null;

// PatientStay HL7
function PatientStayHL7(hl7Message) {
    this.hl7Message = hl7Message.copy();

PatientStayHL7.prototype = Object.create(PatientStay.prototype);

PatientStayHL7.prototype.patientFirstName = function() {
    return this.hl7Message['PID']['PID.5']['PID.5.2'].toString();

PatientStayHL7.prototype.patientLastName = function() {
    return this.hl7Message['PID']['PID.5']['PID.5.1'].toString();

PatientStayHL7.prototype.getMedicalRecordNumber = function() {
    return this.hl7Message['PID']['PID.3']['PID.3.1'].toString()
If an interface follows our normal HL7 mappings (Hospital A), I can just create a channel, create a PatientStayH7 Message, pass the msg, and generate the new JSON payload. Very little code would be added to the channel's transformation.
var patientStay = new PatientStayHL7(msg);
msg = patientStay.toJSON();
When an interface does something slightly different (Hospital B), I can create a new object and override the method that populates that field.
In this example they put the MRN in 2.1 instead of 2.3.
// Create HospitalB object that inherits from PatientStayHL7
function HospitalB(hl7Message) {
   PatientStayHL7.call(this, hl7Message);
HospitalB.prototype = Object.create(PatientStayHL7.prototype);

// Override getMedicalRecordNumber to pull from PID.2.1 instead of PID.2.1
HospitalB.prototype.getMedicalRecordNumber = function() {
	return this.hl7Message['PID']['PID.2']['PID.2.1'].toString()

var patientStay = new HospitalB(msg);
msg = patientStay.toJSON();

Thoughts? Any downsides? An alternative that you are using successfully
Attached Files
File Type: zip javascript_object.zip (26.6 KB, 15 views)
Reply With Quote