Node.js integrates with M: The NoSQL Hierarchical database

April 3, 2013

We have talked recently about the significance of integrating the Node.js language with the powerful M database, particularly in the space of healthcare applications.

/blog_archives/files/6_1529600465.png

The efficiency of Node.js combined with the high performance of M, provides an unparalleled fresh approach for building healthcare applications.

Here is how you can try this today:

  • Install GT.M (the open source implementation of M)
  • Install Node.js
  • Install the nodem module that provides Node.js drivers for M
  • Install the ewdglobals module that provides Node.js integration

With this, you are now ready to try the following examplesLet’s start with an easy one:

 

var ewd = require('ewdglobals');
var nodem = require('nodem');

var db = new nodem.Gtm();

ewd.init(db);

db.open();

var patient = new ewd.GlobalNode(‘patient’,[123456]);

var document = {
  “name”: “John Doe”,
  “city”: “New York”
  };

patient._setDocument(document);

db.close();

If we put this in a file called exampleA.js and run it with:

nodejs  exampleA.js

We can then inspect the state of the entry in the M database with the ZWRITE command:

GTM>zwrite ^patient

To find that it is now set to:

^patient(123456,"city")="New York"
^patient(123456,"name")="John Doe"

This reflects the mapping between the tree-like hierarchical structure of M multi-dimensional arrays, and the nested properties structure of Node.js objects.

We can also use Node.js to get an entire entry from the M database into a JSON structure, and then print it to the console by using the following two lines:

var record = patient._getDocument();
console.log('patient info: ' + JSON.stringify(record));

By using jQuery inspired notation, we can now access further elements down the hierarchical structure of the M entry:

var nameValue  = patient.$('name')._value;
var cityValue  = patient.$('city')._value;

console.log(‘Name: ‘ + nameValue + ‘ City: ‘ + cityValue);

A more complete example illustrates the use of multiple levels of the data hierarchy:


var ewd = require('ewdglobals');
var nodem = require('nodem');

var db = new nodem.Gtm();

ewd.init(db);

console.log(“Setting a Global”);

db.open();

var patient = new ewd.GlobalNode(‘patient’,[123456]);

var document = {
  “name”: “John Doe”,
  “city”: “New York”,
  “treatments” : {
    “surgeries” : [ “apendicectomy”, “biopsy” ],
    “radiation” : [ “gamma”, “x-rays” ],
    “physiotherapy” : [ “knee”, “shoulder” ]
    },
  };

patient._setDocument(document);

console.log(“Querying properties”);

// By quering for a subscript, all its attributes
// become available as properties.
//
var treatementsObject = patient.$(‘treatments’);

console.log(‘Surgeries: ‘);
patient.treatments.surgeries._forEach( function( id, subNode ) {
  console.log( ‘id: ‘ + id + ‘:’ + subNode._value );
  });

// Accessing radiation treatements
console.log(‘Radiation:’);
console.log( patient.treatments.radiation[0]._value );
console.log( patient.treatments.radiation[1]._value );

db.close();

Note in particular, the use of the forEach() construct to visit all the elements of an array in the structure. And the use of the dot notation for progressively accessing each one of the properties that map to M sub-indices.

This is a similar to the MongoDB storage based on JSON structures, but with the difference that we can access the internal elements of the data structure.

This modern integration of Node.js with M offers a unique opportunity for Javascript developers to enter the field of healthcare IT applications and bring rapid innovation to it.

Node.js developers: Healthcare needs your help!

Leave a Reply