UNPKG

4.28 kBTypeScriptView Raw
1import { Sequence } from './Sequence.js';
2import { OkPacket, RowDataPacket, FieldPacket } from '../packets/index.js';
3import { Readable } from 'stream';
4import { TypeCast } from '../../parsers/typeCast.js';
5
6export interface QueryOptions {
7 /**
8 * The SQL for the query
9 */
10 sql: string;
11
12 /**
13 * The values for the query
14 */
15 values?: any | any[] | { [param: string]: any };
16
17 /**
18 * This overrides the namedPlaceholders option set at the connection level.
19 */
20 namedPlaceholders?: boolean;
21
22 /**
23 * Every operation takes an optional inactivity timeout option. This allows you to specify appropriate timeouts for
24 * operations. It is important to note that these timeouts are not part of the MySQL protocol, and rather timeout
25 * operations through the client. This means that when a timeout is reached, the connection it occurred on will be
26 * destroyed and no further operations can be performed.
27 */
28 timeout?: number;
29
30 /**
31 * Either a boolean or string. If true, tables will be nested objects. If string (e.g. '_'), tables will be
32 * nested as tableName_fieldName
33 */
34 nestTables?: any;
35
36 /**
37 * Determines if column values should be converted to native JavaScript types.
38 *
39 * @default true
40 *
41 * 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.
42 *
43 * ---
44 *
45 * You can also specify a function to do the type casting yourself:
46 * ```ts
47 * (field: Field, next: () => void) => {
48 * return next();
49 * }
50 * ```
51 *
52 * ---
53 *
54 * **WARNING:**
55 *
56 * YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once:
57 *
58 * ```js
59 * field.string();
60 * field.buffer();
61 * field.geometry();
62 * ```
63
64 * Which are aliases for:
65 *
66 * ```js
67 * parser.parseLengthCodedString();
68 * parser.parseLengthCodedBuffer();
69 * parser.parseGeometryValue();
70 * ```
71 *
72 * You can find which field function you need to use by looking at `RowDataPacket.prototype._typeCast`.
73 */
74 typeCast?: TypeCast;
75
76 /**
77 * This overrides the same option set at the connection level.
78 *
79 */
80 rowsAsArray?: boolean;
81
82 /**
83 * By specifying a function that returns a readable stream, an arbitrary stream can be sent when sending a local fs file.
84 */
85 infileStreamFactory?: (path: string) => Readable;
86}
87
88export interface StreamOptions {
89 /**
90 * Sets the max buffer size in objects of a stream
91 */
92 highWaterMark?: number;
93
94 /**
95 * The object mode of the stream (Default: true)
96 */
97 objectMode?: any;
98}
99
100export interface QueryError extends NodeJS.ErrnoException {
101 /**
102 * Either a MySQL server error (e.g. 'ER_ACCESS_DENIED_ERROR'),
103 * a node.js error (e.g. 'ECONNREFUSED') or an internal error
104 * (e.g. 'PROTOCOL_CONNECTION_LOST').
105 */
106 code: string;
107
108 /**
109 * The sql state marker
110 */
111 sqlStateMarker?: string;
112
113 /**
114 * The sql state
115 */
116 sqlState?: string;
117
118 /**
119 * The field count
120 */
121 fieldCount?: number;
122
123 /**
124 * Boolean, indicating if this error is terminal to the connection object.
125 */
126 fatal: boolean;
127}
128
129declare class Query extends Sequence {
130 /**
131 * The SQL for a constructed query
132 */
133 sql: string;
134
135 /**
136 * Emits a query packet to start the query
137 */
138 start(): void;
139
140 /**
141 * Determines the packet class to use given the first byte of the packet.
142 *
143 * @param firstByte The first byte of the packet
144 * @param parser The packet parser
145 */
146 determinePacket(firstByte: number, parser: any): any;
147
148 /**
149 * Creates a Readable stream with the given options
150 *
151 * @param options The options for the stream.
152 */
153 stream(options?: StreamOptions): Readable;
154
155 on(event: string, listener: (...args: any[]) => void): this;
156 on(event: 'error', listener: (err: QueryError) => any): this;
157 on(
158 event: 'fields',
159 listener: (fields: FieldPacket, index: number) => any,
160 ): this;
161 on(
162 event: 'result',
163 listener: (result: RowDataPacket | OkPacket, index: number) => any,
164 ): this;
165 on(event: 'end', listener: () => any): this;
166}
167
168export type QueryableConstructor<T = object> = new (...args: any[]) => T;
169
170export { Query };