Skip to content

Commit a9c8d09

Browse files
fix: types for query values (#3985)
* fix: types for query values * docs: add TypeScript documentation for QueryValues * fix: support named placeholders in QueryValues type * Update website/docs/documentation/typescript-examples.mdx --------- Co-authored-by: Weslley Araújo <46850407+wellwelwel@users.noreply.github.com>
1 parent 40641ee commit a9c8d09

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

typings/mysql/lib/protocol/sequences/Query.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { OkPacket, RowDataPacket, FieldPacket } from '../packets/index.js';
33
import { Readable } from 'stream';
44
import { TypeCast } from '../../parsers/typeCast.js';
55

6+
export type QueryValues =
7+
| string
8+
| number
9+
| bigint
10+
| boolean
11+
| Date
12+
| null
13+
| Blob
14+
| Buffer
15+
| ({} | null)[]
16+
| { [key: string]: QueryValues };
17+
618
export interface QueryOptions {
719
/**
820
* The SQL for the query
@@ -12,7 +24,7 @@ export interface QueryOptions {
1224
/**
1325
* The values for the query
1426
*/
15-
values?: any | any[] | { [param: string]: any };
27+
values?: QueryValues;
1628

1729
/**
1830
* This overrides the namedPlaceholders option set at the connection level.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FieldPacket, QueryResult } from '../../packets/index.js';
2-
import { QueryOptions, QueryableConstructor } from '../Query.js';
2+
import { QueryOptions, QueryableConstructor, QueryValues } from '../Query.js';
33

44
export declare function ExecutableBase<T extends QueryableConstructor>(
55
Base?: T
@@ -8,14 +8,14 @@ export declare function ExecutableBase<T extends QueryableConstructor>(
88
execute<T extends QueryResult>(sql: string): Promise<[T, FieldPacket[]]>;
99
execute<T extends QueryResult>(
1010
sql: string,
11-
values: any
11+
values: QueryValues
1212
): Promise<[T, FieldPacket[]]>;
1313
execute<T extends QueryResult>(
1414
options: QueryOptions
1515
): Promise<[T, FieldPacket[]]>;
1616
execute<T extends QueryResult>(
1717
options: QueryOptions,
18-
values: any
18+
values: QueryValues
1919
): Promise<[T, FieldPacket[]]>;
2020
};
2121
} & T;

website/docs/documentation/typescript-examples.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,46 @@ For `ProcedureCallPacket<RowDataPacket[]>`, please see the following examples.
381381

382382
<hr />
383383

384+
### QueryValues
385+
386+
<Stability level={2} />
387+
388+
The `QueryValues` type represents the allowed types for query parameter values:
389+
390+
```ts
391+
import mysql, { RowDataPacket, QueryValues } from 'mysql2';
392+
393+
const conn = mysql.createConnection({
394+
user: 'test',
395+
database: 'test',
396+
});
397+
398+
const values: QueryValues = [1, 'test', null];
399+
400+
conn.query<RowDataPacket[]>(
401+
'SELECT * FROM users WHERE id = ? AND name = ? AND deleted_at = ?',
402+
values,
403+
(_err, rows) => {
404+
console.log(rows);
405+
}
406+
);
407+
```
408+
409+
The `QueryValues` type includes the following types:
410+
411+
- `string`
412+
- `number`
413+
- `bigint`
414+
- `boolean`
415+
- `Date`
416+
- `null`
417+
- `Blob`
418+
- `Buffer`
419+
- `({} | null)[]` (array of values)
420+
- `{ [key: string]: QueryValues }` (named placeholders)
421+
422+
<hr />
423+
384424
### OkPacket
385425

386426
<Stability

0 commit comments

Comments
 (0)