Quick Start your project with a JavaScript MarkLogic 6 Driver for Node.JS

Many people are experimenting with NoSQL databases using NodeJS as a server platform. This is a neat yet powerful package that lets you rapidly develop a server application in JavaScript. I have created a simple database ‘driver’ for NodeJS for the MarkLogic V6 REST API. Just ‘npm install mldb’. In this post I describe why you’d want to use it.

The MarkLogic REST API is easy to learn and extremely powerful. It provides almost everything you need when building an application against MarkLogic Server. Where this accessibility falls down is in having few programming language bindings (only Java so far – but hey, the REST API is new!), and in the underlying complexity. MarkLogic is an Enterprise class NoSQL database. This means we have, amongst other things, ACID transactions, built in security and administration controls, and a very sophisticated search mechanism.

Problem is, this power can sometimes get in the way. Ever written JavaScript code to do Digest authentication? Nope, neither has anyone else. It’s a pain to have to reconfigure MarkLogic to use no authentication and a default user before you code against it. Also, creating the REST API server endpoint is not a walk in the park for the uninitiated. Once you get in to the API, sometimes MarkLogic’s power gets in the way there too. You’re forced to add a URI for every doc. You need to provide default search options, and specify whether you want to use/receive JSON or XML.

My MarkLogic driver for NodeJS – mldb – aims to hide all this work from the average mortal developer. It also aims to be better than other NoSQL NodeJS drivers, and to show off MarkLogic’s Enterprise features without the up front work. So for example, you tell me what database name you want, what port it should be on, and my driver will go away and create it for you – server and all. Don’t care what a new document’s URI is? We’ll generate it for you. Don’t understand what Basic and Digest authentication is? We’ll handle it automatically. So all you need to do is simply use the database!

Here’s an example to create a document, and print a message when it’s complete:-

var mldb = require("mldb");
var db = new mldb(); // default mldbtest settings
db.save({title: "my json doc", author: "Adam Fowler"},
  "/documents/1",function(result) {
    console.log("doc added");

You’ll notice that callbacks are used. This allows NodeJS developers to get the best performance out of their server. All other NoSQL node drivers I have seen block for IO! I mean, what’s the point of using NodeJS if you’re going to do that!?! I’ve kept true to NodeJS’ architecture. You see I’ve specified a URI. This will be optional in mldb. (Once I’ve coded the auto URI generation!)

The project is still in its infancy, but I have big hopes for it. Please check out the project page, and the wiki, and add your own requirements in to the issue tracker! I’ll be working on this over Crimbo for my own needs, so if you ask for a feature then Node Santa may listen!

Why do I care?

Ah the perennial question asked of all pre-sales engineers! Well, the main reason people will want to use this database driver is its ease of use. I’ve aimed to create the most usable, simple API possible. In a real world project though, you may very well not care about this driver! If you’re just starting out evaluating NoSQL databases and need one with Enterprise security, scalability, and search then you’ll want to use MarkLogic. This driver makes it very quick indeed to build a web application on top of MarkLogic. If you want to see just how quick your application can be, then download and install MarkLogic and fire up NodeJS with my database driver.

Hey, I thought you guys had an XML database? Why are you talking about JSON!?!

Yeah we are a database that stores and processes XML. But guess what? We can pretty much store anything. The REST API in V6 completely abstracts out the internal data model – which is XML – from the JSON that you pass in and out of the database. This gives you the ease of adoption in using a simple schema less format like JSON, but backs it up with the power of XQuery, XSLT and the range indexes – including geospatial indexes – search and alerting features of MarkLogic.

You can quickly build your first app using my driver and NodeJS. If you decide MarkLogic is for you, then you can start to look at what else MarkLogic gives you out of the box. Like a simple, single installer that ‘just works’ in seconds. Like support for 200+ document formats for full text searching and meta data extraction, storing XML and JSON documents, an interactive point and click application builder with a selection of AJAX widgets, and the ability to write custom XQuery functions (akin to, but more powerful than, RDBMS stored procedures) when you need the power of MarkLogic.

Oh my word, this will change my life! How do I get it!?!

Calm yourself. Simply do a ‘npm install mldb’. At the moment I don’t have any documentation (the project is 48 hours old!), but I do have some test scripts to add information to a database and query it, so please check those out on GitHub. All else fails, contact me on Skype via IM – adamfowleruk – or email my work address adam dot fowler at marklogic dot com.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.