UNPKG

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