[mssql] add typed queries & batches

This commit is contained in:
Pierre Anctil
2015-12-22 17:24:19 +01:00
parent 4c41545030
commit ddf001276e
2 changed files with 25 additions and 3 deletions
+20 -2
View File
@@ -3,6 +3,10 @@
import sql = require('mssql');
interface Entity{
value: number;
}
var config: sql.config = {
user: 'user',
password: 'password',
@@ -33,6 +37,18 @@ var connection: sql.Connection = new sql.Connection(config, function (err: any)
}
});
getArticlesQuery = "SELECT 1 as value FROM TABLE";
requestQuery.query<Entity>(getArticlesQuery, function (err, recordSet) {
if (err) {
console.error('Error happened calling Query: ' + err.name + " " + err.message);
}
// checking to see if the articles returned as at least one.
else if (recordSet.length > 0 && recordSet[0].value) {
}
});
var requestStoredProcedure = new sql.Request(connection);
var testId: number = 0;
var testString: string = 'test';
@@ -109,8 +125,10 @@ function test_promise_returns() {
var request = new sql.Request();
request.batch('create procedure #temporary as select * from table').then((recordset) => { });
request.batch<Entity>('create procedure #temporary as select * from table;select 1 as value').then((recordset) => { });
request.bulk(new sql.Table("table_name")).then(() => { });
request.query('SELECT 1').then((recordset) => { });
request.query<Entity>('SELECT 1 as value').then(res => { });
request.execute('procedure_name').then((recordset) => { });
}
@@ -120,7 +138,7 @@ function test_request_constructor() {
var connection: sql.Connection = new sql.Connection(config);
var preparedStatment = new sql.PreparedStatement(connection);
var transaction = new sql.Transaction(connection);
var request1 = new sql.Request(connection);
var request2 = new sql.Request(preparedStatment);
var request3 = new sql.Request(transaction);
@@ -141,4 +159,4 @@ function test_classes_extend_eventemitter() {
request.on('error', () => { });
preparedStatment.on('error', () => { })
}
}
+5 -1
View File
@@ -7,7 +7,7 @@
/// <reference path="../es6-promise/es6-promise.d.ts" />
declare module "mssql" {
import events = require('events');
import events = require('events');
type sqlTypeWithNoParams = { type: sqlTypeFactoryWithNoParams }
type sqlTypeWithLength = { type: sqlTypeFactoryWithLength, length: number }
@@ -206,9 +206,13 @@ declare module "mssql" {
public output(name: string, type: any, value?: any): void;
public pipe(stream: NodeJS.WritableStream): void;
public query(command: string): Promise<void>;
public query<Entity>(command: string): Promise<Entity[]>;
public query(command: string, callback: (err?: any, recordset?: any) => void): void;
public query<Entity>(command: string, callback: (err?: any, recordset?: Entity[]) => void): void;
public batch(batch: string): Promise<recordSet>;
public batch<Entity>(batch: string): Promise<Entity[]>;
public batch(batch: string, callback: (err?: any, recordset?: any) => void): void;
public batch<Entity>(batch: string, callback: (err?: any, recordset?: Entity[]) => void): void;
public bulk(table: Table): Promise<void>;
public bulk(table: Table, callback: (err: any, rowCount: any) => void): void;
public cancel(): void;