From 9c20c3e0a1301a8d68d7f6843205814215020d72 Mon Sep 17 00:00:00 2001 From: larshp Date: Mon, 21 Sep 2020 10:53:16 +0200 Subject: [PATCH] typings workaround --- index.d.ts | 8 +- package-lock.json | 4 - package.json | 2 +- typings/mysql/LICENSE.txt | 15 ++ typings/mysql/index.d.ts | 36 +++ typings/mysql/info.txt | 1 + typings/mysql/lib/Connection.d.ts | 237 ++++++++++++++++++ typings/mysql/lib/Pool.d.ts | 65 +++++ typings/mysql/lib/PoolCluster.d.ts | 56 +++++ typings/mysql/lib/PoolConnection.d.ts | 8 + typings/mysql/lib/protocol/packets/Field.d.ts | 16 ++ .../lib/protocol/packets/FieldPacket.d.ts | 22 ++ .../mysql/lib/protocol/packets/OkPacket.d.ts | 16 ++ .../lib/protocol/packets/ResultSetHeader.d.ts | 14 ++ .../lib/protocol/packets/RowDataPacket.d.ts | 10 + typings/mysql/lib/protocol/packets/index.d.ts | 14 ++ .../mysql/lib/protocol/sequences/Query.d.ts | 132 ++++++++++ .../lib/protocol/sequences/Sequence.d.ts | 5 + 18 files changed, 652 insertions(+), 9 deletions(-) create mode 100644 typings/mysql/LICENSE.txt create mode 100644 typings/mysql/index.d.ts create mode 100644 typings/mysql/info.txt create mode 100644 typings/mysql/lib/Connection.d.ts create mode 100644 typings/mysql/lib/Pool.d.ts create mode 100644 typings/mysql/lib/PoolCluster.d.ts create mode 100644 typings/mysql/lib/PoolConnection.d.ts create mode 100644 typings/mysql/lib/protocol/packets/Field.d.ts create mode 100644 typings/mysql/lib/protocol/packets/FieldPacket.d.ts create mode 100644 typings/mysql/lib/protocol/packets/OkPacket.d.ts create mode 100644 typings/mysql/lib/protocol/packets/ResultSetHeader.d.ts create mode 100644 typings/mysql/lib/protocol/packets/RowDataPacket.d.ts create mode 100644 typings/mysql/lib/protocol/packets/index.d.ts create mode 100644 typings/mysql/lib/protocol/sequences/Query.d.ts create mode 100644 typings/mysql/lib/protocol/sequences/Sequence.d.ts diff --git a/index.d.ts b/index.d.ts index 04bf0a7273..9d77567a7c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,8 +4,8 @@ import { PoolConnection as PromisePoolConnection } from './promise'; -import * as mysql from 'mysql'; -export * from 'mysql'; +import * as mysql from './typings/mysql'; +export * from './typings/mysql'; export interface Connection extends mysql.Connection { execute< @@ -142,11 +142,11 @@ export interface Pool extends mysql.Connection { on(event: 'enqueue', listener: () => any): this; promise(promiseImpl?: PromiseConstructor): PromisePool; } - + type authPlugins = (pluginMetadata: { connection: Connection; command: string }) => (pluginData: Buffer) => Promise; - + export interface ConnectionOptions extends mysql.ConnectionOptions { charsetNumber?: number; compress?: boolean; diff --git a/package-lock.json b/package-lock.json index 7c26a5f131..a34eae2a6f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,10 +48,6 @@ "integrity": "sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==", "dev": true }, - "@types/mysql": { - "version": "github:types/mysql#c26b1bc2bac17010081455e3127a90fb2eafcec9", - "from": "github:types/mysql" - }, "@types/normalize-package-data": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz", diff --git a/package.json b/package.json index 6534b52b51..2bfdbb9d53 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ ], "files": [ "lib", + "typings", "index.js", "index.d.ts", "promise.js", @@ -51,7 +52,6 @@ "author": "Andrey Sidorov ", "license": "MIT", "dependencies": { - "@types/mysql": "types/mysql", "denque": "^1.4.1", "generate-function": "^2.3.1", "iconv-lite": "^0.6.2", diff --git a/typings/mysql/LICENSE.txt b/typings/mysql/LICENSE.txt new file mode 100644 index 0000000000..fc354170c5 --- /dev/null +++ b/typings/mysql/LICENSE.txt @@ -0,0 +1,15 @@ +ISC License + +Copyright (c) 2016, Felix Frederick Becker + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/typings/mysql/index.d.ts b/typings/mysql/index.d.ts new file mode 100644 index 0000000000..0a2a542e8c --- /dev/null +++ b/typings/mysql/index.d.ts @@ -0,0 +1,36 @@ + +import BaseConnection = require('./lib/Connection'); +import {ConnectionOptions, SslOptions} from './lib/Connection'; +import BasePoolConnection = require('./lib/PoolConnection'); +import BasePool = require('./lib/Pool'); +import {PoolOptions} from './lib/Pool'; +import BasePoolCluster = require('./lib/PoolCluster'); +import {PoolClusterOptions} from './lib/PoolCluster'; +import BaseQuery = require('./lib/protocol/sequences/Query'); +import {QueryOptions, StreamOptions, QueryError} from './lib/protocol/sequences/Query'; + +export function createConnection(connectionUri: string): Connection; +export function createConnection(config: BaseConnection.ConnectionOptions): Connection; +export function createPool(config: BasePool.PoolOptions): Pool; +export function createPoolCluster(config?: BasePoolCluster.PoolClusterOptions): PoolCluster; +export function escape(value: any): string; +export function format(sql: string): string; +export function format(sql: string, values: any[]): string; +export function format(sql: string, values: any): string; + +export { + ConnectionOptions, + SslOptions, + PoolOptions, + PoolClusterOptions, + QueryOptions, + QueryError +}; +export * from './lib/protocol/packets/index'; + +// Expose class interfaces +export interface Connection extends BaseConnection {} +export interface PoolConnection extends BasePoolConnection {} +export interface Pool extends BasePool {} +export interface PoolCluster extends BasePoolCluster {} +export interface Query extends BaseQuery {} diff --git a/typings/mysql/info.txt b/typings/mysql/info.txt new file mode 100644 index 0000000000..8ee94197f0 --- /dev/null +++ b/typings/mysql/info.txt @@ -0,0 +1 @@ +temporary workaround, see https://github.com/sidorares/node-mysql2/issues/1210 diff --git a/typings/mysql/lib/Connection.d.ts b/typings/mysql/lib/Connection.d.ts new file mode 100644 index 0000000000..b6d169f711 --- /dev/null +++ b/typings/mysql/lib/Connection.d.ts @@ -0,0 +1,237 @@ + +import Query = require('./protocol/sequences/Query'); +import {OkPacket, FieldPacket, RowDataPacket, ResultSetHeader} from './protocol/packets/index'; +import {EventEmitter} from 'events'; + +declare namespace Connection { + + 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; + + /** + * 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 | 'local'; + + /** + * 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?: boolean | ((field: any, 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. 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?: 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?: Array; + + /** + * object with ssl parameters or a string containing name of ssl profile + */ + ssl?: string | SslOptions; + } + + export interface SslOptions { + /** + * A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates + */ + pfx?: string; + + /** + * A string holding the PEM encoded private key + */ + key?: string; + + /** + * A string of passphrase for the private key or pfx + */ + passphrase?: string; + + /** + * A string holding the PEM encoded certificate + */ + cert?: string; + + /** + * Either a string or list of strings of PEM encoded CA certificates to trust. + */ + ca?: string | string[]; + + /** + * Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List) + */ + crl?: string | string[]; + + /** + * A string describing the ciphers to use or exclude + */ + ciphers?: string; + + /** + * You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this. + */ + rejectUnauthorized?: boolean; + } +} + +declare class Connection extends EventEmitter { + + config: Connection.ConnectionOptions; + threadId: number; + + static createQuery(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; + static createQuery(sql: string, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; + + beginTransaction(callback: (err: Query.QueryError | null) => void): void; + + connect(callback?: (err: Query.QueryError | null) => void): void; + + commit(callback?: (err: Query.QueryError | null) => void): void; + + changeUser(options: Connection.ConnectionOptions, callback?: (err: Query.QueryError | null) => void): void; + + query(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; + query(sql: string, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; + query(options: Query.QueryOptions, callback?: (err: Query.QueryError | null, result: T, fields?: FieldPacket[]) => any): Query; + query(options: Query.QueryOptions, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; + + end(callback?: (err: Query.QueryError | null) => void): void; + end(options: any, callback?: (err: Query.QueryError | null) => void): void; + + destroy(): void; + + pause(): void; + + resume(): void; + + escape(value: any): string; + + escapeId(value: string): string; + escapeId(values: string[]): string; + + format(sql: string, values?: any | any[] | { [param: string]: any }): string; + + on(event: string, listener: Function): this; + + rollback(callback: () => void): void; +} + +export = Connection; diff --git a/typings/mysql/lib/Pool.d.ts b/typings/mysql/lib/Pool.d.ts new file mode 100644 index 0000000000..ebfb7a55c7 --- /dev/null +++ b/typings/mysql/lib/Pool.d.ts @@ -0,0 +1,65 @@ + +import Query = require('./protocol/sequences/Query'); +import {OkPacket, RowDataPacket, FieldPacket, ResultSetHeader} from './protocol/packets/index'; +import Connection = require('./Connection'); +import PoolConnection = require('./PoolConnection'); +import {EventEmitter} from 'events'; + +declare namespace Pool { + + export interface PoolOptions extends Connection.ConnectionOptions { + /** + * 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; + + /** + * Enable keep-alive on the socket. It's disabled by default, but the + * user can enable it and supply an initial delay. + */ + enableKeepAlive?: true; + + /** + * If keep-alive is enabled users can supply an initial delay. + */ + keepAliveInitialDelay?: number; + } +} + +declare class Pool extends EventEmitter { + + config: Pool.PoolOptions; + + getConnection(callback: (err: NodeJS.ErrnoException, connection: PoolConnection) => any): void; + + query(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; + query(sql: string, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; + query(options: Query.QueryOptions, callback?: (err: Query.QueryError | null, result: T, fields?: FieldPacket[]) => any): Query; + query(options: Query.QueryOptions, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; + + end(callback?: (err: NodeJS.ErrnoException | null, ...args: any[]) => any): void; + + on(event: string, listener: Function): this; + on(event: 'connection', listener: (connection: PoolConnection) => any): this; +} + +export = Pool; diff --git a/typings/mysql/lib/PoolCluster.d.ts b/typings/mysql/lib/PoolCluster.d.ts new file mode 100644 index 0000000000..92909abd4c --- /dev/null +++ b/typings/mysql/lib/PoolCluster.d.ts @@ -0,0 +1,56 @@ + +import Connection = require('./Connection'); +import PoolConnection = require('./PoolConnection'); +import {EventEmitter} from 'events'; + +declare namespace PoolCluster { + + export interface PoolClusterOptions { + /** + * 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; + } +} + +declare class PoolCluster extends EventEmitter { + + config: PoolCluster.PoolClusterOptions; + + add(config: PoolCluster.PoolClusterOptions): void; + add(group: string, config: PoolCluster.PoolClusterOptions): void; + + end(): void; + + getConnection(callback: (err: NodeJS.ErrnoException | null, connection: PoolConnection) => void): void; + getConnection(group: string, callback: (err: NodeJS.ErrnoException | null, connection: PoolConnection) => void): void; + getConnection(group: string, selector: string, callback: (err: NodeJS.ErrnoException | null, connection: PoolConnection) => void): void; + + of(pattern: string, selector?: string): PoolCluster; + + on(event: string, listener: Function): this; + on(event: 'remove', listener: (nodeId: number) => void): this; + on(event: 'connection', listener: (connection: PoolConnection) => void): this; +} + +export = PoolCluster; diff --git a/typings/mysql/lib/PoolConnection.d.ts b/typings/mysql/lib/PoolConnection.d.ts new file mode 100644 index 0000000000..2d7a6deb77 --- /dev/null +++ b/typings/mysql/lib/PoolConnection.d.ts @@ -0,0 +1,8 @@ + +import Connection = require('./Connection'); + +declare class PoolConnection extends Connection { + release(): void; +} + +export = PoolConnection; diff --git a/typings/mysql/lib/protocol/packets/Field.d.ts b/typings/mysql/lib/protocol/packets/Field.d.ts new file mode 100644 index 0000000000..e97574c3fa --- /dev/null +++ b/typings/mysql/lib/protocol/packets/Field.d.ts @@ -0,0 +1,16 @@ + +declare interface Field { + constructor: { + name: 'Field' + }; + db: string; + table: string; + name: string; + type: string; + length: number; + string: Function; + buffer: Function; + geometry: Function; +} + +export = Field; diff --git a/typings/mysql/lib/protocol/packets/FieldPacket.d.ts b/typings/mysql/lib/protocol/packets/FieldPacket.d.ts new file mode 100644 index 0000000000..de2a9fab69 --- /dev/null +++ b/typings/mysql/lib/protocol/packets/FieldPacket.d.ts @@ -0,0 +1,22 @@ + +declare interface FieldPacket { + constructor: { + name: 'FieldPacket' + }; + catalog: string; + charsetNr: number; + db: string; + decimals: number; + default: any; + flags: number; + length: number; + name: string; + orgName: string; + orgTable: string; + protocol41: boolean; + table: string; + type: number; + zerofill: boolean; +} + +export = FieldPacket; diff --git a/typings/mysql/lib/protocol/packets/OkPacket.d.ts b/typings/mysql/lib/protocol/packets/OkPacket.d.ts new file mode 100644 index 0000000000..3f0f0d9905 --- /dev/null +++ b/typings/mysql/lib/protocol/packets/OkPacket.d.ts @@ -0,0 +1,16 @@ + +declare interface OkPacket { + constructor: { + name: 'OkPacket' + }; + fieldCount: number; + affectedRows: number; + changedRows: number; + insertId: number; + serverStatus: number; + warningCount: number; + message: string; + procotol41: boolean; +} + +export = OkPacket; diff --git a/typings/mysql/lib/protocol/packets/ResultSetHeader.d.ts b/typings/mysql/lib/protocol/packets/ResultSetHeader.d.ts new file mode 100644 index 0000000000..b738f62098 --- /dev/null +++ b/typings/mysql/lib/protocol/packets/ResultSetHeader.d.ts @@ -0,0 +1,14 @@ + +declare interface ResultSetHeader { + constructor: { + name: 'ResultSetHeader' + }; + affectedRows: number; + fieldCount: number; + info: string; + insertId: number; + serverStatus: number; + warningStatus: number; +} + +export = ResultSetHeader; diff --git a/typings/mysql/lib/protocol/packets/RowDataPacket.d.ts b/typings/mysql/lib/protocol/packets/RowDataPacket.d.ts new file mode 100644 index 0000000000..8a2c2c3cb2 --- /dev/null +++ b/typings/mysql/lib/protocol/packets/RowDataPacket.d.ts @@ -0,0 +1,10 @@ + +declare interface RowDataPacket { + constructor: { + name: 'RowDataPacket' + }; + [column: string]: any; + [column: number]: any; +} + +export = RowDataPacket; diff --git a/typings/mysql/lib/protocol/packets/index.d.ts b/typings/mysql/lib/protocol/packets/index.d.ts new file mode 100644 index 0000000000..74aa1a3761 --- /dev/null +++ b/typings/mysql/lib/protocol/packets/index.d.ts @@ -0,0 +1,14 @@ + +import OkPacket = require('./OkPacket'); +import RowDataPacket = require('./RowDataPacket'); +import FieldPacket = require('./FieldPacket'); +import Field = require('./Field'); +import ResultSetHeader = require('./ResultSetHeader'); + +export { + OkPacket, + RowDataPacket, + FieldPacket, + Field, + ResultSetHeader +}; diff --git a/typings/mysql/lib/protocol/sequences/Query.d.ts b/typings/mysql/lib/protocol/sequences/Query.d.ts new file mode 100644 index 0000000000..9329d3b556 --- /dev/null +++ b/typings/mysql/lib/protocol/sequences/Query.d.ts @@ -0,0 +1,132 @@ + +import Sequence = require('./Sequence'); +import {OkPacket, RowDataPacket, FieldPacket} from '../packets/index'; +import {Readable} from 'stream'; + +declare namespace Query { + + export interface QueryOptions { + /** + * The SQL for the query + */ + sql: string; + + /** + * The values for the query + */ + values?: any | any[] | { [param: string]: 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?: any; + } + + export interface StreamOptions { + /** + * Sets the max buffer size in objects of a stream + */ + highWaterMark?: number; + + /** + * The object mode of the stream (Default: true) + */ + objectMode?: any; + } + + export interface QueryError extends NodeJS.ErrnoException { + /** + * 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 sql state marker + */ + sqlStateMarker?: string; + + /** + * The sql state + */ + sqlState?: string; + + /** + * The field count + */ + fieldCount?: number; + + /** + * Boolean, indicating if this error is terminal to the connection object. + */ + fatal: boolean; + } +} + +declare class Query extends Sequence { + + /** + * The SQL for a constructed query + */ + sql: string; + + /** + * Emits a query packet to start the query + */ + start(): void; + + /** + * Determines the packet class to use given the first byte of the packet. + * + * @param firstByte The first byte of the packet + * @param parser The packet parser + */ + determinePacket(firstByte: number, parser: any): any; + + /** + * Creates a Readable stream with the given options + * + * @param options The options for the stream. + */ + stream(options: Query.StreamOptions): Readable; + + on(event: string, listener: Function): this; + on(event: 'error', listener: (err: Query.QueryError) => any): this; + on(event: 'fields', listener: (fields: FieldPacket, index: number) => any): this; + on(event: 'result', listener: (result: RowDataPacket | OkPacket, index: number) => any): this; + on(event: 'end', listener: () => any): this; +} + +export = Query; diff --git a/typings/mysql/lib/protocol/sequences/Sequence.d.ts b/typings/mysql/lib/protocol/sequences/Sequence.d.ts new file mode 100644 index 0000000000..a520c42093 --- /dev/null +++ b/typings/mysql/lib/protocol/sequences/Sequence.d.ts @@ -0,0 +1,5 @@ + +import {EventEmitter} from 'events'; + +declare class Sequence extends EventEmitter { } +export = Sequence;