From 5ac3ef67bac39d4f69abed92450357fd5de1302b Mon Sep 17 00:00:00 2001 From: Ben Farr Date: Sun, 13 Sep 2015 21:36:04 +0100 Subject: [PATCH] Return Promises Methods return a promises if the callback is omitted. --- mssql/mssql-tests.ts | 23 ++++++++++++++++ mssql/mssql.d.ts | 64 +++++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/mssql/mssql-tests.ts b/mssql/mssql-tests.ts index 29dcd3421..d4cf834b0 100644 --- a/mssql/mssql-tests.ts +++ b/mssql/mssql-tests.ts @@ -90,3 +90,26 @@ function test_table() { table.rows.add('name2', 7, 3.14); } + +function test_promise_returns() { + // Methods return a promises if the callback is omitted. + var connection: sql.Connection = new sql.Connection(config); + connection.connect().then(() => { }); + connection.close().then(() => { }); + + var preparedStatment = new sql.PreparedStatement(connection); + preparedStatment.prepare("SELECT @myValue").then(() => { }); + preparedStatment.execute({ myValue: 1 }).then((recordSet) => { }); + preparedStatment.unprepare().then(() => { }); + + var transaction = new sql.Transaction(connection); + transaction.begin().then(() => { }); + transaction.commit().then(() => { }); + transaction.rollback().then(() => { }); + + var request = new sql.Request(); + request.batch('create procedure #temporary as select * from table').then((recordset) => { }); + request.bulk(new sql.Table("table_name")).then(() => { }); + request.query('SELECT 1').then((recordset) => { }); + request.execute('procedure_name').then((recordset) => { }); +} diff --git a/mssql/mssql.d.ts b/mssql/mssql.d.ts index 7a7d79f6d..f5bdd72c5 100644 --- a/mssql/mssql.d.ts +++ b/mssql/mssql.d.ts @@ -3,6 +3,8 @@ // Definitions by: COLSA Corporation // Definitions: https://github.com/borisyankov/DefinitelyTyped +/// + declare module "mssql" { export var Date: any; @@ -47,12 +49,15 @@ declare module "mssql" { export var map: { js: any, sql: any }[]; export var DRIVERS: string[]; + type recordSet = any; + type IIsolationLevel = number; + export var ISOLATION_LEVEL: { - READ_UNCOMMITTED: number - READ_COMMITTED: number - REPEATABLE_READ: number - SERIALIZABLE: number - SNAPSHOT: number + READ_UNCOMMITTED: IIsolationLevel + READ_COMMITTED: IIsolationLevel + REPEATABLE_READ: IIsolationLevel + SERIALIZABLE: IIsolationLevel + SNAPSHOT: IIsolationLevel } export interface IOptions { @@ -87,9 +92,11 @@ declare module "mssql" { public constructor(config: config, callback?: (err?: any) => void); - public connect(callback?: (err?: any) => void): void; + public connect(): Promise; + public connect(callback: (err: any) => void): void; - public close(): void; + public close(): Promise; + public close(callback: (err: any) => void): void; } class columns { @@ -110,32 +117,41 @@ declare module "mssql" { export class Request { public constructor(connection?: Connection); - public execute(procedure: string, callback?: (err?: any, recordsets?: any, returnValue?: any) => void): void; - public input(name: string, value: any): void; - public input(name: string, type: any, value: any): void; - public output(name: string, type: any, value?: any): void; public pipe(stream: any): void; - public query(command: string, callback?: (err?: any, recordset?: any) => void): void; - public batch(batch: string, callback?: (err?: any, recordset?: any) => void): void; - public bulk(table: Table, callback?: (err?: any, rowCount?: any) => void): void; - public cancel(): void; - public parameters: any; + execute(procedure: string): Promise; + execute(procedure: string, callback: (err?: any, recordsets?: any, returnValue?: any) => void): void; + input(name: string, value: any): void; + input(name: string, type: any, value: any): void; + output(name: string, type: any, value?: any): void; + query(command: string): Promise; + query(command: string, callback: (err?: any, recordset?: any) => void): void; + batch(batch: string): Promise; + batch(batch: string, callback: (err?: any, recordset?: any) => void): void; + bulk(table: Table): Promise; + bulk(table: Table, callback: (err: any, rowCount: any) => void): void; + cancel(): void; + parameters: any; } export class Transaction { - public constructor(connection?: Connection); - public begin(isolationLevel?: any, callback?: (err?: any) => void): void; - public begin(callback?: (err?: any) => void): void; - public commit(callback?: (err?: any) => void): void; - public rollback(callback?: (err?: any) => void): void; + public constructor(connection: Connection); + public begin(isolationLevel?: IIsolationLevel): Promise; + public begin(isolationLevel?: IIsolationLevel, callback?: (err?: any) => void): void; + public commit(): Promise; + public commit(callback: (err?: any) => void): void; + public rollback(): Promise; + public rollback(callback: (err?: any) => void): void; } export class PreparedStatement { public constructor(connection?: Connection); public input(name: string, type: any): void; public output(name: string, type: any): void; - public prepare(statement: string, callback?: (err?: any) => void): void; - public execute(values: any, callback?: (err?: any) => void): void; - public unprepare(callback?: (err?: any) => void): void; + public prepare(statement?: string): Promise; + public prepare(statement?: string, callback?: (err?: any) => void): void; + public execute(values: Object): Promise; + public execute(values: Object, callback: (err: any, recordSet: recordSet) => void): void; + public unprepare(): Promise; + public unprepare(callback: (err?: any) => void): void; } }