forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.d.ts
490 lines (402 loc) · 16.7 KB
/
mysql.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Type definitions for node-mysql
// Project: https://github.com/felixge/node-mysql
// Definitions by: William Johnston <https://github.com/wjohnsto>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
///<reference path='../node/node.d.ts' />
declare module "mysql" {
import stream = require("stream");
function createConnection(connectionUri: string): IConnection;
function createConnection(config: IConnectionConfig): IConnection;
function createPool(config: IPoolConfig): IPool;
function createPoolCluster(config?: IPoolClusterConfig): IPoolCluster;
function escape(value: any): string;
function format(sql: string): string;
function format(sql: string, values: Array<any>): string;
interface IMySql {
createConnection(connectionUri: string): IConnection;
createConnection(config: IConnectionConfig): IConnection;
createPool(config: IPoolConfig): IPool;
createPoolCluster(config?: IPoolClusterConfig): IPoolCluster;
escape(value: any): string;
format(sql: string): string;
format(sql: string, values: Array<any>): string;
}
interface IConnectionStatic {
createQuery(sql: string): IQuery;
createQuery(sql: string, callback: (err: IError, ...args: any[]) => void): IQuery;
createQuery(sql: string, values: Array<any>): IQuery;
createQuery(sql: string, values: Array<any>, callback: (err: IError, ...args: any[]) => void): IQuery;
}
interface IConnection {
config: IConnectionConfig;
threadId: number;
beginTransaction(callback: (err: IError) => void): void;
connect(): void;
connect(callback: (err: IError, ...args: any[]) => void): void;
connect(options: any, callback?: (err: IError, ...args: any[]) => void): void;
commit(callback: (err: IError) => void): void;
changeUser(options: IConnectionOptions): void;
changeUser(options: IConnectionOptions, callback: (err: IError) => void): void;
query: IQueryFunction;
end(): void;
end(callback: (err: IError, ...args: any[]) => void): void;
end(options: any, callback: (err: IError, ...args: any[]) => void): void;
destroy(): void;
pause(): void;
release(): void;
resume(): void;
escape(value: any): string;
escapeId(value: string): string;
escapeId(values: Array<string>): string;
format(sql: string): string;
format(sql: string, values: Array<any>): string;
on(ev: string, callback: (...args: any[]) => void): IConnection;
on(ev: 'error', callback: (err: IError) => void): IConnection;
rollback(callback: () => void): void;
}
interface IPool {
config: IPoolConfig;
getConnection(callback: (err: IError, connection: IConnection) => void): void;
query: IQueryFunction;
on(ev: string, callback: (...args: any[]) => void): IPool;
on(ev: 'connection', callback: (connection: IConnection) => void): IPool;
on(ev: 'error', callback: (err: IError) => void): IPool;
}
interface IPoolCluster {
config: IPoolClusterConfig;
add(config: IPoolConfig): void;
add(group: string, config: IPoolConfig): void;
end(): void;
getConnection(callback: (err: IError, connection: IConnection) => void): void;
getConnection(group: string, callback: (err: IError, connection: IConnection) => void): void;
getConnection(group: string, selector: string, callback: (err: IError, connection: IConnection) => void): void;
of(pattern: string): IPool;
of(pattern: string, selector: string): IPool;
on(ev: string, callback: (...args: any[]) => void): IPoolCluster;
on(ev: 'remove', callback: (nodeId: number) => void): IPoolCluster;
on(ev: 'connection', callback: (connection: IConnection) => void): IPoolCluster;
on(ev: 'error', callback: (err: IError) => void): IPoolCluster;
}
interface IQuery {
/**
* The SQL for a constructed query
*/
sql: string;
/**
* Emits a query packet to start the query
*/
start(): void;
/**
* Determines the packet class to use given the first byte of the packet.
*
* @param firstByte The first byte of the packet
* @param parser The packet parser
*/
determinePacket(firstByte: number, parser: any): any;
/**
* Creates a Readable stream with the given options
*
* @param options The options for the stream.
*/
stream(options: IStreamOptions): stream.Readable;
/**
* Pipes a stream downstream, providing automatic pause/resume based on the
* options sent to the stream.
*
* @param options The options for the stream.
*/
pipe(callback: (...args: any[]) => void): IQuery;
on(ev: string, callback: (...args: any[]) => void): IQuery;
on(ev: 'error', callback: (err: IError) => void): IQuery;
on(ev: 'fields', callback: (fields: any, index: number) => void): IQuery;
on(ev: 'result', callback: (row: any, index: number) => void): IQuery;
on(ev: 'end', callback: () => void): IQuery;
}
interface IQueryFunction {
(sql: string): IQuery;
(sql: string, callback: (err: IError, ...args: any[]) => void): IQuery;
(sql: string, values: Array<any>): IQuery;
(sql: string, values: Array<any>, callback: (err: IError, ...args: any[]) => void): IQuery;
(sql: string, values: any): IQuery;
(sql: string, values: any, callback: (err: IError, ...args: any[]) => void): IQuery;
(options: IQueryOptions): IQuery;
(options: IQueryOptions, callback: (err: IError, ...args: any[]) => void): IQuery;
(options: IQueryOptions, values: Array<any>): IQuery;
(options: IQueryOptions, values: Array<any>, callback: (err: IError, ...args: any[]) => void): IQuery;
(options: IQueryOptions, values: any): IQuery;
(options: IQueryOptions, values: any, callback: (err: IError, ...args: any[]) => void): IQuery;
}
interface IQueryOptions {
/**
* The SQL for the query
*/
sql: string;
/**
* Every operation takes an optional inactivity timeout option. This allows you to specify appropriate timeouts for
* operations. It is important to note that these timeouts are not part of the MySQL protocol, and rather timeout
* operations through the client. This means that when a timeout is reached, the connection it occurred on will be
* destroyed and no further operations can be performed.
*/
timeout?: number;
/**
* Either a boolean or string. If true, tables will be nested objects. If string (e.g. '_'), tables will be
* nested as tableName_fieldName
*/
nestTables?: any;
/**
* Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
* to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
*
* You can also specify a function (field: any, next: () => void) => {} to do the type casting yourself.
*
* WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.
*
* field.string()
* field.buffer()
* field.geometry()
*
* are aliases for
*
* parser.parseLengthCodedString()
* parser.parseLengthCodedBuffer()
* parser.parseGeometryValue()
*
* You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
*/
typeCast?: any;
}
interface IStreamOptions {
/**
* Sets the max buffer size in objects of a stream
*/
highWaterMark?: number;
/**
* The object mode of the stream (Default: true)
*/
objectMode?: any;
}
interface IConnectionOptions {
/**
* The MySQL user to authenticate as
*/
user?: string;
/**
* The password of that MySQL user
*/
password?: string;
/**
* Name of the database to use for this connection
*/
database?: string;
/**
* The charset for the connection. This is called "collation" in the SQL-level of MySQL (like utf8_general_ci).
* If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used.
* (Default: 'UTF8_GENERAL_CI')
*/
charset?: string;
}
interface IConnectionConfig extends IConnectionOptions {
/**
* The hostname of the database you are connecting to. (Default: localhost)
*/
host?: string;
/**
* The port number to connect to. (Default: 3306)
*/
port?: number;
/**
* The source IP address to use for TCP connection
*/
localAddress?: string;
/**
* The path to a unix domain socket to connect to. When used host and port are ignored
*/
socketPath?: string;
/**
* The timezone used to store local dates. (Default: 'local')
*/
timezone?: string;
/**
* The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds)
*/
connectTimeout?: number;
/**
* Stringify objects instead of converting to values. (Default: 'false')
*/
stringifyObjects?: boolean;
/**
* Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false)
*/
insecureAuth?: boolean;
/**
* Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
* to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
*
* You can also specify a function (field: any, next: () => void) => {} to do the type casting yourself.
*
* WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.
*
* field.string()
* field.buffer()
* field.geometry()
*
* are aliases for
*
* parser.parseLengthCodedString()
* parser.parseLengthCodedBuffer()
* parser.parseGeometryValue()
*
* You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
*/
typeCast?: any;
/**
* A custom query format function
*/
queryFormat?: (query: string, values: any) => void;
/**
* When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option
* (Default: false)
*/
supportBigNumbers?: boolean;
/**
* Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be
* always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving
* bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately
* represented with [JavaScript Number objects] (http://ecma262-5.com/ELS5_HTML.htm#Section_8.5)
* (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects.
* This option is ignored if supportBigNumbers is disabled.
*/
bigNumberStrings?: boolean;
/**
* Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date
* objects. (Default: false)
*/
dateStrings?: boolean;
/**
* This will print all incoming and outgoing packets on stdout.
* You can also restrict debugging to packet types by passing an array of types (strings) to debug;
*
* (Default: false)
*/
debug?: any;
/**
* Generates stack traces on Error to include call site of library entrance ("long stack traces"). Slight
* performance penalty for most calls. (Default: true)
*/
trace?: boolean;
/**
* Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
*/
multipleStatements?: boolean;
/**
* List of connection flags to use other than the default ones. It is also possible to blacklist default ones
*/
flags?: Array<string>;
/**
* object with ssl parameters or a string containing name of ssl profile
*/
ssl?: any;
}
interface IPoolConfig extends IConnectionConfig {
/**
* The milliseconds before a timeout occurs during the connection acquisition. This is slightly different from connectTimeout,
* because acquiring a pool connection does not always involve making a connection. (Default: 10 seconds)
*/
acquireTimeout?: number;
/**
* Determines the pool's action when no connections are available and the limit has been reached. If true, the pool will queue
* the connection request and call it when one becomes available. If false, the pool will immediately call back with an error.
* (Default: true)
*/
waitForConnections?: boolean;
/**
* The maximum number of connections to create at once. (Default: 10)
*/
connectionLimit?: number;
/**
* The maximum number of connection requests the pool will queue before returning an error from getConnection. If set to 0, there
* is no limit to the number of queued connection requests. (Default: 0)
*/
queueLimit?: number;
}
interface IPoolClusterConfig {
/**
* If true, PoolCluster will attempt to reconnect when connection fails. (Default: true)
*/
canRetry?: boolean;
/**
* If connection fails, node's errorCount increases. When errorCount is greater than removeNodeErrorCount,
* remove a node in the PoolCluster. (Default: 5)
*/
removeNodeErrorCount?: number;
/**
* The default selector. (Default: RR)
* RR: Select one alternately. (Round-Robin)
* RANDOM: Select the node by random function.
* ORDER: Select the first node available unconditionally.
*/
defaultSelector?: string;
}
interface ISslCredentials {
/**
* A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates
*/
pfx?: string;
/**
* A string holding the PEM encoded private key
*/
key?: string;
/**
* A string of passphrase for the private key or pfx
*/
passphrase?: string;
/**
* A string holding the PEM encoded certificate
*/
cert?: string;
/**
* Either a string or list of strings of PEM encoded CA certificates to trust.
*/
ca?: Array<string>;
/**
* Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List)
*/
crl?: Array<string>;
/**
* A string describing the ciphers to use or exclude
*/
ciphers?: string;
}
interface IError extends Error {
/**
* Either a MySQL server error (e.g. 'ER_ACCESS_DENIED_ERROR'),
* a node.js error (e.g. 'ECONNREFUSED') or an internal error
* (e.g. 'PROTOCOL_CONNECTION_LOST').
*/
code: string;
/**
* The error number for the error code
*/
errno: number;
/**
* The sql state marker
*/
sqlStateMarker?: string;
/**
* The sql state
*/
sqlState?: string;
/**
* The field count
*/
fieldCount?: number;
/**
* The stack trace for the error
*/
stack?: string;
/**
* Boolean, indicating if this error is terminal to the connection object.
*/
fatal: boolean;
}
}