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 trustedConnection?: boolean | undefined;
169}
170
171export declare var pool: ConnectionPool;
172
173export interface PoolOpts<T> extends Omit<PoolOptions<T>, 'create' | 'destroy' | 'min' | 'max'> {
174 create?: CallbackOrPromise<T> | undefined;
175 destroy?: ((resource: T) => any) | undefined;
176 min?: number | undefined;
177 max?: number | undefined;
178}
179
180export interface config {
181 driver?: string | undefined;
182 user?: string | undefined;
183 password?: string | undefined;
184 server: string;
185 port?: number | undefined;
186 domain?: string | undefined;
187 database?: string | undefined;
188 connectionTimeout?: number | undefined;
189 requestTimeout?: number | undefined;
190 stream?: boolean | undefined;
191 parseJSON?: boolean | undefined;
192 options?: IOptions | undefined;
193 pool?: PoolOpts<Connection> | undefined;
194 arrayRowMode?: boolean | undefined;
195 authentication?: tds.ConnectionAuthentication | undefined;
196 /**
197 * Invoked before opening the connection. The parameter conn is the configured
198 * tedious Connection. It can be used for attaching event handlers.
199 */
200 beforeConnect?: ((conn: Connection) => void) | undefined
201}
202
203export declare class MSSQLError extends Error {
204 constructor(message: Error | string, code?: string);
205 public code: string;
206 public name: string;
207 public originalError?: Error | undefined;
208}
209
210export declare class ConnectionPool extends events.EventEmitter {
211 public readonly connected: boolean;
212 public readonly connecting: boolean;
213 public readonly healthy: boolean;
214 public readonly driver: string;
215 public readonly size: number;
216 public readonly available: number;
217 public readonly pending: number;
218 public readonly borrowed: number;
219 public readonly pool: Pool<Connection>;
220 public static parseConnectionString(connectionString: string): config;
221 public constructor(config: config, callback?: (err?: any) => void);
222 public constructor(connectionString: string, callback?: (err?: any) => void);
223 public query(command: string): Promise<IResult<any>>;
224 public query(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
225 public query<Entity>(command: string): Promise<IResult<Entity>>;
226 public query<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
227 public query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
228 public batch(batch: string): Promise<IResult<any>>;
229 public batch(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
230 public batch(batch: string, callback: (err?: Error, recordset?: IResult<any>) => void): void;
231 public batch<Entity>(batch: string): Promise<IResult<Entity>>;
232 public batch<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
233 public connect(): Promise<ConnectionPool>;
234 public connect(callback: (err: any) => void): void;
235 public close(): Promise<void>;
236 public close(callback: (err: any) => void): void;
237 public request(): Request;
238 public transaction(): Transaction;
239}
240
241export declare class ConnectionError extends MSSQLError {}
242
243export interface IColumnOptions {
244 nullable?: boolean | undefined;
245 primary?: boolean | undefined;
246 identity?: boolean | undefined;
247 readOnly?: boolean | undefined;
248 length?: number | undefined
249}
250
251export interface IColumn extends ISqlType {
252 name: string;
253 nullable: boolean;
254 primary: boolean;
255}
256
257declare class columns extends Array<IColumn> {
258 public add(name: string, type: (() => ISqlType) | ISqlType, options?: IColumnOptions): number;
259}
260
261type IRow = (string | number | boolean | Date | Buffer | undefined | null)[];
262
263declare class rows extends Array<IRow> {
264 public add(...row: IRow): number;
265}
266
267export declare class Table {
268 public create: boolean;
269 public columns: columns;
270 public rows: rows;
271 public constructor(tableName?: string);
272 public schema?: string | undefined;
273 public database?: string | undefined;
274 public name?: string | undefined;
275 public path?: string | undefined;
276 public temporary?: boolean | undefined;
277}
278
279interface IRequestParameters {
280 [name: string]: {
281 name: string;
282 type: (() => ISqlType) | ISqlType;
283 io: number;
284 value: any;
285 length: number;
286 scale: number;
287 precision: number;
288 tvpType: any;
289 }
290}
291
292/**
293 * Options object to be passed through to driver (currently tedious only)
294 */
295export interface IBulkOptions {
296 /** Honors constraints during bulk load, using T-SQL CHECK_CONSTRAINTS. (default: false) */
297 checkConstraints?: boolean | undefined;
298 /** Honors insert triggers during bulk load, using the T-SQL FIRE_TRIGGERS. (default: false) */
299 fireTriggers?: boolean | undefined;
300 /** Honors null value passed, ignores the default values set on table, using T-SQL KEEP_NULLS. (default: false) */
301 keepNulls?: boolean | undefined;
302 /** Places a bulk update(BU) lock on table while performing bulk load, using T-SQL TABLOCK. (default: false) */
303 tableLock?: boolean | undefined;
304}
305
306export declare class Request extends events.EventEmitter {
307 public transaction: Transaction;
308 public pstatement: PreparedStatement;
309 public parameters: IRequestParameters;
310 public verbose: boolean;
311 public canceled: boolean;
312 public multiple: boolean;
313 public stream: any;
314 public constructor(connection?: ConnectionPool);
315 public constructor(transaction: Transaction);
316 public constructor(preparedStatement: PreparedStatement);
317 public execute(procedure: string): Promise<IProcedureResult<any>>;
318 public execute<Entity>(procedure: string): Promise<IProcedureResult<Entity>>;
319 public execute<Entity>(procedure: string, callback: (err?: any, recordsets?: IProcedureResult<Entity>, returnValue?: any) => void): void;
320 public input(name: string, value: any): Request;
321 public input(name: string, type: (() => ISqlType) | ISqlType, value: any): Request;
322 public replaceInput(name: string, value: any): Request;
323 public replaceInput(name: string, type: (() => ISqlType) | ISqlType, value: any): Request;
324 public output(name: string, type: (() => ISqlType) | ISqlType, value?: any): Request;
325 public pipe(stream: NodeJS.WritableStream): NodeJS.WritableStream;
326 public query(command: string): Promise<IResult<any>>;
327 public query(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
328 public query<Entity>(command: string): Promise<IResult<Entity>>;
329 public query<Entity>(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
330 public query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
331 public batch(batch: string): Promise<IResult<any>>;
332 public batch(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
333 public batch(batch: string, callback: (err?: Error, recordset?: IResult<any>) => void): void;
334 public batch<Entity>(batch: string): Promise<IResult<Entity>>;
335 public batch<Entity>(strings: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
336 public batch<Entity>(batch: string, callback: (err?: any, recordset?: IResult<Entity>) => void): void;
337 public bulk(table: Table): Promise<IBulkResult>;
338 public bulk(table: Table, options: IBulkOptions): Promise<IBulkResult>;
339 public bulk(table: Table, callback: (err: Error, result: IBulkResult) => void): void;
340 public bulk(table: Table, options: IBulkOptions, callback: (err: Error, result: IBulkResult) => void): void;
341 public cancel(): void;
342 public pause(): boolean;
343 public resume(): boolean;
344}
345
346export declare class RequestError extends MSSQLError {
347 public number?: number | undefined;
348 public lineNumber?: number | undefined;
349 public state?: string | undefined;
350 public class?: string | undefined;
351 public serverName?: string | undefined;
352 public procName?: string | undefined;
353}
354
355export declare class Transaction extends events.EventEmitter {
356 public isolationLevel: IIsolationLevel;
357 public constructor(connection?: ConnectionPool);
358 /**
359 * Begin a transaction.
360 * @param [isolationLevel] - Controls the locking and row versioning behavior of TSQL statements issued by a connection.
361 * @param [callback] A callback which is called after transaction has began, or an error has occurred. If omited, method returns Promise.
362 */
363 public begin(isolationLevel?: IIsolationLevel): Promise<Transaction>;
364 public begin(isolationLevel?: IIsolationLevel, callback?: (err?: ConnectionError | TransactionError) => void): Transaction;
365 public commit(): Promise<void>;
366 public commit(callback: (err?: any) => void): void;
367 public rollback(): Promise<void>;
368 public rollback(callback: (err?: any) => void): void;
369 public request(): Request;
370}
371
372export declare class TransactionError extends MSSQLError {}
373
374export declare class PreparedStatement extends events.EventEmitter {
375 public transaction: Transaction;
376 public prepared: boolean;
377 public statement: string;
378 public parameters: IRequestParameters;
379 public stream: any;
380 public constructor(connection?: ConnectionPool);
381 public constructor(transaction: Transaction);
382 public input(name: string, type: (() => ISqlType) | ISqlType): PreparedStatement;
383 public output(name: string, type: (() => ISqlType) | ISqlType): PreparedStatement;
384 public prepare(statement?: string): Promise<void>;
385 public prepare(statement?: string, callback?: (err?: Error) => void): PreparedStatement;
386 public execute(values: Object): Promise<IProcedureResult<any>>;
387 public execute<Entity>(values: Object): Promise<IProcedureResult<Entity>>;
388 public execute(values: Object, callback: (err?: Error, result?: IProcedureResult<any>) => void): Request;
389 public execute<Entity>(values: Object, callback: (err?: Error, result?: IProcedureResult<Entity>) => void): Request;
390 public unprepare(): Promise<void>;
391 public unprepare(callback: (err?: Error) => void): PreparedStatement;
392}
393
394export declare class PreparedStatementError extends MSSQLError {}
395
396/**
397 * Open global connection pool.
398 * @param config Connection configuration object or connection string
399 */
400export declare function connect(config: config | string): Promise<ConnectionPool>;
401
402/**
403 * Open global connection pool.
404 * @param config Connection configuration object or connection string.
405 * @param callback A callback which is called after connection has established, or an error has occurred
406 */
407export declare function connect(config: config | string, callback?: (err?: Error) => void): void;
408
409
410export declare function query(command: string): Promise<IResult<any>>;
411export declare function query(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<any>>;
412export declare function query<Entity>(command: string): Promise<IResult<Entity>>;
413export declare function query<Entity>(command: TemplateStringsArray, ...interpolations: any[]): Promise<IResult<Entity>>;
414export declare function query<Entity>(command: string, callback: (err?: Error, recordset?: IResult<Entity>) => void): void;
415
\No newline at end of file