Skip to content
Michael J. Ryan edited this page May 21, 2015 · 1 revision

azureUtil.sql(name)

The azure-config-util object instance presents an sql(name) method that returns a promise, that will resolve to an mssql-ng connection instance.

This method uses storage(...) to establish an azure-storage-simple connection wrapper.

Required Modules

In order to use the sql(name) method, you will have to install the following modules in addition to azure-config-util

  • mssql-ng - the wrapper this method returns
  • mssql - peer dependency for mssql-ng

Example

Table Values

Section Key JsonValue
sql myconnection {...}
  • The shown "name" field in the JsonValue is optional when it matches the Key.

JsonValue

The JsonValue should be a configuration object for mssql connections.

Code

var sql = require('mssql-ng');
var db = await azureUtil.sql('myconnection');

.query template processor

\\ simple query template processor, returns promise for a result
db.query`
 
    --you can specify output parameters (name, type, initialValue) 
    SET ${sql.output('param1', sql.NVarChar(sql.MAX)} = 'output param value'
 
    --input parameters can be auto-detected, or explicit
    SELECT ${someVariable1} [Test1],
           ${sql.input('param2', sql.Decimal, someNumberVariable)} [Test2]
 
`.then(function(result){
  
  //request parameters attached to result.parameters 
  console.log('output @param1', result.parameters.param1.value);
 
  //recordset/rows attached to result.recordset 
  console.log('row 1, column 1', result.recordset[0].Test1); 
  console.log('row 1, column 2', result.recordset[0].Test2);
 
}).catch(console.error);

.queryStream template procesor

db.queryStream`
  SELECT *
  FROM SomeTable
  WHERE Foo=${someBar}
    AND Baz=${true}
    AND Something <= ${new Date()}
`.then(function(request){
  return new Promise(function(resolve,reject){
    recordStream
      .pipe(itemProcessor)
      .on('error',reject)
      .on('end',resolve)
      .resume();
  });
})
.catch(function(err){
  console.error(err);
  process.exit(200);
});

Instance is a connection

var request = new sql.Request(db);
...

Clone this wiki locally