mssql object
The feature described in this topic is available only in preview. To use this feature and other new Windows Azure capabilities, sign up for the free preview.
Provides functionality for working directly with tables in the SQL Database using Transact-SQL.
This object contains the following members:
Methods
open
| Syntax | Description |
|---|---|
|
|
Opens a SQL connection. The connection is passed as an argument to the success handler. |
query
| Syntax | Description |
|---|---|
|
|
Executes the specified sql. Results are passed to the success callback on the options object. |
|
|
Executes the specified sql, with question mark (?) placeholders replaced with values from the params array. Results are passed to the success callback on the options object. |
queryRaw
| Syntax | Description |
|---|---|
|
|
Executes the specified sql. Results are passed to the success callback on the options object in a raw format. |
|
|
Executes the specified sql, with question mark (?) placeholders replaced with values from the params array. Results are passed to the success callback on the options object in a raw format. |
The global mssql object differs from the Microsoft Driver for Node.js for SQL Server in the following ways
-
You do not need to pass the connection string; Windows Azure Mobile Services does this for you.
-
Success and error callbacks are separate and specified as properties of an options object.
The following example executes a query that returns a single record form the statusupdates table
mssql.query('select top 1 * from statusupdates', { success: function(results) { console.log(results); } });
The following example implements custom authorization by reading permissions for each user from the permissions table. The placeholder (?) is replaced with the supplied parameter when the query is executed.
function insert(item, user, request) { var sql = "SELECT _id FROM permissions WHERE userId = ? AND permission = 'submit order'"; mssql.query(sql, [user.userId], { success: function(results) { if (results.length > 0) { // Permission record was found. Continue normal execution. request.execute(); } else { console.log('User %s attempted to submit an order without permissions.', user.userId); request.respond(statusCodes.FORBIDDEN, 'You do not have permission to submit orders.'); } } }); }
The following example manually opens a connection to the SQL Database. The connection is passed as an argument to the success handler.
function insert(item, user, request) { mssql.open({ success: function(connection) { connection.query(//query to execute); connection.query(//query to execute) // etc } } }