Connection.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 { EventEmitter } from 'events';
  6. import { Readable } from 'stream';
  7. import { Query, QueryError } from './protocol/sequences/Query.js';
  8. import { Prepare, PrepareStatementInfo } from './protocol/sequences/Prepare.js';
  9. import {
  10. OkPacket,
  11. FieldPacket,
  12. RowDataPacket,
  13. ResultSetHeader,
  14. OkPacketParams,
  15. ErrorPacketParams,
  16. } from './protocol/packets/index.js';
  17. import { Connection as PromiseConnection } from '../../../promise.js';
  18. import { AuthPlugin } from './Auth.js';
  19. import { QueryableBase } from './protocol/sequences/QueryableBase.js';
  20. import { ExecutableBase } from './protocol/sequences/ExecutableBase.js';
  21. import { TypeCast } from './parsers/typeCast.js';
  22. export interface SslOptions {
  23. /**
  24. * A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates
  25. */
  26. pfx?: string;
  27. /**
  28. * Either a string/buffer or list of strings/Buffers holding the PEM encoded private key(s) to use
  29. */
  30. key?: string | string[] | Buffer | Buffer[];
  31. /**
  32. * A string of passphrase for the private key or pfx
  33. */
  34. passphrase?: string;
  35. /**
  36. * A string/buffer or list of strings/Buffers holding the PEM encoded certificate(s)
  37. */
  38. cert?: string | string[] | Buffer | Buffer[];
  39. /**
  40. * Either a string/Buffer or list of strings/Buffers of PEM encoded CA certificates to trust.
  41. */
  42. ca?: string | string[] | Buffer | Buffer[];
  43. /**
  44. * Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List)
  45. */
  46. crl?: string | string[];
  47. /**
  48. * A string describing the ciphers to use or exclude
  49. */
  50. ciphers?: string;
  51. /**
  52. * You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this.
  53. */
  54. rejectUnauthorized?: boolean;
  55. /**
  56. * Configure the minimum supported version of SSL, the default is TLSv1.2.
  57. */
  58. minVersion?: string;
  59. /**
  60. * Configure the maximum supported version of SSL, the default is TLSv1.3.
  61. */
  62. maxVersion?: string;
  63. /**
  64. * You can verify the server name identity presented on the server certificate when connecting to a MySQL server.
  65. * You should enable this but it is disabled by default right now for backwards compatibility.
  66. */
  67. verifyIdentity?: boolean;
  68. }
  69. export interface ConnectionOptions {
  70. /**
  71. * DECIMAL and NEWDECIMAL types will be returned as numbers if this option is set to `true` ( default: `false`).
  72. */
  73. decimalNumbers?: boolean;
  74. /**
  75. * The MySQL user to authenticate as
  76. */
  77. user?: string;
  78. /**
  79. * The password of that MySQL user
  80. */
  81. password?: string;
  82. /**
  83. * Alias for the MySQL user password. Makes a bit more sense in a multifactor authentication setup (see
  84. * "password2" and "password3")
  85. */
  86. password1?: string;
  87. /**
  88. * 2nd factor authentication password. Mandatory when the authentication policy for the MySQL user account
  89. * requires an additional authentication method that needs a password.
  90. * https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html
  91. */
  92. password2?: string;
  93. /**
  94. * 3rd factor authentication password. Mandatory when the authentication policy for the MySQL user account
  95. * requires two additional authentication methods and the last one needs a password.
  96. * https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html
  97. */
  98. password3?: string;
  99. /**
  100. * Name of the database to use for this connection
  101. */
  102. database?: string;
  103. /**
  104. * The charset for the connection. This is called 'collation' in the SQL-level of MySQL (like utf8_general_ci).
  105. * If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used.
  106. * (Default: 'UTF8_GENERAL_CI')
  107. */
  108. charset?: string;
  109. /**
  110. * The hostname of the database you are connecting to. (Default: localhost)
  111. */
  112. host?: string;
  113. /**
  114. * The port number to connect to. (Default: 3306)
  115. */
  116. port?: number;
  117. /**
  118. * The source IP address to use for TCP connection
  119. */
  120. localAddress?: string;
  121. /**
  122. * The path to a unix domain socket to connect to. When used host and port are ignored
  123. */
  124. socketPath?: string;
  125. /**
  126. * The timezone used to store local dates. (Default: 'local')
  127. */
  128. timezone?: string | 'local';
  129. /**
  130. * The milliseconds before a timeout occurs during the initial connection to the MySQL server. (Default: 10 seconds)
  131. */
  132. connectTimeout?: number;
  133. /**
  134. * Stringify objects instead of converting to values. (Default: 'false')
  135. */
  136. stringifyObjects?: boolean;
  137. /**
  138. * Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false)
  139. */
  140. insecureAuth?: boolean;
  141. /**
  142. * By specifying a function that returns a readable stream, an arbitrary stream can be sent when sending a local fs file.
  143. */
  144. infileStreamFactory?: (path: string) => Readable;
  145. /**
  146. * Determines if column values should be converted to native JavaScript types.
  147. *
  148. * @default true
  149. *
  150. * 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.
  151. *
  152. * ---
  153. *
  154. * You can also specify a function to do the type casting yourself:
  155. * ```ts
  156. * (field: Field, next: () => unknown) => {
  157. * return next();
  158. * }
  159. * ```
  160. *
  161. * ---
  162. *
  163. * **WARNING:**
  164. *
  165. * YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once:
  166. *
  167. * ```js
  168. * field.string();
  169. * field.buffer();
  170. * field.geometry();
  171. * ```
  172. * Which are aliases for:
  173. *
  174. * ```js
  175. * parser.parseLengthCodedString();
  176. * parser.parseLengthCodedBuffer();
  177. * parser.parseGeometryValue();
  178. * ```
  179. *
  180. * You can find which field function you need to use by looking at `RowDataPacket.prototype._typeCast`.
  181. */
  182. typeCast?: TypeCast;
  183. /**
  184. * A custom query format function
  185. */
  186. queryFormat?: (query: string, values: any) => void;
  187. /**
  188. * When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option
  189. * (Default: false)
  190. */
  191. supportBigNumbers?: boolean;
  192. /**
  193. * Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be
  194. * always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving
  195. * bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately
  196. * represented with [JavaScript Number objects](https://262.ecma-international.org/5.1/#sec-8.5)
  197. * (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects.
  198. * This option is ignored if supportBigNumbers is disabled.
  199. */
  200. bigNumberStrings?: boolean;
  201. /**
  202. * Force date types (TIMESTAMP, DATETIME, DATE) to be returned as strings rather then inflated into JavaScript Date
  203. * objects. Can be true/false or an array of type names to keep as strings.
  204. *
  205. * (Default: false)
  206. */
  207. dateStrings?: boolean | Array<'TIMESTAMP' | 'DATETIME' | 'DATE'>;
  208. /**
  209. * This will print all incoming and outgoing packets on stdout.
  210. * You can also restrict debugging to packet types by passing an array of types (strings) to debug;
  211. *
  212. * (Default: false)
  213. */
  214. debug?: any;
  215. /**
  216. * Generates stack traces on Error to include call site of library entrance ('long stack traces'). Slight
  217. * performance penalty for most calls. (Default: true)
  218. */
  219. trace?: boolean;
  220. /**
  221. * Allow multiple mysql statements per query. Be careful with this, it exposes you to SQL injection attacks. (Default: false)
  222. */
  223. multipleStatements?: boolean;
  224. /**
  225. * List of connection flags to use other than the default ones. It is also possible to blacklist default ones
  226. */
  227. flags?: Array<string>;
  228. /**
  229. * object with ssl parameters or a string containing name of ssl profile
  230. */
  231. ssl?: string | SslOptions;
  232. /**
  233. * Return each row as an array, not as an object.
  234. * This is useful when you have duplicate column names.
  235. * This can also be set in the `QueryOption` object to be applied per-query.
  236. */
  237. rowsAsArray?: boolean;
  238. /**
  239. * Enable keep-alive on the socket. (Default: true)
  240. */
  241. enableKeepAlive?: boolean;
  242. /**
  243. * If keep-alive is enabled users can supply an initial delay. (Default: 0)
  244. */
  245. keepAliveInitialDelay?: number;
  246. charsetNumber?: number;
  247. compress?: boolean;
  248. authSwitchHandler?: (data: any, callback: () => void) => any;
  249. connectAttributes?: { [param: string]: any };
  250. isServer?: boolean;
  251. maxPreparedStatements?: number;
  252. namedPlaceholders?: boolean;
  253. nestTables?: boolean | string;
  254. passwordSha1?: string;
  255. pool?: any;
  256. stream?: any;
  257. uri?: string;
  258. connectionLimit?: number;
  259. maxIdle?: number;
  260. idleTimeout?: number;
  261. Promise?: any;
  262. queueLimit?: number;
  263. waitForConnections?: boolean;
  264. authPlugins?: {
  265. [key: string]: AuthPlugin;
  266. };
  267. /**
  268. * Force JSON to be returned as string
  269. *
  270. * (Default: false)
  271. */
  272. jsonStrings?: boolean;
  273. }
  274. declare class Connection extends QueryableBase(ExecutableBase(EventEmitter)) {
  275. config: ConnectionOptions;
  276. threadId: number;
  277. authorized: boolean;
  278. static createQuery<
  279. T extends
  280. | RowDataPacket[][]
  281. | RowDataPacket[]
  282. | OkPacket
  283. | OkPacket[]
  284. | ResultSetHeader,
  285. >(
  286. sql: string,
  287. callback?: (
  288. err: QueryError | null,
  289. result: T,
  290. fields: FieldPacket[],
  291. ) => any,
  292. ): Query;
  293. static createQuery<
  294. T extends
  295. | RowDataPacket[][]
  296. | RowDataPacket[]
  297. | OkPacket
  298. | OkPacket[]
  299. | ResultSetHeader,
  300. >(
  301. sql: string,
  302. values: any | any[] | { [param: string]: any },
  303. callback?: (
  304. err: QueryError | null,
  305. result: T,
  306. fields: FieldPacket[],
  307. ) => any,
  308. ): Query;
  309. beginTransaction(callback: (err: QueryError | null) => void): void;
  310. connect(callback?: (err: QueryError | null) => void): void;
  311. commit(callback?: (err: QueryError | null) => void): void;
  312. changeUser(
  313. options: ConnectionOptions,
  314. callback?: (err: QueryError | null) => void,
  315. ): void;
  316. end(callback?: (err: QueryError | null) => void): void;
  317. end(options: any, callback?: (err: QueryError | null) => void): void;
  318. destroy(): void;
  319. pause(): void;
  320. resume(): void;
  321. escape(value: any): string;
  322. escapeId(value: string): string;
  323. escapeId(values: string[]): string;
  324. format(sql: string, values?: any | any[] | { [param: string]: any }): string;
  325. on(event: string, listener: (...args: any[]) => void): this;
  326. rollback(callback: (err: QueryError | null) => void): void;
  327. prepare(
  328. sql: string,
  329. callback?: (err: QueryError | null, statement: PrepareStatementInfo) => any,
  330. ): Prepare;
  331. unprepare(sql: string): PrepareStatementInfo;
  332. serverHandshake(args: any): any;
  333. promise(promiseImpl?: PromiseConstructor): PromiseConnection;
  334. ping(callback?: (err: QueryError | null) => any): void;
  335. writeOk(args?: OkPacketParams): void;
  336. writeError(args?: ErrorPacketParams): void;
  337. writeEof(warnings?: number, statusFlags?: number): void;
  338. writeTextResult(rows?: Array<any>, columns?: Array<any>): void;
  339. writePacket(packet: any): void;
  340. sequenceId: number;
  341. }
  342. export { Connection };