Node-mssql: Asymetrical interfaces between the query() methods

Created on 18 Oct 2017  路  3Comments  路  Source: tediousjs/node-mssql

There seems to be an asymetrical interface between the query() method on the global sql instance and the query() method on a Request instance.

This prevents the use of a string variable as parameter of the query() method on the global sql instance. Only a tagged template literal seems to work.

See below for an example of the four different ways to query, of which 3 work and one doesn't.

let sql = require('mssql');

(async () => {
    try {
        const pool = await sql.connect('mssql://sa:secret@my-server/my-database');
        var requestQuery = new sql.Request(pool);

        const q = `SELECT * FROM mytable`;

        // This doesn't work (Error: Could not find stored procedure 'S')
        const result1 = await sql.query(q); 
        console.dir(result1);

        // This works
        const result2 = await sql.query`SELECT * FROM mytable`;
        console.dir(result2);

        // This works
        const result3 = await requestQuery.query(q);
        console.dir(result3);

        // This works
        const result4 = await requestQuery.query`SELECT * FROM mytable`
        console.dir(result4);

    } catch (err) { 
        // ... error checks
        console.error('Error', err)
    }
})();
bug

Most helpful comment

Fixed and to be released in 4.2.

All 3 comments

+1 I have this issue as well

So crazy. Even for a simple wrapper like this needs a workaround.

async function _sql(...args) {
    var result;

    if(args[0][0].length > 1) { // bad condition to check whatever it is template literal
        // is it with template literal? If so, use db.
        result = await db.query.apply(db, args);
    } else {
         // if not use request, otherwise it won't work!
        var req = db.request();
        var result = await req.query(args[0]);
    }

    return result.recordset;
}

Fixed and to be released in 4.2.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danpetitt picture danpetitt  路  3Comments

linvi picture linvi  路  3Comments

brilvio picture brilvio  路  3Comments

sizovilya picture sizovilya  路  3Comments

andrewmcgivery picture andrewmcgivery  路  5Comments