Jan Blaha

Blog about software development

OData server on node.js

node.js

At the very beginning when I started to work on jsreport I decided to give it a standard based API. The decision was based on the fact I didn't want to design my own API syntax for doing queries like give me an entity by name, give my the top five last modified entities or give me just entity ids. For this kind of requests OData has already very nice syntax with bunch of clients in bunch of languages so the work for the consumers is very simple. The other stuff what OData provides like batching requests or entity relationships is rather too much for me but the plain queries are very nice so why not use it.

I quickly found jaydata library which provides JavaScript OData client as well as server libraries. It was looking great at the first glance, but unfortunately it was a huge pain for me to use it over a year. The mongodb based odata server it provides is super slow. It for example creates a complete new mongo client with new connection pool for every request. The client provides a nice unit of work pattern but it is something what I don't need in the browser and it just makes troubles. The jaydata team didn't answer any of my questions about the mongo server so I was forced to fork jaydata and fix or workaround very often. Finally I decided to abandon jaydata and write my own OData server. I took me couple of weeks but now it's here, with free MIT license, ready for you.

The project is stored in github repository node-simple-odata-server

npm install simple-odata-server

var express = require('express');
var app = express();
var ODataServer = require("odata-simple-server");
var MongoClient = require('mongodb').MongoClient;

MongoClient.connect(url, function(err, db) {
    odataServer = new ODataServer()
                    .model(/** odata model **/)
                    .onMongo(function(cb) { cb(err, db); }); 
});

app.use('/odata', odataServer.handle.bind(odataServer);
app.listen(3000);

Now you are ready to serve OData requests you like. It supports mongodb, nedb or even your custom datasource. Check it out on github for details.

last blog posts


09-12-2017 18:40 jsreport

Quite some time ago I blogged about rendering pdf reports in c#. Recently we have added excel reports into jsreport and it was released with a little delay also into .NET. This means you should be able to use both html-to-xlsx and xlsx recipes to create excel files from your .NET environments now.

read more

09-10-2015 14:09 AWS

Such a very common thing like adding an existing external volume to Amazon elastic beanstalk is not easily supported out of the box. The official blog mentions only how to attach a snapshot or how to attach and overwrite a new volume every time the service starts. It took me a while to make the config file actually adding an existing volume without formatting it every time so I share it here with you...

read more

04-10-2015 14:09 jsreport

The best practice when adding email notifications feature to your system is to separate as much as you can from email body assembling to email sending outside of the core system. The emails templates quite likely often changes and you don't want to deploy the system because of every single notification change. The best is to just separate everything into an external system and give the access to your PR or Marketing department so they change emails as the time goes without affecting the core system.

read more

Jan Blaha

About author

Hi! My name is Jan Blaha. I'm software developer and startup enthusiastic. See my current work.