Reputation: 29166
I am developing an application whose front-end is built using jquery mobile. This application requires database access to fetch various information. It will also need to update the database.
To sync my application front-end, I am thinking of using node.js as a middle tier. The choice of node.js was made because we want it to scale properly. This middle tier will serve the front-end with all the required data that it needs. It will also receive data from the front-end and update the back-end database.
The problem is the data that will have pretty complex structure and relations. Converting the data model to and from plain JSON by hand can become a real pain. So I was thinking of using some other module/framework also. I tried with Jaydata so that I can form data models and then expose them using OpenData protocol, but the documentation of it seems to be lacking. I also though about using Sequelize but I think it doesn't have pretty good support for database at this point (triggers, procedures and all).
So what can I use at this point to fulfill my requirements? Is there any node module/framework which fulfills my requirements or do I have to use raw code to fetch data from the back-end, convert it to proper models and serve them?
I am using Postgresql as my back-end database. For the time being my front-end is made using jquery mobile but I may choose to use different technology (PHP/ASP.NET) to build it.
Upvotes: 3
Views: 3646
Reputation: 11740
UPDATE: there are a lot of improvement with JayData Server for the NodeJS platform, for example navigation properties are supported with MongoDB now. Also using the system is super easy now, read more on how to set up your own MongoDB back middle tier server.
You could indeed use JayData for that, provided that it's limitations are within your acceptance range:
On the server side JayData runs as a NodeJS package and supports sqLite and mongoDB. With 1.3 coming in January mysql and MSSqlServer support will be added. JayData actually exposes the server side database as an OData V2 compliant endpoint with V3 support coming in January.
SqLite has navigation properties but mongoDB has performance. For production size implementation only mongoDB is the viable way. The limitation is that currently JayData does not support automatic relations (navigation properties) with mongoDB. I can confirm that navigation properties will be supported with the mongoDB provider in 1.3
What you are able to achieve here is to have a complex server side model that has reference fields for implementing 1..n and 1..1 relations but you will not be able to query based on navigation properties nor will you be able to use the "Include" operator to server-shape deep trees.
I am preparing a small example on how to expose a model on a NodeJS/mongoDB platform with JayData and ammend it here.
UPDATE: alternatively you could implemented your own Data API with JavaScript API Services (that's a lower level API in JayData) that exposes API surfaces as an OData endpoint with Service Functions. Check out this: http://jaystack.com/blog/create-your-first-online-api-with-javascript-api-services . (Please note that the article refers to the JayStorm service multiple times but the techniques described apply to a local configuration too.
Here are the steps to fire up your own JayData backed NodeJS middle tier layer with either JavaScript Data Services or with JavaScript API Services. Code you publish with JayData will be published with the OData protocol and using JayData on the client would turn the result into typed result trees.
Please note that the OData protocol layer is a Connect/Express middleware so you must be using either of them to use JayData as server.
Preparations on a clean linux system, most steps might not be needed for you
bash$
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
mkdir test; cd test
npm install mongodb express http connect q node-uuid bcrypt xmldom qs
npm install jaydata
1) Create and boot up an API Service class without mongoDB backend.
/*** begin server.js ***/
var c = require('express');
require('jaydata');
window.DOMParser=require('xmldom').DOMParser;
require('q');
$data.ServiceBase.extend("myapi", {
helloWorld: function() {
///<returns type="string" />
return "Hello JavaScript Server World";
}
});
myapi.annotateFromVSDoc();
var app = c();
app.use(c.query());
app.use(c.bodyParser());
app.use("/test", $data.JayService.OData.Utils.simpleBodyReader());
app.use("/test", $data.JayService.createAdapter(myapi, function (req, res) {
return new myapi();
}));
app.listen(8080);
/*** end server.js ***/
Call helloWorld with:
bash$: curl http://localhost:8080/test/helloWorld
The blog post mentioned above shows more from this point
2) Create a mongoDB backed JavaScript Data Service
/* begin server.js */
var c = require('express');
require('jaydata');
window.DOMParser=require('xmldom').DOMParser;
require('q');
var app = c();
app.use(c.query());
app.use(c.bodyParser());
app.use(c.cookieParser());
app.use(c.methodOverride());
$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 }
});
test.Context.annotateFromVSDoc();
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.errorHandler());
app.listen(8080);
/* end server.js */
Since the "test" database is exposed with the OData protocol you can do CRUD operations from any AJAX tool, but I recommend datajs or JayData.
Also note that with the Data Service classes you can define server side event handlers that can alter the data to be inserted or cancel the operation (much like triggers in the SQL world). You can also pass in authorization logic to the JayData server context to enforce user based rules and access control.
Upvotes: 6