I have to deal with large amounts of code developed in an old technology : Netscape IWS server side javascript (SSJS). I realized that with a few tricks and automatic conversions you can make the SSJS code work in Node.js almost unchanged. The problem I am having is that SSJS queries to the database are written in a synchronous style:
var cursor=database.cursor(sqlRequest);
while(cursor.next())
{
//do the stuff
}
The solution I would like to try is to declare prototypes for database
and cursor
SSJS native objects and to mimic their behavior with node-mysql :
function Database() {
// initialize database connection
}
Database.prototype.cursor = function(sqlRequest) {
//here is the difficult part: how to access the query result
//without providing a callback function ?
return new Cursor(data);
};
function Cursor(data) {...}
Cursor.prototype.next = function() {..};
global.database= new Database();
You see that in order to write the body of the Database.cursor
function I should get to use connection.query
in a synchronous way.
I know that the topic was discussed many times (here, here or here) and that it is not a good idea to force nodejs to behave synchronously but in my case it would save a huge refactoring of existing code...