UNPKG

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