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
This commit is contained in:
Peter Keuter
2017-05-01 16:36:19 +02:00
committed by Andy
parent de52c9ccca
commit 5dc27752e4
2 changed files with 47 additions and 35 deletions

View File

@@ -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 <http://www.colsa.com/>, Ben Farr <https://github.com/jaminfarr>, Vitor Buzinaro <https://github.com/buzinas>, Matt Richardson <https://github.com/mrrichar/>, Jørgen Elgaard Larsen <https://github.com/elhaard/>
// Definitions by: COLSA Corporation <http://www.colsa.com/>, Ben Farr <https://github.com/jaminfarr>, Vitor Buzinaro <https://github.com/buzinas>, Matt Richardson <https://github.com/mrrichar/>, Jørgen Elgaard Larsen <https://github.com/elhaard/>, Peter Keuter <https://github.com/pkeuter/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
@@ -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<T> {
recordsets: IRecordSet<T>[];
recordset: IRecordSet<T>;
rowsAffected: number[],
output: { [key: string]: any };
}
export interface IProcedureResult<T> extends IResult<T> {
returnValue: any;
}
export interface IRecordSet<T> extends Array<T> {
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<Connection>;
public constructor(connectionString: string, callback?: (err?: any) => void);
public connect(): Promise<ConnectionPool>;
public connect(callback: (err: any) => void): void;
public close(): Promise<void>;
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<IRecordSet<any>>;
public execute<Entity>(procedure: string): Promise<IRecordSet<Entity>>;
public execute<Entity>(procedure: string, callback: (err?: any, recordsets?: IRecordSet<Entity>, returnValue?: any, rowsAffected?: number) => void): void;
public execute(procedure: string): Promise<IProcedureResult<any>>;
public execute<Entity>(procedure: string): Promise<IProcedureResult<Entity>>;
public execute<Entity>(procedure: string, callback: (err?: any, recordsets?: IProcedureResult<Entity>, 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<IRecordSet<any>>;
public query<Entity>(command: string): Promise<IRecordSet<Entity>>;
public query<Entity>(command: string, callback: (err?: Error, recordset?: IRecordSet<Entity>, rowsAffected?: number) => void): void;
public query<Entity>(command: string, callback: (err?: Error, recordset?: IRecordSet<Entity>) => void): void;
public batch(batch: string): Promise<IRecordSet<any>>;
public batch<Entity>(batch: string): Promise<IRecordSet<Entity>>;
public batch(batch: string, callback: (err?: Error, recordset?: IRecordSet<any>) => void): void;
public batch<Entity>(batch: string, callback: (err?: any, recordset?: IRecordSet<Entity>) => void): void;
public query(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
public query(command: string): Promise<IResult<any>>;
public query<Entity>(command: string): Promise<IResult<Entity>>;
public query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
public batch(batch: string): Promise<IResult<any>>;
public batch<Entity>(batch: string): Promise<IResult<Entity>>;
public batch(batch: string, callback: (err?: Error, recordset?: IResult<any>) => void): void;
public batch<Entity>(batch: string, callback: (err?: any, recordset?: IResult<Entity>) => void): void;
public bulk(table: Table): Promise<number>;
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<void>;
public begin(isolationLevel?: IIsolationLevel, callback?: (err?: any) => void): void;
public commit(): Promise<void>;
@@ -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<void>;

View File

@@ -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<Entity>(getArticlesQuery, function (err, recordSet) {
requestQuery.query<Entity>(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<Entity>('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();