UNPKG

22.2 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import stream = require("stream");
4import tls = require("tls");
5import events = require("events");
6
7export interface EscapeFunctions {
8 /**
9 * Escape an untrusted string to be used as a SQL value. Use this on user
10 * provided data.
11 * @param value Value to escape
12 * @param stringifyObjects If true, don't convert objects into SQL lists
13 * @param timeZone Convert dates from UTC to the given timezone.
14 */
15 escape(value: any, stringifyObjects?: boolean, timeZone?: string): string;
16
17 /**
18 * Escape an untrusted string to be used as a SQL identifier (database,
19 * table, or column name). Use this on user provided data.
20 * @param value Value to escape.
21 * @param forbidQualified Don't allow qualified identifiers (eg escape '.')
22 */
23 escapeId(value: string, forbidQualified?: boolean): string;
24
25 /**
26 * Safely format a SQL query containing multiple untrusted values.
27 * @param sql Query, with insertion points specified with ? (for values) or
28 * ?? (for identifiers)
29 * @param values Array of objects to insert.
30 * @param stringifyObjects If true, don't convert objects into SQL lists
31 * @param timeZone Convert dates from UTC to the given timezone.
32 */
33 format(sql: string, values?: any[], stringifyObjects?: boolean, timeZone?: string): string;
34}
35
36/**
37 * Escape an untrusted string to be used as a SQL value. Use this on user
38 * provided data.
39 * @param value Value to escape
40 * @param stringifyObjects If true, don't convert objects into SQL lists
41 * @param timeZone Convert dates from UTC to the given timezone.
42 */
43export function escape(value: any, stringifyObjects?: boolean, timeZone?: string): string;
44
45/**
46 * Escape an untrusted string to be used as a SQL identifier (database,
47 * table, or column name). Use this on user provided data.
48 * @param value Value to escape.
49 * @param forbidQualified Don't allow qualified identifiers (eg escape '.')
50 */
51export function escapeId(value: string, forbidQualified?: boolean): string;
52
53/**
54 * Safely format a SQL query containing multiple untrusted values.
55 * @param sql Query, with insertion points specified with ? (for values) or
56 * ?? (for identifiers)
57 * @param values Array of objects to insert.
58 * @param stringifyObjects If true, don't convert objects into SQL lists
59 * @param timeZone Convert dates from UTC to the given timezone.
60 */
61export function format(sql: string, values?: any[], stringifyObjects?: boolean, timeZone?: string): string;
62
63export function createConnection(connectionUri: string | ConnectionConfig): Connection;
64
65export function createPool(config: PoolConfig | string): Pool;
66
67export function createPoolCluster(config?: PoolClusterConfig): PoolCluster;
68
69/**
70 * Create a string that will be inserted unescaped with format(), escape().
71 * Note: the value will still be escaped if used as an identifier (??) by
72 * format().
73 * @param sql
74 */
75export function raw(sql: string): {
76 toSqlString: () => string;
77};
78
79export interface Connection extends EscapeFunctions, events.EventEmitter {
80 config: ConnectionConfig;
81
82 state: "connected" | "authenticated" | "disconnected" | "protocol_error" | (string & {});
83
84 threadId: number | null;
85
86 createQuery: QueryFunction;
87
88 connect(callback?: (err: MysqlError, ...args: any[]) => void): void;
89
90 connect(options: any, callback?: (err: MysqlError, ...args: any[]) => void): void;
91
92 changeUser(options: ConnectionOptions, callback?: (err: MysqlError) => void): void;
93 changeUser(callback: (err: MysqlError) => void): void;
94
95 beginTransaction(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
96
97 beginTransaction(callback: (err: MysqlError) => void): void;
98
99 commit(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
100 commit(callback: (err: MysqlError) => void): void;
101
102 rollback(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
103 rollback(callback: (err: MysqlError) => void): void;
104
105 query: QueryFunction;
106
107 ping(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
108 ping(callback: (err: MysqlError) => void): void;
109
110 statistics(options?: QueryOptions, callback?: (err: MysqlError) => void): void;
111 statistics(callback: (err: MysqlError) => void): void;
112
113 /**
114 * Close the connection. Any queued data (eg queries) will be sent first. If
115 * there are any fatal errors, the connection will be immediately closed.
116 * @param callback Handler for any fatal error
117 */
118 end(callback?: (err?: MysqlError) => void): void;
119 end(options: any, callback: (err?: MysqlError) => void): void;
120
121 /**
122 * Close the connection immediately, without waiting for any queued data (eg
123 * queries) to be sent. No further events or callbacks will be triggered.
124 */
125 destroy(): void;
126
127 /**
128 * Pause the connection. No more 'result' events will fire until resume() is
129 * called.
130 */
131 pause(): void;
132
133 /**
134 * Resume the connection.
135 */
136 resume(): void;
137}
138
139export interface PoolConnection extends Connection {
140 release(): void;
141
142 /**
143 * Close the connection. Any queued data (eg queries) will be sent first. If
144 * there are any fatal errors, the connection will be immediately closed.
145 * @param callback Handler for any fatal error
146 */
147 end(): void;
148
149 /**
150 * Close the connection immediately, without waiting for any queued data (eg
151 * queries) to be sent. No further events or callbacks will be triggered.
152 */
153 destroy(): void;
154}
155
156export interface Pool extends EscapeFunctions, events.EventEmitter {
157 config: PoolActualConfig;
158
159 getConnection(callback: (err: MysqlError, connection: PoolConnection) => void): void;
160
161 acquireConnection(
162 connection: PoolConnection,
163 callback: (err: MysqlError, connection: PoolConnection) => void,
164 ): void;
165
166 /**
167 * Close the connection. Any queued data (eg queries) will be sent first. If
168 * there are any fatal errors, the connection will be immediately closed.
169 * @param callback Handler for any fatal error
170 */
171 end(callback?: (err: MysqlError) => void): void;
172
173 query: QueryFunction;
174}
175
176export interface PoolCluster extends events.EventEmitter {
177 config: PoolClusterConfig;
178
179 add(config: PoolConfig): void;
180
181 add(id: string, config: PoolConfig): void;
182
183 /**
184 * Close the connection. Any queued data (eg queries) will be sent first. If
185 * there are any fatal errors, the connection will be immediately closed.
186 * @param callback Handler for any fatal error
187 */
188 end(callback?: (err: MysqlError) => void): void;
189
190 of(pattern: string, selector?: string): Pool;
191 of(pattern: undefined | null | false, selector: string): Pool;
192
193 /**
194 * remove all pools which match pattern
195 */
196 remove(pattern: string): void;
197
198 getConnection(callback: (err: MysqlError, connection: PoolConnection) => void): void;
199
200 getConnection(pattern: string, callback: (err: MysqlError, connection: PoolConnection) => void): void;
201
202 getConnection(
203 pattern: string,
204 selector: string,
205 callback: (err: MysqlError, connection: PoolConnection) => void,
206 ): void;
207}
208
209// related to Query
210export type packetCallback = (packet: any) => void;
211
212export interface Query {
213 /**
214 * Template query
215 */
216 sql: string;
217
218 /**
219 * Values for template query
220 */
221 values?: string[] | undefined;
222
223 /**
224 * Default true
225 */
226 typeCast?: TypeCast | undefined;
227
228 /**
229 * Default false
230 */
231 nestedTables: boolean;
232
233 /**
234 * Emits a query packet to start the query
235 */
236 start(): void;
237
238 /**
239 * Determines the packet class to use given the first byte of the packet.
240 *
241 * @param byte The first byte of the packet
242 * @param parser The packet parser
243 */
244 determinePacket(byte: number, parser: any): any;
245
246 OkPacket: packetCallback;
247 ErrorPacket: packetCallback;
248 ResultSetHeaderPacket: packetCallback;
249 FieldPacket: packetCallback;
250 EofPacket: packetCallback;
251
252 RowDataPacket(packet: any, parser: any, connection: Connection): void;
253
254 /**
255 * Creates a Readable stream with the given options
256 *
257 * @param options The options for the stream. (see readable-stream package)
258 */
259 stream(options?: stream.ReadableOptions): stream.Readable;
260
261 on(ev: string, callback: (...args: any[]) => void): Query;
262
263 on(ev: "result", callback: (row: any, index: number) => void): Query;
264
265 on(ev: "error", callback: (err: MysqlError) => void): Query;
266
267 on(ev: "fields", callback: (fields: FieldInfo[], index: number) => void): Query;
268
269 on(ev: "packet", callback: (packet: any) => void): Query;
270
271 on(ev: "end", callback: () => void): Query;
272}
273
274export interface GeometryType extends Array<{ x: number; y: number } | GeometryType> {
275 x: number;
276 y: number;
277}
278
279export type TypeCast =
280 | boolean
281 | ((
282 field: UntypedFieldInfo & {
283 type: string;
284 length: number;
285 string(): null | string;
286 buffer(): null | Buffer;
287 geometry(): null | GeometryType;
288 },
289 next: () => void,
290 ) => any);
291
292export type queryCallback = (err: MysqlError | null, results?: any, fields?: FieldInfo[]) => void;
293
294// values can be non [], see custom format (https://github.com/mysqljs/mysql#custom-format)
295export interface QueryFunction {
296 (query: Query): Query;
297
298 (options: string | QueryOptions, callback?: queryCallback): Query;
299
300 (options: string | QueryOptions, values: any, callback?: queryCallback): Query;
301}
302
303export interface QueryOptions {
304 /**
305 * The SQL for the query
306 */
307 sql: string;
308
309 /**
310 * Values for template query
311 */
312 values?: any;
313
314 /**
315 * Every operation takes an optional inactivity timeout option. This allows you to specify appropriate timeouts for
316 * operations. It is important to note that these timeouts are not part of the MySQL protocol, and rather timeout
317 * operations through the client. This means that when a timeout is reached, the connection it occurred on will be
318 * destroyed and no further operations can be performed.
319 */
320 timeout?: number | undefined;
321
322 /**
323 * Either a boolean or string. If true, tables will be nested objects. If string (e.g. '_'), tables will be
324 * nested as tableName_fieldName
325 */
326 nestTables?: any;
327
328 /**
329 * Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
330 * to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
331 *
332 * You can also specify a function (field: any, next: () => void) => {} to do the type casting yourself.
333 *
334 * WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.
335 *
336 * field.string()
337 * field.buffer()
338 * field.geometry()
339 *
340 * are aliases for
341 *
342 * parser.parseLengthCodedString()
343 * parser.parseLengthCodedBuffer()
344 * parser.parseGeometryValue()
345 *
346 * You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
347 */
348 typeCast?: TypeCast | undefined;
349}
350
351export interface ConnectionOptions {
352 /**
353 * The MySQL user to authenticate as
354 */
355 user?: string | undefined;
356
357 /**
358 * The password of that MySQL user
359 */
360 password?: string | undefined;
361
362 /**
363 * Name of the database to use for this connection
364 */
365 database?: string | undefined;
366
367 /**
368 * The charset for the connection. This is called "collation" in the SQL-level of MySQL (like utf8_general_ci).
369 * If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used.
370 * (Default: 'UTF8_GENERAL_CI')
371 */
372 charset?: string | undefined;
373
374 /**
375 * Number of milliseconds
376 */
377 timeout?: number | undefined;
378}
379
380export interface ConnectionConfig extends ConnectionOptions {
381 /**
382 * The hostname of the database you are connecting to. (Default: localhost)
383 */
384 host?: string | undefined;
385
386 /**
387 * The port number to connect to. (Default: 3306)
388 */
389 port?: number | undefined;
390
391 /**
392 * The source IP address to use for TCP connection
393 */
394 localAddress?: string | undefined;
395
396 /**
397 * The path to a unix domain socket to connect to. When used host and port are ignored
398 */
399 socketPath?: string | undefined;
400
401 /**
402 * The timezone used to store local dates. (Default: 'local')
403 */
404 timezone?: string | undefined;
405
406 /**
407 * The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds)
408 */
409 connectTimeout?: number | undefined;
410
411 /**
412 * Stringify objects instead of converting to values. (Default: 'false')
413 */
414 stringifyObjects?: boolean | undefined;
415
416 /**
417 * Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false)
418 */
419 insecureAuth?: boolean | undefined;
420
421 /**
422 * Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
423 * to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
424 *
425 * You can also specify a function (field: any, next: () => void) => {} to do the type casting yourself.
426 *
427 * WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.
428 *
429 * field.string()
430 * field.buffer()
431 * field.geometry()
432 *
433 * are aliases for
434 *
435 * parser.parseLengthCodedString()
436 * parser.parseLengthCodedBuffer()
437 * parser.parseGeometryValue()
438 *
439 * You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
440 */
441 typeCast?: TypeCast | undefined;
442
443 /**
444 * A custom query format function
445 */
446 queryFormat?(query: string, values: any): string;
447
448 /**
449 * When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option
450 * (Default: false)
451 */
452 supportBigNumbers?: boolean | undefined;
453
454 /**
455 * Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be
456 * always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving
457 * bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately
458 * represented with [JavaScript Number objects] (http://ecma262-5.com/ELS5_HTML.htm#Section_8.5)
459 * (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects.
460 * This option is ignored if supportBigNumbers is disabled.
461 */
462 bigNumberStrings?: boolean | undefined;
463
464 /**
465 * Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript
466 * Date objects. Can be true/false or an array of type names to keep as strings. (Default: false)
467 */
468 dateStrings?: boolean | Array<"TIMESTAMP" | "DATETIME" | "DATE"> | undefined;
469
470 /**
471 * This will print all incoming and outgoing packets on stdout.
472 * You can also restrict debugging to packet types by passing an array of types (strings) to debug;
473 *
474 * (Default: false)
475 */
476 debug?: boolean | string[] | Types[] | undefined;
477
478 /**
479 * Generates stack traces on errors to include call site of library entrance ("long stack traces"). Slight
480 * performance penalty for most calls. (Default: true)
481 */
482 trace?: boolean | undefined;
483
484 /**
485 * Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
486 */
487 multipleStatements?: boolean | undefined;
488
489 /**
490 * List of connection flags to use other than the default ones. It is also possible to blacklist default ones
491 */
492 flags?: string | string[] | undefined;
493
494 /**
495 * object with ssl parameters or a string containing name of ssl profile
496 */
497 ssl?: string | (tls.SecureContextOptions & { rejectUnauthorized?: boolean | undefined }) | undefined;
498}
499
500export interface PoolSpecificConfig {
501 /**
502 * The milliseconds before a timeout occurs during the connection acquisition. This is slightly different from connectTimeout,
503 * because acquiring a pool connection does not always involve making a connection. (Default: 10 seconds)
504 */
505 acquireTimeout?: number | undefined;
506
507 /**
508 * Determines the pool's action when no connections are available and the limit has been reached. If true, the pool will queue
509 * the connection request and call it when one becomes available. If false, the pool will immediately call back with an error.
510 * (Default: true)
511 */
512 waitForConnections?: boolean | undefined;
513
514 /**
515 * The maximum number of connections to create at once. (Default: 10)
516 */
517 connectionLimit?: number | undefined;
518
519 /**
520 * The maximum number of connection requests the pool will queue before returning an error from getConnection. If set to 0, there
521 * is no limit to the number of queued connection requests. (Default: 0)
522 */
523 queueLimit?: number | undefined;
524}
525
526export interface PoolConfig extends PoolSpecificConfig, ConnectionConfig {
527}
528
529export interface PoolActualConfig extends PoolSpecificConfig {
530 connectionConfig: ConnectionConfig;
531}
532
533export interface PoolClusterConfig {
534 /**
535 * If true, PoolCluster will attempt to reconnect when connection fails. (Default: true)
536 */
537 canRetry?: boolean | undefined;
538
539 /**
540 * If connection fails, node's errorCount increases. When errorCount is greater than removeNodeErrorCount,
541 * remove a node in the PoolCluster. (Default: 5)
542 */
543 removeNodeErrorCount?: number | undefined;
544
545 /**
546 * If connection fails, specifies the number of milliseconds before another connection attempt will be made.
547 * If set to 0, then node will be removed instead and never re-used. (Default: 0)
548 */
549 restoreNodeTimeout?: number | undefined;
550
551 /**
552 * The default selector. (Default: RR)
553 * RR: Select one alternately. (Round-Robin)
554 * RANDOM: Select the node by random function.
555 * ORDER: Select the first node available unconditionally.
556 */
557 defaultSelector?: string | undefined;
558}
559
560export interface MysqlError extends Error {
561 /**
562 * Either a MySQL server error (e.g. 'ER_ACCESS_DENIED_ERROR'),
563 * a node.js error (e.g. 'ECONNREFUSED') or an internal error
564 * (e.g. 'PROTOCOL_CONNECTION_LOST').
565 */
566 code: string;
567
568 /**
569 * The error number for the error code
570 */
571 errno: number;
572
573 /**
574 * The sql state marker
575 */
576 sqlStateMarker?: string | undefined;
577
578 /**
579 * The sql state
580 */
581 sqlState?: string | undefined;
582
583 /**
584 * The field count
585 */
586 fieldCount?: number | undefined;
587
588 /**
589 * Boolean, indicating if this error is terminal to the connection object.
590 */
591 fatal: boolean;
592
593 /**
594 * SQL of failed query
595 */
596 sql?: string | undefined;
597
598 /**
599 * Error message from MySQL
600 */
601 sqlMessage?: string | undefined;
602}
603
604// Result from an insert, update, or delete statement.
605export interface OkPacket {
606 fieldCount: number;
607 /**
608 * The number of affected rows from an insert, update, or delete statement.
609 */
610 affectedRows: number;
611 /**
612 * The insert id after inserting a row into a table with an auto increment primary key.
613 */
614 insertId: number;
615 serverStatus?: number | undefined;
616 warningCount?: number | undefined;
617 /**
618 * The server result message from an insert, update, or delete statement.
619 */
620 message: string;
621 /**
622 * 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.
623 */
624 changedRows: number;
625 protocol41: boolean;
626}
627
628export const enum Types {
629 DECIMAL = 0x00, // aka DECIMAL (http://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html)
630 TINY = 0x01, // aka TINYINT, 1 byte
631 SHORT = 0x02, // aka SMALLINT, 2 bytes
632 LONG = 0x03, // aka INT, 4 bytes
633 FLOAT = 0x04, // aka FLOAT, 4-8 bytes
634 DOUBLE = 0x05, // aka DOUBLE, 8 bytes
635 NULL = 0x06, // NULL (used for prepared statements, I think)
636 TIMESTAMP = 0x07, // aka TIMESTAMP
637 LONGLONG = 0x08, // aka BIGINT, 8 bytes
638 INT24 = 0x09, // aka MEDIUMINT, 3 bytes
639 DATE = 0x0a, // aka DATE
640 TIME = 0x0b, // aka TIME
641 DATETIME = 0x0c, // aka DATETIME
642 YEAR = 0x0d, // aka YEAR, 1 byte (don't ask)
643 NEWDATE = 0x0e, // aka ?
644 VARCHAR = 0x0f, // aka VARCHAR (?)
645 BIT = 0x10, // aka BIT, 1-8 byte
646 TIMESTAMP2 = 0x11, // aka TIMESTAMP with fractional seconds
647 DATETIME2 = 0x12, // aka DATETIME with fractional seconds
648 TIME2 = 0x13, // aka TIME with fractional seconds
649 JSON = 0xf5, // aka JSON
650 NEWDECIMAL = 0xf6, // aka DECIMAL
651 ENUM = 0xf7, // aka ENUM
652 SET = 0xf8, // aka SET
653 TINY_BLOB = 0xf9, // aka TINYBLOB, TINYTEXT
654 MEDIUM_BLOB = 0xfa, // aka MEDIUMBLOB, MEDIUMTEXT
655 LONG_BLOB = 0xfb, // aka LONGBLOG, LONGTEXT
656 BLOB = 0xfc, // aka BLOB, TEXT
657 VAR_STRING = 0xfd, // aka VARCHAR, VARBINARY
658 STRING = 0xfe, // aka CHAR, BINARY
659 GEOMETRY = 0xff, // aka GEOMETRY
660}
661
662export interface UntypedFieldInfo {
663 catalog: string;
664 db: string;
665 table: string;
666 orgTable: string;
667 name: string;
668 orgName: string;
669 charsetNr: number;
670 length: number;
671 flags: number;
672 decimals: number;
673 default?: string | undefined;
674 zeroFill: boolean;
675 protocol41: boolean;
676}
677
678export interface FieldInfo extends UntypedFieldInfo {
679 type: Types;
680}