Using JayData in Node.JS environment

Author: Hajnalka Battancs May 30th, 2012


Database management on the client-side is not always enough for your needs and contains some security issues. But with the power of node.js, you can use the JavaScript Language Query (JSLQ) even on the server-side. You can combine the features of Nnode.js like WebSockets with the simplicity of JayData.

JayData module installation

NPM is the tool you will use to install the JayData module. It is very simple, especially if you have experience with the node.js module installation procedure.


That’s it, only one line of command, and the module is up and ready to go! You just need to require the module in your code, which is also very simple.

Using JayData in node.js

On the screenshot, you can see a test application running in node.js. The application is only modified to include the JayData node.js module.




Because node.js use the awesome V8 JavaScript engine, JayData behaves exactly as in the browser. You have to define a context and choose your favorite provider. If you already have a client-side application, you don’t have to rewrite the database managing code parts.

Request logger example application

To demonstrate the combined power of JayData and node.js, a request logger is a perfect use case. The goal is to log every request sent to the server and you can use the query string to filter the server log and get the query result. You start the server in node.js as usually, by creating an HTTP listener. But before that, we have to initialize our storage manager by declaring the entity context and instantiating it. When it’s ready, we can start to listen to the HTTP requests. On every request, we create a new instance of the Request entity, fill up the HTTPHeaders entity set, and finally call the saveChanges function. In the callback function of saveChanges, we can actually start to work as requested. In our scenario, we check the query string. If the user decided to query the log, we further examine the query string for filtering and ordering parameters. Because of the chainable behavior of the JavaScript Language Query (JSLQ), we can chain the required filters together, call the order function and run the final query on the entity set. We simply put the JSON result into the response. Even with the query features, this example is very simple.




The entity context definition is only a few lines of code. Here it is:

A Request and an HTTPHeader entity are declared. Watch out to define the inverseProperty and the required attribute to define the association correctly.

This is the startup code part. We create the entity context instance and in the callback, we start the HTTP listener of node.js. Could this be simpler?

This is the part, where we add the request and the headers to the context. Because of the defined association, you don’t have to handle it manually, just reference the request in the HTTPHeader entity via the Request property.

In the callback of the saveChanges function, we start to process the query string of the request. We chain up the filter expressions and finally execute the toArray function. We write the result as stringified JSON to the response stream and we are done.