Install your own oData server with Node.js and MongoDB

Author: Gábor Dolla October 16th, 2012

, , , ,

Update: it does work on windows from 1.2.4 !!!!

Using a remote datasource from the browser with jaydata is as easy as pie if your datasource speaks oData.  Unfortunately database servers don’t but with jaydata it is possible to use a database server as an oData endpoint. In this example I use MongoDB as the database server. In the following months jaydata will support many sql servers as backend so you will be able to access your data stored in your mysql, mssql, oracle, etc. server using oData.

Please note that this runs on Linux and OSX and will not run on Windows. In this example I use Ubuntu Server 12.04.

First install the necessary tools and libraries:
sudo apt-get install mongodb
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get install nodejs nodejs-dev
sudo apt-get install  npm build-essential make autoconf libtool flex bison git libxml2-dev

Let’s create a new project:
mkdir test; cd test
npm install mongodb express http connect q node-uuid bcrypt xmldom qs
npm install jaydata

Let’s define the model:

Only a few lines of code is needed to start up a webserver and make the database accessible:

If you start up the server and visit http://localhost:8080/test/$metadata then you’ll find the metadata.


When you see a message on the console saying: “Should be empty: []” please ignore it. It comes from ‘q’ promise and it is harmless.

Let’s create a simple client for our new service:

Download JayData from http://jaydata.codeplex.com/ and unzip it under scripts folder, download datajs from http://datajs.codeplex.com/, after copying the files, scripts folder should look like this:


Let’s write a simple client:

Open http://localhost:8080/index.html look at the console. You won’t find much there as the database is empty. Let’s put some data into the database:


Refresh the browser and the products will be listed on the console.

Having a direct connection from the browser to the database is really powerful but also raises questions about security. In JayStorm we’ve implemented a security middleware.

Of course, you can use your new OData endpoint from other OData clients including .Net, Java, Excel, mobile devices, etc. You can even you it as a REST endpoint so AJAX clients can also be served.

You can find more examples on how to use JayData http://jaydata.org/examples/index.html and on a new site http://jslq.spirit.de/ created by our friend Rainer.

Finally two pieces of good news:
1. If you want to use OData with database backend without setting up your own environment, you can use our infrastructure at a very competitive price. Just visit jaystack.com and sign up for JayStorm service which gives you all of this and much more.
2. We’re working on a prebuilt solution, which can be run on all the popular virtualization platforms including VMWare, HyperV, VirtualBox, AWS, Azure.

, , , ,