Connection.d.ts 9.0 KB

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