Install your own oData server with nodejs and MongoDB

Author: Gábor Dolla
19 June 2013

node.js, MongoDB, OData, examples

 

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:

 

$data.Class.define("test.Product", $data.Entity, null, {
    Id: { type: "id", key: true, computed: true, nullable: false },
    Name: { type: "string" },
    Price: { type: "integer" }
}, null);
$data.Class.defineEx("test.Context", [$data.EntityContext,$data.ServiceBase], null, {
    Products: { type: $data.EntitySet, elementType: test.Product }
});
exports = test.Context;

 

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

 

var c = require('express');
require('jaydata');
window.DOMParser=require('xmldom').DOMParser;
require('q');
require('./model.js');
var app = c();
app.use(c.query());
app.use(c.bodyParser());
app.use(c.cookieParser());
app.use(c.methodOverride());
app.use(c.session({ secret: 'session key' }));
app.use("/test", $data.JayService.OData.Utils.simpleBodyReader());
app.use("/test", $data.JayService.createAdapter(test.Context, function (req, res) {
    return new test.Context({name: "mongoDB", databaseName:"test", address: "localhost", port: 27017 });
}));
app.use("/", c.static(__dirname));
app.use(c.errorHandler());
app.listen(8080);

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

testsvc

 

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:

 

scripts

 

Let’s write a simple client:

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Test</title>
    <script src="scripts/datajs-1.0.3.js"></script>
    <script src="scripts/jaydata/jaydata.min.js"></script>
    <script src="scripts/jaydata/jaydataproviders/oDataProvider.js"></script>
    <script>
     $data.service('/test', function (contextFactory, contextType) {
       var context = contextFactory();
       context.Products.toArray(function (items) {
         console.log(items);
       });
     });
    </script>
</head>
<body>
</body>
</html>

 

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:

 

mongo

 

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, you can read more about it here: http://jaystack.com/blog/jaystorm-services-overview.

 

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.

Share this

categories: JayData examples, MongoDb, node.js

Comments

  • Missing user avatar
    Humberto

    Hi,
    I'm using Ubuntu Server 12.04 and I'm trying to build an OData endpoint with nodejs and jaydata following the instructions you give but when the instruction require('jaydata'); is executed node returns Should be empty: []

    Am I missing something?

    Thanks in advance.

    Humberto

  • Missing user avatar
    Humberto

    Forgot to mention that I'm using node version 0.8.14 and jaydata 1.2.3

  • Missing user avatar

    Hi
    We do not check comments often so please use our forum for questions.

    'Should be empty: []' on the console comes from a library that we use, q promise.
    It's harmless, causes no problem whatsoever.

    Since the blogpost was published we made some improvements so you can also run it on windows.

    Gabor

  • Missing user avatar
    luke

    Really Powerful!!!

    Do you have git project for the sample code to fork?

    Thanks.

Leave a comment

comments powered by Disqus