How to set up a nodejs OData endpoint with odata-server

After your very positive feedback to server-side JayData implementation we decided to help you to create OData-enabled endpoints with much less code by releasing the odata-server npm module.
OData-server module comes handy if want to create a node.js service with standard communication protocols (RESTful OData with XMLS/JSON support). The system comes with an out-of-the-box MongoDB data provider, but you can use SQLite or extend the service with your custom JayData data provider. The beauty of the module is that you can use JayData JavaScript Language Query to manipulate object on the server-side, just as you do with JayData library on the client-side – while writing developer-friendly, pure JavaScript code.

We show how will you able to create your CRUD-capable OData endpoint with 4 calls:

You can use odata-server to publish JavaScript Data Services, which is similar to IQueryable results of WCF Data Services; or JavaScript API Services similar to WCF service methods.

Installing odata-server

The module can be found in npm, so you just have to type the following command:

1

npm install odata-server

Warming up the odata-server

The module comes with two example services: newsreader and northwind, let’s launch our todo one.
After starting the services with node todo.js command, the OData endpoint will be published with initialized MongoDB schema, this means you can access your services by opening the following URL in your browser:

http://localhost:52999/todo

Todo example explained

As a client-side JayData application, server-side services begin with the definition of typed entities.

After defining the Todo entity, the second step is to declare the EntityContext to enlist the sets of your entities. As we don’t have multiple objects and object relations, we will have the Todos EntitySet here:

1

2

3

$data.EntityContext.extend(<span class="str">"TodoDatabase"</span>,{

Todos:{type:$data.EntitySet,elementType:Todo}

});

Initializing your todo service takes only two lines of as $data.createODataServer() function encapsulates all the necessary operations based on 4 parameters:

After starting the service code saved to your todo.js you will have a working OData endpoint, which can be accessed using JayData or any other OData client. Have you ever seen any easier way to set up an OData end-point? Okay-okay, JayStorm PaaS doesn’t count! 🙂

The odata-server is compatible with Linux and Windows platforms and you can host it on your own infrastructure or in cloud IaaS like Microsoft Windows Azure or Amazon Web Services.

It’s important to mention that filtering of the records happens on the server by translating the JavaScript .filter() expression to OData query string and JayData parses the results to typed entities, so you can work with results as objects.

Get JayData

JayData on the Social Networks

Copyright JayStack Technologies co. Ltd. 2012.-2018. All Rights Reserved - Privacy policy
Non-JayStack logos/trademarks are copyright to their respective owners and do not constitute an endorsement of any JayStack products.