I am new to node and development in general. I am looking to build an 
application using node and publishing 2-3 endpoints for standard CRUD ops 
against a SQL database. My current project is setup as an App Service in 
Azure. To get started, I created my first GET method. Azure stubbed this 
out for me into a /API folder. It created two files <apiname>.js and 
<apiname>.json. Inside, it has the proper method for me to start writing 
code. 

I have the GET method working. It takes in standard ODATA query params and 
transforms into a SQL select statement and serves up the data in JSON.  My 
question is this....

Should the require('mssql'), config and connect statements really be inside 
the GET? That would mean that I have to repeat the config (connection) 
settings in each method on each endpoint. Where would be the best place to 
put this? Also, are simple sql.connect calls ok for large loads or do I 
need to consider connection pools, etc?  My GET method is below.

module.exports = {
    "get": function (req, res, next) {
        
        var tableConfig = {
    "name": "Meals",
    "schema":"dbo",
    "flavor": "mssql",
    "softDelete":true
    };
     
    var query = {
    "resultLimit": req.query.$top,
    "selections": req.query.$select,
    "filters": req.query.$filter,
    "ordering": req.query.$orderBy
    };
     
    var odQuery = require('azure-odata-sql').format(query, tableConfig);
    var sqlQry = odQuery[0].sql;
        
        var sql = require("mssql");

        // config for your database
        var config = {
            user: 'zumoadmin',
            password: '**********',
            server: '*******.database.windows.net', 
            database: '*********',
            options: {
                encrypt: true
            }
        };
    

        sql.connect(config, function (err) {
        
            if (err) console.log(err);
    

            var request = new sql.Request();  
 
            for (var i = 0; i < odQuery[0].parameters.length; i++) {
                console.log(odQuery[0].parameters[i]);
                request.input(odQuery[0].parameters[i].name, 
odQuery[0].parameters[i].value);
            }   

            request.query(sqlQry, function (err, recordset) {
                
                if (err) console.log(err);
    

                var data = {
                    "results"   : recordset,
                    "rowcount"  : recordset.length 
                };
                res.json(data);
                
            });
        });
    },
};





-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/f701e59e-7f49-479b-8927-bba6e5c29ca8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to