From 5dc27752e49efac4540158817fa08e640646f474 Mon Sep 17 00:00:00 2001 From: Peter Keuter Date: Mon, 1 May 2017 16:36:19 +0200 Subject: [PATCH] update mssql definitions to 4.0.1 (#15909) * update mssql definitions to 4.0.1 * updated tests * support es6 tagged template literals * implemented connectionstring constructor * updated tests --- types/mssql/index.d.ts | 57 +++++++++++++++++++++----------------- types/mssql/mssql-tests.ts | 25 +++++++++++------ 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/types/mssql/index.d.ts b/types/mssql/index.d.ts index 5f013e33a1..80dbf2cf43 100644 --- a/types/mssql/index.d.ts +++ b/types/mssql/index.d.ts @@ -1,6 +1,6 @@ -// Type definitions for mssql 3.3 +// Type definitions for mssql 4.0.1 // Project: https://www.npmjs.com/package/mssql -// Definitions by: COLSA Corporation , Ben Farr , Vitor Buzinaro , Matt Richardson , Jørgen Elgaard Larsen +// Definitions by: COLSA Corporation , Ben Farr , Vitor Buzinaro , Matt Richardson , Jørgen Elgaard Larsen , Peter Keuter // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -8,7 +8,7 @@ import events = require('events'); export interface ISqlType { - type: ISqlTypeFactory; + type: ISqlTypeFactory; } export interface ISqlTypeWithNoParams extends ISqlType { type: ISqlTypeFactoryWithNoParams } export interface ISqlTypeWithLength extends ISqlType { type: ISqlTypeFactoryWithLength; length: number } @@ -97,7 +97,6 @@ export declare var MAX: number; export declare var fix: boolean; export declare var Promise: any; - interface IMap extends Array<{ js: any, sql: any }> { register(jstype: any, sql: any): void; } @@ -110,10 +109,19 @@ export interface IColumnMetadata { index: number; name: string; length: number; - type: ISqlType; + type: ISqlType; udt?: any; } } +export interface IResult { + recordsets: IRecordSet[]; + recordset: IRecordSet; + rowsAffected: number[], + output: { [key: string]: any }; +} +export interface IProcedureResult extends IResult { + returnValue: any; +} export interface IRecordSet extends Array { columns: IColumnMetadata; } @@ -162,15 +170,17 @@ export interface config { pool?: IPool; } -export declare class Connection extends events.EventEmitter { +export declare class ConnectionPool extends events.EventEmitter { public connected: boolean; public connecting: boolean; public driver: string; public constructor(config: config, callback?: (err?: any) => void); - public connect(): Promise; + public constructor(connectionString: string, callback?: (err?: any) => void); + public connect(): Promise; public connect(callback: (err: any) => void): void; public close(): Promise; public close(callback: (err: any) => void): void; + public request(): Request; } export declare class ConnectionError implements Error { @@ -209,32 +219,30 @@ interface IRequestParameters { } export declare class Request extends events.EventEmitter { - public connection: Connection; public transaction: Transaction; public pstatement: PreparedStatement; public parameters: IRequestParameters; public verbose: boolean; - public multiple: boolean; public canceled: boolean; public stream: any; - public constructor(connection?: Connection); + public constructor(connection?: ConnectionPool); public constructor(transaction: Transaction); public constructor(preparedStatement: PreparedStatement); - public execute(procedure: string): Promise>; - public execute(procedure: string): Promise>; - public execute(procedure: string, callback: (err?: any, recordsets?: IRecordSet, returnValue?: any, rowsAffected?: number) => void): void; + public execute(procedure: string): Promise>; + public execute(procedure: string): Promise>; + public execute(procedure: string, callback: (err?: any, recordsets?: IProcedureResult, returnValue?: any) => void): void; public input(name: string, value: any): Request; public input(name: string, type: any, value: any): Request; public output(name: string, type: any, value?: any): Request; public pipe(stream: NodeJS.WritableStream): NodeJS.WritableStream; - public query(command: string): Promise>; - public query(command: string): Promise>; - public query(command: string, callback: (err?: Error, recordset?: IRecordSet, rowsAffected?: number) => void): void; - public query(command: string, callback: (err?: Error, recordset?: IRecordSet) => void): void; - public batch(batch: string): Promise>; - public batch(batch: string): Promise>; - public batch(batch: string, callback: (err?: Error, recordset?: IRecordSet) => void): void; - public batch(batch: string, callback: (err?: any, recordset?: IRecordSet) => void): void; + public query(strings: TemplateStringsArray, ...interpolations: any[]): Promise>; + public query(command: string): Promise>; + public query(command: string): Promise>; + public query(command: string, callback: (err?: Error, recordset?: IResult) => void): void; + public batch(batch: string): Promise>; + public batch(batch: string): Promise>; + public batch(batch: string, callback: (err?: Error, recordset?: IResult) => void): void; + public batch(batch: string, callback: (err?: any, recordset?: IResult) => void): void; public bulk(table: Table): Promise; public bulk(table: Table, callback: (err: Error, rowCount: any) => void): void; public cancel(): void; @@ -248,9 +256,8 @@ export declare class RequestError implements Error { } export declare class Transaction extends events.EventEmitter { - public connection: Connection; public isolationLevel: IIsolationLevel; - public constructor(connection?: Connection); + public constructor(connection?: ConnectionPool); public begin(isolationLevel?: IIsolationLevel): Promise; public begin(isolationLevel?: IIsolationLevel, callback?: (err?: any) => void): void; public commit(): Promise; @@ -267,14 +274,12 @@ export declare class TransactionError implements Error { } export declare class PreparedStatement extends events.EventEmitter { - public connection: Connection; public transaction: Transaction; public prepared: boolean; public statement: string; public parameters: IRequestParameters; - public multiple: boolean; public stream: any; - public constructor(connection?: Connection); + public constructor(connection?: ConnectionPool); public input(name: string, type: ISqlType): PreparedStatement; public output(name: string, type: ISqlType): PreparedStatement; public prepare(statement?: string): Promise; diff --git a/types/mssql/mssql-tests.ts b/types/mssql/mssql-tests.ts index 764f6da8e1..4bda996f85 100644 --- a/types/mssql/mssql-tests.ts +++ b/types/mssql/mssql-tests.ts @@ -1,7 +1,7 @@ import sql = require('mssql'); interface Entity{ - value: number; + value: number; } var config: sql.config = { @@ -15,7 +15,13 @@ var config: sql.config = { } } -var connection: sql.Connection = new sql.Connection(config, function (err: any) { +var connectionStringTest: sql.ConnectionPool = new sql.ConnectionPool("connectionstring", (err) => { + if (err) { + return err; + } +}); + +var connection: sql.ConnectionPool = new sql.ConnectionPool(config, function (err: any) { if (err != null) { console.warn("Issue with connecting to SQL Server!"); } @@ -24,25 +30,25 @@ var connection: sql.Connection = new sql.Connection(config, function (err: any) var getArticlesQuery = "SELECT * FROM TABLE"; - requestQuery.query(getArticlesQuery, function (err, recordSet) { + requestQuery.query(getArticlesQuery, function (err, result) { 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) { + else if (result.recordset.length > 0) { } }); getArticlesQuery = "SELECT 1 as value FROM TABLE"; - requestQuery.query(getArticlesQuery, function (err, recordSet) { + requestQuery.query(getArticlesQuery, function (err, result) { 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) { + else if (result.recordset.length > 0 && result.recordset[0].value) { } }); @@ -124,7 +130,7 @@ function test_table() { function test_promise_returns() { // Methods return a promises if the callback is omitted. - var connection: sql.Connection = new sql.Connection(config); + var connection: sql.ConnectionPool = new sql.ConnectionPool(config); connection.connect().then(() => { }); connection.close().then(() => { }); @@ -144,13 +150,14 @@ function test_promise_returns() { request.bulk(new sql.Table("table_name")).then(() => { }); request.query('SELECT 1').then((recordset) => { }); request.query('SELECT 1 as value').then(res => { }); + request.query`SELECT ${1} as value`.then(res => { }); request.execute('procedure_name').then((recordset) => { }); } function test_request_constructor() { // Request can be constructed with a connection, preparedStatment, transaction or no arguments - var connection: sql.Connection = new sql.Connection(config); + var connection: sql.ConnectionPool = new sql.ConnectionPool(config); var preparedStatment = new sql.PreparedStatement(connection); var transaction = new sql.Transaction(connection); @@ -161,7 +168,7 @@ function test_request_constructor() { } function test_classes_extend_eventemitter() { - var connection: sql.Connection = new sql.Connection(config); + var connection: sql.ConnectionPool = new sql.ConnectionPool(config); var transaction = new sql.Transaction(); var request = new sql.Request(); var preparedStatment = new sql.PreparedStatement();