-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathmysql2.d.ts
1003 lines (828 loc) · 33.6 KB
/
mysql2.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable import/no-duplicates, import/no-unresolved, import/no-extraneous-dependencies */
declare module 'mysql2' {
export interface IQueryReturn<T> {
0: T[];
1: FieldInfo[];
[Symbol.iterator](): T[] | FieldInfo[];
[index: number]: T[] | FieldInfo[];
}
// Type definitions for mysql 2.15
// Project: https://github.com/mysqljs/mysql
// Definitions by: William Johnston <https://github.com/wjohnsto>
// Kacper Polak <https://github.com/kacepe>
// Krittanan Pingclasai <https://github.com/kpping>
// James Munro <https://github.com/jdmunro>
// Sanders DeNardi <https://github.com/sedenardi>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
/// <reference types="node" />
import stream = require('stream');
import tls = require('tls');
export interface EscapeFunctions {
/**
* Escape an untrusted string to be used as a SQL value. Use this on user
* provided data.
* @param value Value to escape
* @param stringifyObjects If true, don't convert objects into SQL lists
* @param timeZone Convert dates from UTC to the given timezone.
*/
escape(value: any, stringifyObjects?: boolean, timeZone?: string): string;
/**
* Escape an untrusted string to be used as a SQL identifier (database,
* table, or column name). Use this on user provided data.
* @param value Value to escape.
* @param forbidQualified Don't allow qualified identifiers (eg escape '.')
*/
escapeId(value: string, forbidQualified?: boolean): string;
/**
* Safely format a SQL query containing multiple untrusted values.
* @param sql Query, with insertion points specified with ? (for values) or
* ?? (for identifiers)
* @param values Array of objects to insert.
* @param stringifyObjects If true, don't convert objects into SQL lists
* @param timeZone Convert dates from UTC to the given timezone.
*/
format(sql: string, values: any[], stringifyObjects?: boolean, timeZone?: string): string;
}
/**
* Escape an untrusted string to be used as a SQL value. Use this on user
* provided data.
* @param value Value to escape
* @param stringifyObjects If true, don't convert objects into SQL lists
* @param timeZone Convert dates from UTC to the given timezone.
*/
export function escape(value: any, stringifyObjects?: boolean, timeZone?: string): string;
/**
* Escape an untrusted string to be used as a SQL identifier (database,
* table, or column name). Use this on user provided data.
* @param value Value to escape.
* @param forbidQualified Don't allow qualified identifiers (eg escape '.')
*/
export function escapeId(value: string, forbidQualified?: boolean): string;
/**
* Safely format a SQL query containing multiple untrusted values.
* @param sql Query, with insertion points specified with ? (for values) or
* ?? (for identifiers)
* @param values Array of objects to insert.
* @param stringifyObjects If true, don't convert objects into SQL lists
* @param timeZone Convert dates from UTC to the given timezone.
*/
export function format(sql: string, values: any[], stringifyObjects?: boolean, timeZone?: string): string;
export function createConnection(connectionUri: string | ConnectionConfig): Connection;
export function createPool(config: PoolConfig | string): Pool;
export function createPoolCluster(config?: PoolClusterConfig): PoolCluster;
/**
* Create a string that will be inserted unescaped with format(), escape().
* Note: the value will still be escaped if used as an identifier (??) by
* format().
* @param sql
*/
export function raw(sql: string): {
toSqlString: () => string
};
export interface Connection extends EscapeFunctions {
config: ConnectionConfig;
state: 'connected' | 'authenticated' | 'disconnected' | 'protocol_error' | string;
authorized: boolean;
threadId: number | null;
createQuery: QueryFunction;
connect(callback?: (err: MysqlError, ...args: any[]) => void): void;
connect(options: any, callback?: (err: MysqlError, ...args: any[]) => void): void;
changeUser(options: ConnectionOptions, callback?: (err: MysqlError) => void): void;
changeUser(callback: (err: MysqlError) => void): void;
beginTransaction(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
beginTransaction(callback: (err: MysqlError) => void): void;
commit(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
commit(callback: (err: MysqlError) => void): void;
rollback(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
rollback(callback: (err: MysqlError) => void): void;
query: QueryFunction;
ping(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
ping(callback: (err: MysqlError) => void): void;
statistics(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
statistics(callback: (err: MysqlError) => void): void;
/**
* Close the connection. Any queued data (eg queries) will be sent first. If
* there are any fatal errors, the connection will be immediately closed.
* @param callback Handler for any fatal error
*/
end(callback?: (err?: MysqlError) => void): void;
end(options: any, callback: (err?: MysqlError) => void): void;
/**
* Close the connection immediately, without waiting for any queued data (eg
* queries) to be sent. No further events or callbacks will be triggered.
*/
destroy(): void;
/**
* Pause the connection. No more 'result' events will fire until resume() is
* called.
*/
pause(): void;
/**
* Resume the connection.
*/
resume(): void;
on(ev: 'drain' | 'connect', callback: () => void): Connection;
/**
* Set handler to be run when the connection is closed.
*/
on(ev: 'end', callback: (err?: MysqlError) => void): Connection;
on(ev: 'fields', callback: (fields: any[]) => void): Connection;
/**
* Set handler to be run when a a fatal error occurs.
*/
on(ev: 'error', callback: (err: MysqlError) => void): Connection;
/**
* Set handler to be run when a callback has been queued to wait for an
* available connection.
*/
// tslint:disable-next-line:unified-signatures
on(ev: 'enqueue', callback: (err?: MysqlError) => void): Connection;
/**
* Set handler to be run on a certain event.
*/
on(ev: string, callback: (...args: any[]) => void): Connection;
}
export interface PoolConnection extends Connection {
release(): void;
/**
* Close the connection. Any queued data (eg queries) will be sent first. If
* there are any fatal errors, the connection will be immediately closed.
* @param callback Handler for any fatal error
*/
end(): void;
/**
* Close the connection immediately, without waiting for any queued data (eg
* queries) to be sent. No further events or callbacks will be triggered.
*/
destroy(): void;
}
export interface Pool extends EscapeFunctions {
config: PoolActualConfig;
getConnection(callback: (err: MysqlError, connection: PoolConnection) => void): void;
acquireConnection(
connection: PoolConnection,
callback: (err: MysqlError, connection: PoolConnection) => void,
): void;
releaseConnection(connection: PoolConnection): void;
/**
* Close the connection. Any queued data (eg queries) will be sent first. If
* there are any fatal errors, the connection will be immediately closed.
* @param callback Handler for any fatal error
*/
end(callback?: (err: MysqlError) => void): void;
query: QueryFunction;
/**
* Set handler to be run when a new connection is made within the pool.
*/
on(ev: 'connection', callback: (connection: PoolConnection) => void): Pool;
/**
* Set handler to be run when a connection is acquired from the pool. This
* is called after all acquiring activity has been performed on the
* connection, right before the connection is handed to the callback of the
* acquiring code.
*/
// tslint:disable-next-line:unified-signatures
on(ev: 'acquire', callback: (connection: PoolConnection) => void): Pool;
/**
* Set handler to be run when a connection is released back to the pool.
* This is called after all release activity has been performed on the
* connection, so the connection will be listed as free at the time of the
* event.
*/
// tslint:disable-next-line:unified-signatures
on(ev: 'release', callback: (connection: PoolConnection) => void): Pool;
/**
* Set handler to be run when a a fatal error occurs.
*/
on(ev: 'error', callback: (err: MysqlError) => void): Pool;
/**
* Set handler to be run when a callback has been queued to wait for an
* available connection.
*/
on(ev: 'enqueue', callback: (err?: MysqlError) => void): Pool;
/**
* Set handler to be run on a certain event.
*/
on(ev: string, callback: (...args: any[]) => void): Pool;
}
export interface PoolCluster {
config: PoolClusterConfig;
add(config: PoolConfig): void;
add(id: string, config: PoolConfig): void;
/**
* Close the connection. Any queued data (eg queries) will be sent first. If
* there are any fatal errors, the connection will be immediately closed.
* @param callback Handler for any fatal error
*/
end(callback?: (err: MysqlError) => void): void;
of(pattern: string, selector?: string): Pool;
of(pattern: undefined | null | false, selector: string): Pool;
/**
* remove all pools which match pattern
*/
remove(pattern: string): void;
getConnection(callback: (err: MysqlError, connection: PoolConnection) => void): void;
getConnection(pattern: string, callback: (err: MysqlError, connection: PoolConnection) => void): void;
getConnection(
pattern: string,
selector: string,
callback: (err: MysqlError, connection: PoolConnection) => void,
): void;
/**
* Set handler to be run on a certain event.
*/
on(ev: string, callback: (...args: any[]) => void): PoolCluster;
/**
* Set handler to be run when a node is removed or goes offline.
*/
on(ev: 'remove' | 'offline', callback: (nodeId: string) => void): PoolCluster;
}
// related to Query
export type packetCallback = (packet: any) => void;
export interface Query {
/**
* Template query
*/
sql: string;
/**
* Values for template query
*/
values?: string[];
/**
* Default true
*/
typeCast?: TypeCast;
/**
* Default false
*/
nestedTables: boolean;
/**
* Emits a query packet to start the query
*/
start(): void;
/**
* Determines the packet class to use given the first byte of the packet.
*
* @param byte The first byte of the packet
* @param parser The packet parser
*/
determinePacket(byte: number, parser: any): any;
OkPacket: packetCallback;
ErrorPacket: packetCallback;
ResultSetHeaderPacket: packetCallback;
FieldPacket: packetCallback;
EofPacket: packetCallback;
RowDataPacket(packet: any, parser: any, connection: Connection): void;
/**
* Creates a Readable stream with the given options
*
* @param options The options for the stream. (see readable-stream package)
*/
stream(options?: stream.ReadableOptions): stream.Readable;
on(ev: string, callback: (...args: any[]) => void): Query;
on(ev: 'result', callback: (row: any, index: number) => void): Query;
on(ev: 'error', callback: (err: MysqlError) => void): Query;
on(ev: 'fields', callback: (fields: FieldInfo[], index: number) => void): Query;
on(ev: 'packet', callback: (packet: any) => void): Query;
on(ev: 'end', callback: () => void): Query;
}
export interface GeometryType extends Array<{ x: number; y: number } | GeometryType> {
x: number;
y: number;
}
export type TypeCast =
| boolean
| ((
field: UntypedFieldInfo & {
type: string;
length: number;
string(): string;
buffer(): Buffer;
geometry(): null | GeometryType;
},
next: () => void,
) => any);
export type queryCallback = (err: MysqlError | null, results?: any, fields?: FieldInfo[]) => void;
// values can be non [], see custom format (https://github.com/mysqljs/mysql#custom-format)
export interface QueryFunction {
(query: Query): Query;
(options: string | QueryOptions, callback?: queryCallback): Query;
(options: string | QueryOptions, values: any, callback?: queryCallback): Query;
}
export interface QueryOptions {
/**
* The SQL for the query
*/
sql: string;
/**
* Values for template query
*/
values?: any;
/**
* 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?: TypeCast;
}
export interface ConnectionOptions {
/**
* 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;
/**
* Number of milliseconds
*/
timeout?: number;
}
export interface ConnectionConfig extends ConnectionOptions {
/**
* 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?: TypeCast;
/**
* A custom query format function
*/
queryFormat?(query: string, values: any): string;
/**
* 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. Can be true/false or an array of type names to keep as strings. (Default: false)
*/
dateStrings?: boolean | Array<'TIMESTAMP' | 'DATETIME' | 'DATE'>;
/**
* 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?: boolean | string[] | Types[];
/**
* Generates stack traces on errors 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?: string | string[];
/**
* object with ssl parameters or a string containing name of ssl profile
*/
ssl?: string | (tls.SecureContextOptions & { rejectUnauthorized?: boolean });
}
export interface PoolSpecificConfig {
/**
* 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;
}
export interface PoolConfig extends PoolSpecificConfig, ConnectionConfig {
}
export interface PoolActualConfig extends PoolSpecificConfig {
connectionConfig: ConnectionConfig;
}
export interface PoolClusterConfig {
/**
* 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;
/**
* If connection fails, specifies the number of milliseconds before another connection attempt will be made.
* If set to 0, then node will be removed instead and never re-used. (Default: 0)
*/
restoreNodeTimeout?: 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;
}
export interface MysqlError 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;
/**
* SQL of failed query
*/
sql?: string;
/**
* Error message from MySQL
*/
sqlMessage?: string;
}
// Result from an insert, update, or delete statement.
export interface OkPacket {
fieldCount: number;
/**
* The number of affected rows from an insert, update, or delete statement.
*/
affectedRows: number;
/**
* The insert id after inserting a row into a table with an auto increment primary key.
*/
insertId: number;
serverStatus?: number;
warningCount?: number;
/**
* The server result message from an insert, update, or delete statement.
*/
message: string;
/**
* The number of changed rows from an update statement. "changedRows" differs from "affectedRows" in that it does not count updated rows whose values were not changed.
*/
changedRows: number;
protocol41: boolean;
}
export const enum Types {
DECIMAL = 0x00, // aka DECIMAL (http://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html)
TINY = 0x01, // aka TINYINT, 1 byte
SHORT = 0x02, // aka SMALLINT, 2 bytes
LONG = 0x03, // aka INT, 4 bytes
FLOAT = 0x04, // aka FLOAT, 4-8 bytes
DOUBLE = 0x05, // aka DOUBLE, 8 bytes
NULL = 0x06, // NULL (used for prepared statements, I think)
TIMESTAMP = 0x07, // aka TIMESTAMP
LONGLONG = 0x08, // aka BIGINT, 8 bytes
INT24 = 0x09, // aka MEDIUMINT, 3 bytes
DATE = 0x0a, // aka DATE
TIME = 0x0b, // aka TIME
DATETIME = 0x0c, // aka DATETIME
YEAR = 0x0d, // aka YEAR, 1 byte (don't ask)
NEWDATE = 0x0e, // aka ?
VARCHAR = 0x0f, // aka VARCHAR (?)
BIT = 0x10, // aka BIT, 1-8 byte
TIMESTAMP2 = 0x11, // aka TIMESTAMP with fractional seconds
DATETIME2 = 0x12, // aka DATETIME with fractional seconds
TIME2 = 0x13, // aka TIME with fractional seconds
JSON = 0xf5, // aka JSON
NEWDECIMAL = 0xf6, // aka DECIMAL
ENUM = 0xf7, // aka ENUM
SET = 0xf8, // aka SET
TINY_BLOB = 0xf9, // aka TINYBLOB, TINYTEXT
MEDIUM_BLOB = 0xfa, // aka MEDIUMBLOB, MEDIUMTEXT
LONG_BLOB = 0xfb, // aka LONGBLOG, LONGTEXT
BLOB = 0xfc, // aka BLOB, TEXT
VAR_STRING = 0xfd, // aka VARCHAR, VARBINARY
STRING = 0xfe, // aka CHAR, BINARY
GEOMETRY = 0xff, // aka GEOMETRY
}
export interface UntypedFieldInfo {
catalog: string;
db: string;
schema: string;
table: string;
orgTable: string;
name: string;
orgName: string;
charsetNr: number;
length: number;
flags: number;
decimals: number;
default?: string;
zeroFill: boolean;
protocol41: boolean;
}
export interface FieldInfo extends UntypedFieldInfo {
type: Types;
}
export { TypecastField, IConnectionConfig } from 'mysql2/promise';
}
declare module 'mysql2/promise' {
import * as mysql from 'mysql2';
export * from 'mysql2';
export interface IQueryReturn<T> {
0: T[];
1: mysql.FieldInfo[];
[Symbol.iterator](): T[] | mysql.FieldInfo[];
[index: number]: T[] | mysql.FieldInfo[];
}
export interface IExecuteOptions extends mysql.QueryOptions {
values: any[];
}
export type IPromiseQueryFunction = <T>(
arg1: string | mysql.QueryOptions,
values?: any | any[],
) => Promise<IQueryReturn<T>>;
export type IPromiseExecuteFunction = <T>(
arg1: string | IExecuteOptions,
values?: any | any[],
) => Promise<IQueryReturn<T>>;
export interface IPromiseConnection {
connection: mysql.Connection;
query: IPromiseQueryFunction;
execute: IPromiseExecuteFunction;
release(): void;
end(): Promise<void>;
end(options: any): Promise<void>;
}
export interface IPromisePool extends IPromiseConnection {
connection: mysql.Connection;
getConnection(): Promise<IPromiseConnection>;
query: IPromiseQueryFunction;
execute: IPromiseExecuteFunction;
}
type FieldTypes =
| 'DECIMAL'
| 'TINY'
| 'SHORT'
| 'LONG'
| 'FLOAT'
| 'DOUBLE'
| 'NULL'
| 'TIMESTAMP'
| 'LONGLONG'
| 'INT24'
| 'DATE'
| 'TIME'
| 'DATETIME'
| 'YEAR'
| 'NEWDATE'
| 'VARCHAR'
| 'BIT'
| 'JSON'
| 'NEWDECIMAL'
| 'ENUM'
| 'SET'
| 'TINY_BLOB'
| 'MEDIUM_BLOB'
| 'LONG_BLOB'
| 'BLOB'
| 'VAR_STRING'
| 'STRING'
| 'GEOMETRY';
export interface TypecastField {
buffer(): Buffer;
string(): string;
geometry(): any;
db: string;
length: number;
name: string;
table: string;
type: FieldTypes;
}
export interface IConnectionConfig extends mysql.ConnectionOptions {
/**
* 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?: (field: TypecastField, next: () => void) => 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?: string[];
/**
* object with ssl parameters or a string containing name of ssl profile
*/
ssl?: any;
}
export function createConnection(
options: IConnectionConfig,