Skip to content

Commit c21f9bf

Browse files
committed
docs: introduce TypeScript documentation
1 parent 8ca36c7 commit c21f9bf

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@
5757
}
5858
},
5959
{
60-
"files": ["**/*.md/*js"],
60+
"files": ["**/*.md/*js", "**/*.md/*ts"],
6161
"rules": {
6262
"no-undef": "off",
6363
"no-unused-vars": "off",
64+
"@typescript-eslint/no-unused-vars": "off",
6465
"no-console": "off",
6566
"no-unused-labels": "off",
6667
"strict": "off",

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ MySQL2 is free from native bindings and can be installed on Linux, Mac OS or Win
5353
npm install --save mysql2
5454
```
5555

56+
If you are using TypeScript, you will need to install `@types/node`.
57+
58+
```bash
59+
npm install --save-dev @types/node
60+
```
61+
62+
> For TypeScript documentation and examples, see [here](./documentation/en/TypeScript-Examples.md).
63+
5664
## First Query
5765
```js
5866
// get the client
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Using MySQL2 with TypeScript
2+
3+
## Installation
4+
```bash
5+
npm install --save mysql2
6+
npm install --save-dev @types/node
7+
```
8+
9+
> The `@types/node` ensure the proper interaction between **TypeScript** and the **Node.js** modules used by **MySQL2** (*net*, *events*, *stream*, *tls*, etc.).
10+
11+
## Import
12+
You can import **MySQL2** in two ways:
13+
```ts
14+
import * as mysql from 'mysql2';
15+
import * as mysql from 'mysql2/promise';
16+
17+
// By setting the `esModuleInterop` option to `true` in `tsconfig.json`
18+
import mysql from 'mysql2';
19+
import mysql from 'mysql2/promise';
20+
```
21+
22+
## Connection
23+
```ts
24+
import mysql, { ConnectionOptions } from 'mysql2';
25+
26+
const access: ConnectionOptions = {
27+
user: 'test',
28+
database: 'test',
29+
};
30+
31+
const conn = mysql.createConnection(access);
32+
```
33+
34+
## Pool Connection
35+
```ts
36+
import mysql, { PoolOptions } from 'mysql2';
37+
38+
const access: PoolOptions = {
39+
user: 'test',
40+
database: 'test',
41+
};
42+
43+
const conn = mysql.createPool(access);
44+
```
45+
46+
## Query and Execute
47+
### A simple SELECT
48+
```ts
49+
conn.query('SELECT 1 + 1 AS `test`;', (_err, rows) => {
50+
/**
51+
* @rows: [ { test: 2 } ]
52+
*/
53+
});
54+
```
55+
56+
The `rows` output will be these possible types:
57+
- `RowDataPacket[]`
58+
- `RowDataPacket[][]`
59+
- `ResultSetHeader`
60+
- `ProcedureCallPacket`
61+
- `OkPacket`
62+
- `OkPacket[]`
63+
64+
In this example, you need to manually check the output types
65+
66+
---
67+
68+
### Type Specification
69+
#### RowDataPacket[]
70+
An array with the returned rows, for example:
71+
```ts
72+
import mysql, { RowDataPacket } from 'mysql2';
73+
74+
const conn = mysql.createConnection({
75+
user: 'test',
76+
database: 'test',
77+
});
78+
79+
conn.query<RowDataPacket[]>('SELECT 1 + 1 AS `test`;', (_err, rows) => {
80+
console.log(rows);
81+
/**
82+
* @rows: [ { test: 2 } ]
83+
*/
84+
});
85+
```
86+
87+
---
88+
89+
#### RowDataPacket[][]
90+
- `rowsAsArray`
91+
```ts
92+
import mysql, { RowDataPacket } from 'mysql2';
93+
94+
const conn = mysql.createConnection({
95+
user: 'test',
96+
database: 'test',
97+
rowsAsArray: true,
98+
});
99+
100+
const sql = `
101+
SELECT 1 + 1 AS test, 2 + 2 AS test;
102+
`;
103+
104+
conn.query<RowDataPacket[][]>(sql, (_err, rows) => {
105+
console.log(rows);
106+
/**
107+
* @rows: [ [ 2, 4 ] ]
108+
*/
109+
});
110+
```
111+
112+
- `multipleStatements` with multiple queries
113+
```ts
114+
import mysql, { RowDataPacket } from 'mysql2';
115+
116+
const conn = mysql.createConnection({
117+
user: 'test',
118+
database: 'test',
119+
multipleStatements: true,
120+
});
121+
122+
const sql = `
123+
SELECT 1 + 1 AS test;
124+
SELECT 2 + 2 AS test;
125+
`;
126+
127+
conn.query<RowDataPacket[][]>(sql, (_err, rows) => {
128+
console.log(rows);
129+
/**
130+
* @rows: [ [ { test: 2 } ], [ { test: 4 } ] ]
131+
*/
132+
});
133+
```
134+
135+
#### ResultSetHeader
136+
> In progress
137+
138+
#### ProcedureCallPacket
139+
> In progress
140+
141+
#### OkPacket
142+
> In progress
143+
144+
#### OkPacket[]
145+
> In progress
146+
147+
---
148+
149+
## Examples
150+
151+
You can also check various code examples using **MySQL2** and **TypeScript** to understand advanced concepts:
152+
> In progress
153+
154+
- Extending and using **Interfaces** with `RowDataPacket`.
155+
- Checking for `ResultSetHeader` or `RowDataPacket[]` using **Procedure Calls**
156+
- Checking for `ResultSetHeader` or `RowDataPacket[][]` using **Procedure Calls**
157+
- Creating a custom **MySQL2** **Class**

0 commit comments

Comments
 (0)