UNPKG

18.2 kBTypeScriptView Raw
1// Type definitions for mssql 8.1
2// Project: https://www.npmjs.com/package/mssql
3// Definitions by: Jørgen Elgaard Larsen <https://github.com/elhaard>
4// Peter Keuter <https://github.com/pkeuter>
5// Jeff Wooden <https://github.com/woodenconsulting>
6// Cahil Foley <https://github.com/cahilfoley>
7// Rifa Achrinza <https://github.com/achrinza>
8// Daniel Hensby <https://github.com/dhensby>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10// TypeScript Version: 4.0
11
12// @credit COLSA Corporation <http://www.colsa.com/>
13
14/// <reference types="node" />
15
16
17import events = require('events');
18import tds = require('tedious');
19import { Pool } from 'tarn';
20import { CallbackOrPromise, PoolOptions } from 'tarn/dist/Pool';
21export interface ISqlType {
22 type: ISqlTypeFactory;
23}
24export interface ISqlTypeWithNoParams extends ISqlType { type: ISqlTypeFactoryWithNoParams }
25export interface ISqlTypeWithLength extends ISqlType { type: ISqlTypeFactoryWithLength; length: number }
26export interface ISqlTypeWithScale extends ISqlType { type: ISqlTypeFactoryWithScale; scale: number }
27export interface ISqlTypeWithPrecisionScale extends ISqlType { type: ISqlTypeFactoryWithPrecisionScale; precision: number, scale: number }
28export interface ISqlTypeWithTvpType extends ISqlType { type: ISqlTypeFactoryWithTvpType; tvpType: any }
29
30export interface ISqlTypeFactory {
31}
32export interface ISqlTypeFactoryWithNoParams extends ISqlTypeFactory { (): ISqlTypeWithNoParams; }
33export interface ISqlTypeFactoryWithLength extends ISqlTypeFactory { (length?: number): ISqlTypeWithLength }
34export interface ISqlTypeFactoryWithScale extends ISqlTypeFactory { (scale?: number): ISqlTypeWithScale }
35export interface ISqlTypeFactoryWithPrecisionScale extends ISqlTypeFactory { (precision?: number, scale?: number): ISqlTypeWithPrecisionScale; }
36export interface ISqlTypeFactoryWithTvpType extends ISqlTypeFactory { (tvpType?: any): ISqlTypeWithTvpType }
37
38
39export declare var VarChar: ISqlTypeFactoryWithLength;
40export declare var NVarChar: ISqlTypeFactoryWithLength;
41export declare var Text: ISqlTypeFactoryWithNoParams;
42export declare var Int: ISqlTypeFactoryWithNoParams;
43export declare var BigInt: ISqlTypeFactoryWithNoParams;
44export declare var TinyInt: ISqlTypeFactoryWithNoParams;
45export declare var SmallInt: ISqlTypeFactoryWithNoParams;
46export declare var Bit: ISqlTypeFactoryWithNoParams;
47export declare var Float: ISqlTypeFactoryWithNoParams;
48export declare var Numeric: ISqlTypeFactoryWithPrecisionScale;
49export declare var Decimal: ISqlTypeFactoryWithPrecisionScale;
50export declare var Real: ISqlTypeFactoryWithNoParams;
51export declare var Date: ISqlTypeFactoryWithNoParams;
52export declare var DateTime: ISqlTypeFactoryWithNoParams;
53export declare var DateTime2: ISqlTypeFactoryWithScale;
54export declare var DateTimeOffset: ISqlTypeFactoryWithScale;
55export declare var SmallDateTime: ISqlTypeFactoryWithNoParams;
56export declare var Time: ISqlTypeFactoryWithScale;
57export declare var UniqueIdentifier: ISqlTypeFactoryWithNoParams;
58export declare var SmallMoney: ISqlTypeFactoryWithNoParams;
59export declare var Money: ISqlTypeFactoryWithNoParams;
60export declare var Binary: ISqlTypeFactoryWithNoParams;
61export declare var VarBinary: ISqlTypeFactoryWithLength;
62export declare var Image: ISqlTypeFactoryWithNoParams;
63export declare var Xml: ISqlTypeFactoryWithNoParams;
64export declare var Char: ISqlTypeFactoryWithLength;
65export declare var NChar: ISqlTypeFactoryWithLength;
66export declare var NText: ISqlTypeFactoryWithNoParams;
67export declare var TVP: ISqlTypeFactoryWithTvpType;
68export declare var UDT: ISqlTypeFactoryWithNoParams;
69export declare var Geography: ISqlTypeFactoryWithNoParams;
70export declare var Geometry: ISqlTypeFactoryWithNoParams;
71export declare var Variant: ISqlTypeFactoryWithNoParams;
72
73export type Connection = tds.Connection;
74
75export declare var TYPES: {
76 VarChar: ISqlTypeFactoryWithLength;
77 NVarChar: ISqlTypeFactoryWithLength;
78 Text: ISqlTypeFactoryWithNoParams;
79 Int: ISqlTypeFactoryWithNoParams;
80 BigInt: ISqlTypeFactoryWithNoParams;
81 TinyInt: ISqlTypeFactoryWithNoParams;
82 SmallInt: ISqlTypeFactoryWithNoParams;
83 Bit: ISqlTypeFactoryWithNoParams;
84 Float: ISqlTypeFactoryWithNoParams;
85 Numeric: ISqlTypeFactoryWithPrecisionScale;
86 Decimal: ISqlTypeFactoryWithPrecisionScale;
87 Real: ISqlTypeFactoryWithNoParams;
88 Date: ISqlTypeFactoryWithNoParams;
89 DateTime: ISqlTypeFactoryWithNoParams;
90 DateTime2: ISqlTypeFactoryWithScale;
91 DateTimeOffset: ISqlTypeFactoryWithScale;
92 SmallDateTime: ISqlTypeFactoryWithNoParams;
93 Time: ISqlTypeFactoryWithScale;
94 UniqueIdentifier: ISqlTypeFactoryWithNoParams;
95 SmallMoney: ISqlTypeFactoryWithNoParams;
96 Money: ISqlTypeFactoryWithNoParams;
97 Binary: ISqlTypeFactoryWithNoParams;
98 VarBinary: ISqlTypeFactoryWithLength;
99 Image: ISqlTypeFactoryWithNoParams;
100 Xml: ISqlTypeFactoryWithNoParams;
101 Char: ISqlTypeFactoryWithLength;
102 NChar: ISqlTypeFactoryWithLength;
103 NText: ISqlTypeFactoryWithNoParams;
104 TVP: ISqlTypeFactoryWithTvpType;
105 UDT: ISqlTypeFactoryWithNoParams;
106 Geography: ISqlTypeFactoryWithNoParams;
107 Geometry: ISqlTypeFactoryWithNoParams;
108 Variant: ISqlTypeFactoryWithNoParams;
109};
110
111export declare var MAX: number;
112export declare var fix: boolean;
113export declare var Promise: any;
114
115interface IMap extends Array<{ js: any, sql: any }> {
116 register(jstype: any, sql: any): void;
117}
118
119export declare var map: IMap;
120
121export declare var DRIVERS: string[];
122export interface IColumnMetadata {
123 [name: string]: {
124 index: number;
125 name: string;
126 length: number;
127 type: (() => ISqlType) | ISqlType;
128 udt?: any;
129 scale?: number | undefined;
130 precision?: number | undefined;
131 nullable: boolean;
132 caseSensitive: boolean;
133 identity: boolean;
134 readOnly: boolean;
135 }
136}
137export interface IResult<T> {
138 recordsets: T extends Array<any> ? { [P in keyof T]: IRecordSet<T[P]> } : IRecordSet<T>[];
139 recordset: IRecordSet<T extends Array<any> ? T[0] : T>;
140 rowsAffected: number[],
141 output: { [key: string]: any };
142}
143
144export interface IBulkResult {
145 rowsAffected: number;
146}
147
148export interface IProcedureResult<T> extends IResult<T> {
149 returnValue: any;
150}
151export interface IRecordSet<T> extends Array<T> {
152 columns: IColumnMetadata;
153 toTable(name?: string): Table;
154}
155
156type IIsolationLevel = number;
157
158export declare var ISOLATION_LEVEL: {
159 READ_UNCOMMITTED: IIsolationLevel
160 READ_COMMITTED: IIsolationLevel
161 REPEATABLE_READ: IIsolationLevel
162 SERIALIZABLE: IIsolationLevel
163 SNAPSHOT: IIsolationLevel
164}
165
166export interface IOptions extends Omit<tds.ConnectionOptions, 'useColumnNames'> {
167 beforeConnect?: void | undefined;
168 connectionString?: string | undefined;
169 trustedConnection?: boolean | undefined;
170}
171
172export declare var pool: ConnectionPool;
173
174export interface PoolOpts<T> extends Omit<PoolOptions<T>, 'create' | 'destroy' | 'min' | 'max'> {
175 create?: CallbackOrPromise<T> | undefined;
176 destroy?: ((resource: T) => any) | undefined;
177 min?: number | undefined;
178 max?: number | undefined;
179}
180
181export interface config {
182 driver?: string | undefined;
183 user?: string | undefined;
184 password?: string | undefined;
185 server: string;
186 port?: number | undefined;
187 domain?: string | undefined;
188 database?: string | undefined;
189 connectionTimeout?: number | undefined;
190 requestTimeout?: number | undefined;
191 stream?: boolean | undefined;
192 parseJSON?: boolean | undefined;
193 options?: IOptions | undefined;
194 pool?: PoolOpts<Connection> | undefined;
195 arrayRowMode?: boolean | undefined;
196 authentication?: tds.ConnectionAuthentication | undefined;
197 /**
198 * Invoked before opening the connection. The parameter conn is the configured
199 * tedious Connection. It can be used for attaching event handlers.
200 */
201 beforeConnect?: ((conn: Connection) => void) | undefined
202}
203
204export declare class MSSQLError extends Error {
205 constructor(message: Error | string, code?: string);
206 public code: string;
207 public name: string;
208 public originalError?: Error | undefined;
209}
210
211export declare class ConnectionPool extends events.EventEmitter {
212 public readonly connected: boolean;
213 public readonly connecting: boolean;
214 public readonly healthy: boolean;
215 public readonly driver: string;
216 public readonly size: number;
217 public readonly available: number;
218 public readonly pending: number;
219 public readonly borrowed: number;
220 public readonly pool: Pool<Connection>;
221 public static parseConnectionString(connectionString: string): config;
222 public constructor(config: config, callback?: (err?: any) => void);
223 public constructor(connectionString: string, callback?: (err?: any) => void);
224 public query(command: string): Promise<IResult<any>>;
225 public query(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
226 public query<Entity>(command: string): Promise<IResult<Entity>>;
227 public query<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
228 public query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
229 public batch(batch: string): Promise<IResult<any>>;
230 public batch(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
231 public batch(batch: string, callback: (err?: Error, recordset?: IResult<any>) => void): void;
232 public batch<Entity>(batch: string): Promise<IResult<Entity>>;
233 public batch<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
234 public connect(): Promise<ConnectionPool>;
235 public connect(callback: (err: any) => void): void;
236 public close(): Promise<void>;
237 public close(callback: (err: any) => void): void;
238 public request(): Request;
239 public transaction(): Transaction;
240}
241
242export declare class ConnectionError extends MSSQLError {}
243
244export interface IColumnOptions {
245 nullable?: boolean | undefined;
246 primary?: boolean | undefined;
247 identity?: boolean | undefined;
248 readOnly?: boolean | undefined;
249 length?: number | undefined
250}
251
252export interface IColumn extends ISqlType {
253 name: string;
254 nullable: boolean;
255 primary: boolean;
256}
257
258declare class columns extends Array<IColumn> {
259 public add(name: string, type: (() => ISqlType) | ISqlType, options?: IColumnOptions): number;
260}
261
262type IRow = (string | number | boolean | Date | Buffer | undefined | null)[];
263
264declare class rows extends Array<IRow> {
265 public add(...row: IRow): number;
266}
267
268export declare class Table {
269 public create: boolean;
270 public columns: columns;
271 public rows: rows;
272 public constructor(tableName?: string);
273 public schema?: string | undefined;
274 public database?: string | undefined;
275 public name?: string | undefined;
276 public path?: string | undefined;
277 public temporary?: boolean | undefined;
278}
279
280interface IRequestParameters {
281 [name: string]: {
282 name: string;
283 type: (() => ISqlType) | ISqlType;
284 io: number;
285 value: any;
286 length: number;
287 scale: number;
288 precision: number;
289 tvpType: any;
290 }
291}
292
293/**
294 * Options object to be passed through to driver (currently tedious only)
295 */
296export interface IBulkOptions {
297 /** Honors constraints during bulk load, using T-SQL CHECK_CONSTRAINTS. (default: false) */
298 checkConstraints?: boolean | undefined;
299 /** Honors insert triggers during bulk load, using the T-SQL FIRE_TRIGGERS. (default: false) */
300 fireTriggers?: boolean | undefined;
301 /** Honors null value passed, ignores the default values set on table, using T-SQL KEEP_NULLS. (default: false) */
302 keepNulls?: boolean | undefined;
303 /** Places a bulk update(BU) lock on table while performing bulk load, using T-SQL TABLOCK. (default: false) */
304 tableLock?: boolean | undefined;
305}
306
307export declare class Request extends events.EventEmitter {
308 public transaction: Transaction;
309 public pstatement: PreparedStatement;
310 public parameters: IRequestParameters;
311 public verbose: boolean;
312 public canceled: boolean;
313 public multiple: boolean;
314 public stream: any;
315 public constructor(connection?: ConnectionPool);
316 public constructor(transaction: Transaction);
317 public constructor(preparedStatement: PreparedStatement);
318 public execute(procedure: string): Promise<IProcedureResult<any>>;
319 public execute<Entity>(procedure: string): Promise<IProcedureResult<Entity>>;
320 public execute<Entity>(procedure: string, callback: (err?: any, recordsets?: IProcedureResult<Entity>, returnValue?: any) => void): void;
321 public input(name: string, value: any): Request;
322 public input(name: string, type: (() => ISqlType) | ISqlType, value: any): Request;
323 public replaceInput(name: string, value: any): Request;
324 public replaceInput(name: string, type: (() => ISqlType) | ISqlType, value: any): Request;
325 public output(name: string, type: (() => ISqlType) | ISqlType, value?: any): Request;
326 public pipe(stream: NodeJS.WritableStream): NodeJS.WritableStream;
327 public query(command: string): Promise<IResult<any>>;
328 public query(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
329 public query<Entity>(command: string): Promise<IResult<Entity>>;
330 public query<Entity>(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
331 public query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
332 public batch(batch: string): Promise<IResult<any>>;
333 public batch(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
334 public batch(batch: string, callback: (err?: Error, recordset?: IResult<any>) => void): void;
335 public batch<Entity>(batch: string): Promise<IResult<Entity>>;
336 public batch<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
337 public batch<Entity>(batch: string, callback: (err?: any, recordset?: IResult<Entity>) => void): void;
338 public bulk(table: Table): Promise<IBulkResult>;
339 public bulk(table: Table, options: IBulkOptions): Promise<IBulkResult>;
340 public bulk(table: Table, callback: (err: Error, result: IBulkResult) => void): void;
341 public bulk(table: Table, options: IBulkOptions, callback: (err: Error, result: IBulkResult) => void): void;
342 public cancel(): void;
343 public pause(): boolean;
344 public resume(): boolean;
345}
346
347export declare class RequestError extends MSSQLError {
348 public number?: number | undefined;
349 public lineNumber?: number | undefined;
350 public state?: string | undefined;
351 public class?: string | undefined;
352 public serverName?: string | undefined;
353 public procName?: string | undefined;
354}
355
356export declare class Transaction extends events.EventEmitter {
357 public isolationLevel: IIsolationLevel;
358 public constructor(connection?: ConnectionPool);
359 /**
360 * Begin a transaction.
361 * @param [isolationLevel] - Controls the locking and row versioning behavior of TSQL statements issued by a connection.
362 * @param [callback] A callback which is called after transaction has began, or an error has occurred. If omited, method returns Promise.
363 */
364 public begin(isolationLevel?: IIsolationLevel): Promise<Transaction>;
365 public begin(isolationLevel?: IIsolationLevel, callback?: (err?: ConnectionError | TransactionError) => void): Transaction;
366 public commit(): Promise<void>;
367 public commit(callback: (err?: any) => void): void;
368 public rollback(): Promise<void>;
369 public rollback(callback: (err?: any) => void): void;
370 public request(): Request;
371}
372
373export declare class TransactionError extends MSSQLError {}
374
375export declare class PreparedStatement extends events.EventEmitter {
376 public transaction: Transaction;
377 public prepared: boolean;
378 public statement: string;
379 public parameters: IRequestParameters;
380 public stream: any;
381 public constructor(connection?: ConnectionPool);
382 public constructor(transaction: Transaction);
383 public input(name: string, type: (() => ISqlType) | ISqlType): PreparedStatement;
384 public output(name: string, type: (() => ISqlType) | ISqlType): PreparedStatement;
385 public prepare(statement?: string): Promise<void>;
386 public prepare(statement?: string, callback?: (err?: Error) => void): PreparedStatement;
387 public execute(values: Object): Promise<IProcedureResult<any>>;
388 public execute<Entity>(values: Object): Promise<IProcedureResult<Entity>>;
389 public execute(values: Object, callback: (err?: Error, result?: IProcedureResult<any>) => void): Request;
390 public execute<Entity>(values: Object, callback: (err?: Error, result?: IProcedureResult<Entity>) => void): Request;
391 public unprepare(): Promise<void>;
392 public unprepare(callback: (err?: Error) => void): PreparedStatement;
393}
394
395export declare class PreparedStatementError extends MSSQLError {}
396
397/**
398 * Open global connection pool.
399 * @param config Connection configuration object or connection string
400 */
401export declare function connect(config: config | string): Promise<ConnectionPool>;
402
403/**
404 * Open global connection pool.
405 * @param config Connection configuration object or connection string.
406 * @param callback A callback which is called after connection has established, or an error has occurred
407 */
408export declare function connect(config: config | string, callback?: (err?: Error) => void): void;
409
410
411export declare function query(command: string): Promise<IResult<any>>;
412export declare function query(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
413export declare function query<Entity>(command: string): Promise<IResult<Entity>>;
414export declare function query<Entity>(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
415export declare function query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
416
\No newline at end of file