Connection.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // This file was modified by Oracle on November 04, 2021.
  2. // Type definitions and corresponding descriptions were introduced for the
  3. // connection options relevant for multifactor authentication.
  4. // Modifications copyright (c) 2021, Oracle and/or its affiliates.
  5. import Query = require('./protocol/sequences/Query');
  6. import Prepare = require('./protocol/sequences/Prepare');
  7. import {OkPacket, FieldPacket, RowDataPacket, ResultSetHeader} from './protocol/packets/index';
  8. import {EventEmitter} from 'events';
  9. declare namespace Connection {
  10. export interface ConnectionOptions {
  11. /**
  12. * DECIMAL and NEWDECIMAL types will be returned as numbers if this option is set to `true` ( default: `false`).
  13. */
  14. decimalNumbers?: boolean;
  15. /**
  16. * The MySQL user to authenticate as
  17. */
  18. user?: string;
  19. /**
  20. * The password of that MySQL user
  21. */
  22. password?: string;
  23. /**
  24. * Alias for the MySQL user password. Makes a bit more sense in a multifactor authentication setup (see
  25. * "password2" and "password3")
  26. */
  27. password1?: string;
  28. /**
  29. * 2nd factor authentication password. Mandatory when the authentication policy for the MySQL user account
  30. * requires an additional authentication method that needs a password.
  31. * https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html
  32. */
  33. password2?: string;
  34. /**
  35. * 3rd factor authentication password. Mandatory when the authentication policy for the MySQL user account
  36. * requires two additional authentication methods and the last one needs a password.
  37. * https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html
  38. */
  39. password3?: string;
  40. /**
  41. * Name of the database to use for this connection
  42. */
  43. database?: string;
  44. /**
  45. * The charset for the connection. This is called 'collation' in the SQL-level of MySQL (like utf8_general_ci).
  46. * If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used.
  47. * (Default: 'UTF8_GENERAL_CI')
  48. */
  49. charset?: string;
  50. /**
  51. * The hostname of the database you are connecting to. (Default: localhost)
  52. */
  53. host?: string;
  54. /**
  55. * The port number to connect to. (Default: 3306)
  56. */
  57. port?: number;
  58. /**
  59. * The source IP address to use for TCP connection
  60. */
  61. localAddress?: string;
  62. /**
  63. * The path to a unix domain socket to connect to. When used host and port are ignored
  64. */
  65. socketPath?: string;
  66. /**
  67. * The timezone used to store local dates. (Default: 'local')
  68. */
  69. timezone?: string | 'local';
  70. /**
  71. * The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds)
  72. */
  73. connectTimeout?: number;
  74. /**
  75. * Stringify objects instead of converting to values. (Default: 'false')
  76. */
  77. stringifyObjects?: boolean;
  78. /**
  79. * Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false)
  80. */
  81. insecureAuth?: boolean;
  82. /**
  83. * Determines if column values should be converted to native JavaScript types. It is not recommended (and may go away / change in the future)
  84. * to disable type casting, but you can currently do so on either the connection or query level. (Default: true)
  85. *
  86. * You can also specify a function (field: any, next: () => void) => {} to do the type casting yourself.
  87. *
  88. * WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once.
  89. *
  90. * field.string()
  91. * field.buffer()
  92. * field.geometry()
  93. *
  94. * are aliases for
  95. *
  96. * parser.parseLengthCodedString()
  97. * parser.parseLengthCodedBuffer()
  98. * parser.parseGeometryValue()
  99. *
  100. * You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
  101. */
  102. typeCast?: boolean | ((field: any, next: () => void) => any);
  103. /**
  104. * A custom query format function
  105. */
  106. queryFormat?: (query: string, values: any) => void;
  107. /**
  108. * When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option
  109. * (Default: false)
  110. */
  111. supportBigNumbers?: boolean;
  112. /**
  113. * Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be
  114. * always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving
  115. * bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately
  116. * represented with [JavaScript Number objects](https://262.ecma-international.org/5.1/#sec-8.5)
  117. * (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects.
  118. * This option is ignored if supportBigNumbers is disabled.
  119. */
  120. bigNumberStrings?: boolean;
  121. /**
  122. * Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date
  123. * objects. Can be true/false or an array of type names to keep as strings.
  124. *
  125. * (Default: false)
  126. */
  127. dateStrings?: boolean | Array<'TIMESTAMP' | 'DATETIME' | 'DATE'>;
  128. /**
  129. * This will print all incoming and outgoing packets on stdout.
  130. * You can also restrict debugging to packet types by passing an array of types (strings) to debug;
  131. *
  132. * (Default: false)
  133. */
  134. debug?: any;
  135. /**
  136. * Generates stack traces on Error to include call site of library entrance ('long stack traces'). Slight
  137. * performance penalty for most calls. (Default: true)
  138. */
  139. trace?: boolean;
  140. /**
  141. * Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
  142. */
  143. multipleStatements?: boolean;
  144. /**
  145. * List of connection flags to use other than the default ones. It is also possible to blacklist default ones
  146. */
  147. flags?: Array<string>;
  148. /**
  149. * object with ssl parameters or a string containing name of ssl profile
  150. */
  151. ssl?: string | SslOptions;
  152. /**
  153. * Return each row as an array, not as an object.
  154. * This is useful when you have duplicate column names.
  155. * This can also be set in the `QueryOption` object to be applied per-query.
  156. */
  157. rowsAsArray?: boolean
  158. }
  159. export interface SslOptions {
  160. /**
  161. * A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates
  162. */
  163. pfx?: string;
  164. /**
  165. * Either a string/buffer or list of strings/Buffers holding the PEM encoded private key(s) to use
  166. */
  167. key?: string | string[] | Buffer | Buffer[];
  168. /**
  169. * A string of passphrase for the private key or pfx
  170. */
  171. passphrase?: string;
  172. /**
  173. * A string/buffer or list of strings/Buffers holding the PEM encoded certificate(s)
  174. */
  175. cert?: string | string[] | Buffer | Buffer[];
  176. /**
  177. * Either a string/Buffer or list of strings/Buffers of PEM encoded CA certificates to trust.
  178. */
  179. ca?: string | string[] | Buffer | Buffer[];
  180. /**
  181. * Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List)
  182. */
  183. crl?: string | string[];
  184. /**
  185. * A string describing the ciphers to use or exclude
  186. */
  187. ciphers?: string;
  188. /**
  189. * You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this.
  190. */
  191. rejectUnauthorized?: boolean;
  192. /**
  193. * Configure the minimum supported version of SSL, the default is TLSv1.2.
  194. */
  195. minVersion?: string;
  196. /**
  197. * Configure the maximum supported version of SSL, the default is TLSv1.3.
  198. */
  199. maxVersion?: string;
  200. /**
  201. * You can verify the server name identity presented on the server certificate when connecting to a MySQL server.
  202. * You should enable this but it is disabled by default right now for backwards compatibility.
  203. */
  204. verifyIdentity?: boolean;
  205. }
  206. }
  207. declare class Connection extends EventEmitter {
  208. config: Connection.ConnectionOptions;
  209. threadId: number;
  210. authorized: boolean;
  211. static createQuery<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
  212. static createQuery<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
  213. beginTransaction(callback: (err: Query.QueryError | null) => void): void;
  214. connect(callback?: (err: Query.QueryError | null) => void): void;
  215. commit(callback?: (err: Query.QueryError | null) => void): void;
  216. changeUser(options: Connection.ConnectionOptions, callback?: (err: Query.QueryError | null) => void): void;
  217. query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
  218. query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
  219. query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(options: Query.QueryOptions, callback?: (err: Query.QueryError | null, result: T, fields?: FieldPacket[]) => any): Query;
  220. query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(options: Query.QueryOptions, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
  221. end(callback?: (err: Query.QueryError | null) => void): void;
  222. end(options: any, callback?: (err: Query.QueryError | null) => void): void;
  223. destroy(): void;
  224. pause(): void;
  225. resume(): void;
  226. escape(value: any): string;
  227. escapeId(value: string): string;
  228. escapeId(values: string[]): string;
  229. format(sql: string, values?: any | any[] | { [param: string]: any }): string;
  230. on(event: string, listener: Function): this;
  231. rollback(callback: (err: Query.QueryError | null) => void): void;
  232. execute<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
  233. execute<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
  234. execute<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(options: Query.QueryOptions, callback?: (err: Query.QueryError | null, result: T, fields?: FieldPacket[]) => any): Query;
  235. execute<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(options: Query.QueryOptions, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
  236. unprepare(sql: string): any;
  237. serverHandshake(args: any): any;
  238. }
  239. export = Connection;