Node.js integrates with M: a tutorial, part two | Opensource.com

Node.js integrates with M: a tutorial, part two

Posted 06 May 2013 by 

Rating: 
(10 votes)
M database and node.js
Image by : 

opensource.com

submit to reddit

In part one of this tutorial, I introduced the integration between the hierarchical data structures of the M database and the hierarchical structures of the Node.js language.

Here, in part two, I focus on the fact that this integration is equivalent to incorporating persistance storage in the Node.js language using a data model similar to JSON structures. This built upon a proven database that is known to deliver high performace for demanding applications.

In other words, this M database integration provides a

Native JSON Database for Node.js

The connection between the M database and the Node.js data structures is created when we use the expression

var planet = new ewd.GlobalNode('earth',[]);

We create an entry in the underlying M database, with a 'global' called 'earth.' In M terminology, a 'global' is a variable that resides in the database. This M database datastructure is now linked to the variable 'planet' that we have defined in our Node.js code.

Planet is an object that incorporates pointers to the database connection, and through them, we can communicate directly with the database.

For example, we can start adding information to this tree-like structure with expressions such as

planet.$('Africa')._value = "";
planet.$('Asia')._value = "";
planet.$('Australia')._value = "";
planet.$('North America')._value = "";
planet.$('Antartica')._value = "";
planet.$('Europe')._value = "";

We can then insert entries in the branches of this tree with the expressions:

planet.$('Asia').$('Japan').$('Tokyo')._value = "";

We can print out the content of this tree so far by using the expression:

var record = planet._getDocument();
console.log("planet info: " + JSON.stringify(record));

That produces the output:

planet info: {"Africa":"","Antartica":"","Asia":{"Japan":{"Tokyo":""}},"Australia":"","Europe":"","North America":""}

And that, after placing it in the jsbeautifier results in:

planet info: {
    "Africa": "",
    "Antartica": "",
    "Asia": {
        "Japan": {
            "Tokyo": ""
        }
    },
    "Australia": "",
    "Europe": "",
    "North America": ""
}

Notice that M and Node.js enable us to make detailed entries in some of the branches without requiring us to do the same in all other branches. This is similar to what we can do in document databases, such as MongoDB and CouchDB, but with the difference in the M hierarchical database, all the sub-branches of the data structure are available for direct interaction.

In this context, every sub-tree behaves like a tree. Querying happens in a very natural way as well, by doing assignments from potentially existing branches.

For example, we can attempt to get a value from the { Europe, France, Paris } branch, with the expressions:

home = planet.$('Europe').$('France').$('Paris')._value;
console.log(home)

Which in our case will return null, since we have not yet assigned anything to such branch.

The returned value will look better once we do the following assignment:

planet.$('Europe').$('France').$('Paris')._value = "Fromage";

home = planet.$('Europe').$('France').$('Paris')._value;
console.log(home)

This hierarchical structure is reminiscent of the modern Web, where URLs may have deeper and deeper content without having to impose any structure on other URLs.

The very interesting feature here is that just by using a very natural notation inside the Node.js language with the use of "." and "$()", we have been directly inserting and querying data in the permanent storage of a database.

Such a database can concurrently be accessed by others, which provides at the same time a structure for interprocess communication and shared processing.

With this M to Node.js connection, Javascript developers have now at their disposal a JSON native database, powered by the NoSQL hierarchical database M.

This connection empowers Javascript developers to Join the M Revolution and apply their skills to the field of Healthcare IT where most large applications are based on M. The contributions from a yourger generation of developers are greatly needed.

submit to reddit

Luis Ibáñez works as Software Engineer at Google Inc in New York city. Opinions expressed in this site are his own.

You can find him in github at:  http://www.github.com/luisibanez
and in twitter at: http://www.twitter.com/luisibanez

He previously worked as a Technical Leader at Kitware Inc., and Director of Open Source Community Development

International team of scientists open sources search for malaria cure