UNPKG

18.1 kBTypeScriptView Raw
1// Type definitions for mssql 8.0.0
2// Project: https://www.npmjs.com/package/mssql
3// Definitions by: COLSA Corporation <http://www.colsa.com/>
4// Jørgen Elgaard Larsen <https://github.com/elhaard>
5// Peter Keuter <https://github.com/pkeuter>
6// Jeff Wooden <https://github.com/woodenconsulting>
7// Cahil Foley <https://github.com/cahilfoley>
8// Rifa Achrinza <https://github.com/achrinza>
9// Daniel Hensby <https://github.com/dhensby>
10// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11// TypeScript Version: 3.6
12
13/// <reference types="node" />
14
15
16import events = require('events');
17import tds = require('tedious');
18import { Pool } from 'tarn';
19import { CallbackOrPromise, PoolOptions } from 'tarn/dist/Pool';
20export interface ISqlType {
21 type: ISqlTypeFactory;
22}
23export interface ISqlTypeWithNoParams extends ISqlType { type: ISqlTypeFactoryWithNoParams }
24export interface ISqlTypeWithLength extends ISqlType { type: ISqlTypeFactoryWithLength; length: number }
25export interface ISqlTypeWithScale extends ISqlType { type: ISqlTypeFactoryWithScale; scale: number }
26export interface ISqlTypeWithPrecisionScale extends ISqlType { type: ISqlTypeFactoryWithPrecisionScale; precision: number, scale: number }
27export interface ISqlTypeWithTvpType extends ISqlType { type: ISqlTypeFactoryWithTvpType; tvpType: any }
28
29export interface ISqlTypeFactory {
30}
31export interface ISqlTypeFactoryWithNoParams extends ISqlTypeFactory { (): ISqlTypeWithNoParams; }
32export interface ISqlTypeFactoryWithLength extends ISqlTypeFactory { (length?: number): ISqlTypeWithLength }
33export interface ISqlTypeFactoryWithScale extends ISqlTypeFactory { (scale?: number): ISqlTypeWithScale }
34export interface ISqlTypeFactoryWithPrecisionScale extends ISqlTypeFactory { (precision?: number, scale?: number): ISqlTypeWithPrecisionScale; }
35export interface ISqlTypeFactoryWithTvpType extends ISqlTypeFactory { (tvpType?: any): ISqlTypeWithTvpType }
36
37
38export declare var VarChar: ISqlTypeFactoryWithLength;
39export declare var NVarChar: ISqlTypeFactoryWithLength;
40export declare var Text: ISqlTypeFactoryWithNoParams;
41export declare var Int: ISqlTypeFactoryWithNoParams;
42export declare var BigInt: ISqlTypeFactoryWithNoParams;
43export declare var TinyInt: ISqlTypeFactoryWithNoParams;
44export declare var SmallInt: ISqlTypeFactoryWithNoParams;
45export declare var Bit: ISqlTypeFactoryWithNoParams;
46export declare var Float: ISqlTypeFactoryWithNoParams;
47export declare var Numeric: ISqlTypeFactoryWithPrecisionScale;
48export declare var Decimal: ISqlTypeFactoryWithPrecisionScale;
49export declare var Real: ISqlTypeFactoryWithNoParams;
50export declare var Date: ISqlTypeFactoryWithNoParams;
51export declare var DateTime: ISqlTypeFactoryWithNoParams;
52export declare var DateTime2: ISqlTypeFactoryWithScale;
53export declare var DateTimeOffset: ISqlTypeFactoryWithScale;
54export declare var SmallDateTime: ISqlTypeFactoryWithNoParams;
55export declare var Time: ISqlTypeFactoryWithScale;
56export declare var UniqueIdentifier: ISqlTypeFactoryWithNoParams;
57export declare var SmallMoney: ISqlTypeFactoryWithNoParams;
58export declare var Money: ISqlTypeFactoryWithNoParams;
59export declare var Binary: ISqlTypeFactoryWithNoParams;
60export declare var VarBinary: ISqlTypeFactoryWithLength;
61export declare var Image: ISqlTypeFactoryWithNoParams;
62export declare var Xml: ISqlTypeFactoryWithNoParams;
63export declare var Char: ISqlTypeFactoryWithLength;
64export declare var NChar: ISqlTypeFactoryWithLength;
65export declare var NText: ISqlTypeFactoryWithNoParams;
66export declare var TVP: ISqlTypeFactoryWithTvpType;
67export declare var UDT: ISqlTypeFactoryWithNoParams;
68export declare var Geography: ISqlTypeFactoryWithNoParams;
69export declare var Geometry: ISqlTypeFactoryWithNoParams;
70export declare var Variant: ISqlTypeFactoryWithNoParams;
71
72export type Connection = tds.Connection;
73
74export declare var TYPES: {
75 VarChar: ISqlTypeFactoryWithLength;
76 NVarChar: ISqlTypeFactoryWithLength;
77 Text: ISqlTypeFactoryWithNoParams;
78 Int: ISqlTypeFactoryWithNoParams;
79 BigInt: ISqlTypeFactoryWithNoParams;
80 TinyInt: ISqlTypeFactoryWithNoParams;
81 SmallInt: ISqlTypeFactoryWithNoParams;
82 Bit: ISqlTypeFactoryWithNoParams;
83 Float: ISqlTypeFactoryWithNoParams;
84 Numeric: ISqlTypeFactoryWithPrecisionScale;
85 Decimal: ISqlTypeFactoryWithPrecisionScale;
86 Real: ISqlTypeFactoryWithNoParams;
87 Date: ISqlTypeFactoryWithNoParams;
88 DateTime: ISqlTypeFactoryWithNoParams;
89 DateTime2: ISqlTypeFactoryWithScale;
90 DateTimeOffset: ISqlTypeFactoryWithScale;
91 SmallDateTime: ISqlTypeFactoryWithNoParams;
92 Time: ISqlTypeFactoryWithScale;
93 UniqueIdentifier: ISqlTypeFactoryWithNoParams;
94 SmallMoney: ISqlTypeFactoryWithNoParams;
95 Money: ISqlTypeFactoryWithNoParams;
96 Binary: ISqlTypeFactoryWithNoParams;
97 VarBinary: ISqlTypeFactoryWithLength;
98 Image: ISqlTypeFactoryWithNoParams;
99 Xml: ISqlTypeFactoryWithNoParams;
100 Char: ISqlTypeFactoryWithLength;
101 NChar: ISqlTypeFactoryWithLength;
102 NText: ISqlTypeFactoryWithNoParams;
103 TVP: ISqlTypeFactoryWithTvpType;
104 UDT: ISqlTypeFactoryWithNoParams;
105 Geography: ISqlTypeFactoryWithNoParams;
106 Geometry: ISqlTypeFactoryWithNoParams;
107 Variant: ISqlTypeFactoryWithNoParams;
108};
109
110export declare var MAX: number;
111export declare var fix: boolean;
112export declare var Promise: any;
113
114interface IMap extends Array<{ js: any, sql: any }> {
115 register(jstype: any, sql: any): void;
116}
117
118export declare var map: IMap;
119
120export declare var DRIVERS: string[];
121export interface IColumnMetadata {
122 [name: string]: {
123 index: number;
124 name: string;
125 length: number;
126 type: (() => ISqlType) | ISqlType;
127 udt?: any;
128 scale?: number | undefined;
129 precision?: number | undefined;
130 nullable: boolean;
131 caseSensitive: boolean;
132 identity: boolean;
133 readOnly: boolean;
134 }
135}
136export interface IResult<T> {
137 recordsets: IRecordSet<T>[];
138 recordset: IRecordSet<T>;
139 rowsAffected: number[],
140 output: { [key: string]: any };
141}
142
143export interface IBulkResult {
144 rowsAffected: number;
145}
146
147export interface IProcedureResult<T> extends IResult<T> {
148 returnValue: any;
149}
150export interface IRecordSet<T> extends Array<T> {
151 columns: IColumnMetadata;
152 toTable(name?: string): Table;
153}
154
155type IIsolationLevel = number;
156
157export declare var ISOLATION_LEVEL: {
158 READ_UNCOMMITTED: IIsolationLevel
159 READ_COMMITTED: IIsolationLevel
160 REPEATABLE_READ: IIsolationLevel
161 SERIALIZABLE: IIsolationLevel
162 SNAPSHOT: IIsolationLevel
163}
164
165export interface IOptions extends tds.ConnectionOptions {
166 beforeConnect?: void | undefined;
167 connectionString?: string | undefined;
168 enableArithAbort?: boolean | undefined;
169 instanceName?: string | undefined;
170 trustedConnection?: boolean | undefined;
171 useUTC?: boolean | undefined;
172}
173
174export declare var pool: ConnectionPool;
175
176export interface PoolOpts<T> extends Omit<PoolOptions<T>, 'create' | 'destroy' | 'min' | 'max'> {
177 create?: CallbackOrPromise<T> | undefined;
178 destroy?: ((resource: T) => any) | undefined;
179 min?: number | undefined;
180 max?: number | undefined;
181}
182
183export interface config {
184 driver?: string | undefined;
185 user?: string | undefined;
186 password?: string | undefined;
187 server: string;
188 port?: number | undefined;
189 domain?: string | undefined;
190 database?: string | undefined;
191 connectionTimeout?: number | undefined;
192 requestTimeout?: number | undefined;
193 stream?: boolean | undefined;
194 parseJSON?: boolean | undefined;
195 options?: IOptions | undefined;
196 pool?: PoolOpts<Connection> | undefined;
197 arrayRowMode?: boolean | 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}
347
348export declare class RequestError extends MSSQLError {
349 public number?: number | undefined;
350 public lineNumber?: number | undefined;
351 public state?: string | undefined;
352 public class?: string | undefined;
353 public serverName?: string | undefined;
354 public procName?: string | undefined;
355}
356
357export declare class Transaction extends events.EventEmitter {
358 public isolationLevel: IIsolationLevel;
359 public constructor(connection?: ConnectionPool);
360 /**
361 * Begin a transaction.
362 * @param [isolationLevel] - Controls the locking and row versioning behavior of TSQL statements issued by a connection.
363 * @param [callback] A callback which is called after transaction has began, or an error has occurred. If omited, method returns Promise.
364 */
365 public begin(isolationLevel?: IIsolationLevel): Promise<Transaction>;
366 public begin(isolationLevel?: IIsolationLevel, callback?: (err?: ConnectionError | TransactionError) => void): Transaction;
367 public commit(): Promise<void>;
368 public commit(callback: (err?: any) => void): void;
369 public rollback(): Promise<void>;
370 public rollback(callback: (err?: any) => void): void;
371 public request(): Request;
372}
373
374export declare class TransactionError extends MSSQLError {}
375
376export declare class PreparedStatement extends events.EventEmitter {
377 public transaction: Transaction;
378 public prepared: boolean;
379 public statement: string;
380 public parameters: IRequestParameters;
381 public stream: any;
382 public constructor(connection?: ConnectionPool);
383 public constructor(transaction: Transaction);
384 public input(name: string, type: (() => ISqlType) | ISqlType): PreparedStatement;
385 public output(name: string, type: (() => ISqlType) | ISqlType): PreparedStatement;
386 public prepare(statement?: string): Promise<void>;
387 public prepare(statement?: string, callback?: (err?: Error) => void): PreparedStatement;
388 public execute(values: Object): Promise<IProcedureResult<any>>;
389 public execute<Entity>(values: Object): Promise<IProcedureResult<Entity>>;
390 public execute(values: Object, callback: (err?: Error, result?: IProcedureResult<any>) => void): Request;
391 public execute<Entity>(values: Object, callback: (err?: Error, result?: IProcedureResult<Entity>) => void): Request;
392 public unprepare(): Promise<void>;
393 public unprepare(callback: (err?: Error) => void): PreparedStatement;
394}
395
396export declare class PreparedStatementError extends MSSQLError {}
397
398/**
399 * Open global connection pool.
400 * @param config Connection configuration object or connection string
401 */
402export declare function connect(config: config | string): Promise<ConnectionPool>;
403
404/**
405 * Open global connection pool.
406 * @param config Connection configuration object or connection string.
407 * @param callback A callback which is called after connection has established, or an error has occurred
408 */
409export declare function connect(config: config | string, callback?: (err?: Error) => void): void;
410
411
412export declare function query(command: string): Promise<IResult<any>>;
413export declare function query(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
414export declare function query<Entity>(command: string): Promise<IResult<Entity>>;
415export declare function query<Entity>(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
416export declare function query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
417
\No newline at end of file