mysql2 3.3.3 → 3.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -35
- package/index.d.ts +4 -2
- package/lib/commands/query.js +1 -1
- package/package.json +2 -2
- package/promise.d.ts +4 -5
- package/promise.js +4 -0
- package/typings/mysql/lib/Pool.d.ts +2 -0
package/README.md
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
[![Windows Build][appveyor-image]][appveyor-url]
|
|
9
9
|
[![License][license-image]][license-url]
|
|
10
10
|
|
|
11
|
-
English | [简体中文](./
|
|
11
|
+
English | [简体中文](./documentation/zh-cn/) | [Português (BR)](./documentation/pt-br/)
|
|
12
12
|
|
|
13
|
-
> MySQL client for Node.js with focus on performance. Supports prepared statements, non-utf8 encodings, binary log protocol, compression, ssl [much more](
|
|
13
|
+
> MySQL client for Node.js with focus on performance. Supports prepared statements, non-utf8 encodings, binary log protocol, compression, ssl [much more](./documentation/en).
|
|
14
14
|
|
|
15
15
|
__Table of contents__
|
|
16
16
|
|
|
@@ -20,6 +20,9 @@ __Table of contents__
|
|
|
20
20
|
- [Using Prepared Statements](#using-prepared-statements)
|
|
21
21
|
- [Using connection pools](#using-connection-pools)
|
|
22
22
|
- [Using Promise Wrapper](#using-promise-wrapper)
|
|
23
|
+
- [Array Results](#array-results)
|
|
24
|
+
- [Connection Level](#connection-level)
|
|
25
|
+
- [Query Level](#query-level)
|
|
23
26
|
- [API and Configuration](#api-and-configuration)
|
|
24
27
|
- [Documentation](#documentation)
|
|
25
28
|
- [Acknowledgements](#acknowledgements)
|
|
@@ -29,17 +32,17 @@ __Table of contents__
|
|
|
29
32
|
|
|
30
33
|
MySQL2 project is a continuation of [MySQL-Native][mysql-native]. Protocol parser code was rewritten from scratch and api changed to match popular [mysqljs/mysql][node-mysql]. MySQL2 team is working together with [mysqljs/mysql][node-mysql] team to factor out shared code and move it under [mysqljs][node-mysql] organisation.
|
|
31
34
|
|
|
32
|
-
MySQL2 is mostly API compatible with [mysqljs][node-mysql] and supports majority of features. MySQL2 also offers these additional features
|
|
35
|
+
MySQL2 is mostly API compatible with [mysqljs][node-mysql] and supports majority of features. MySQL2 also offers these additional features:
|
|
33
36
|
|
|
34
37
|
- Faster / Better Performance
|
|
35
|
-
- [Prepared Statements](
|
|
38
|
+
- [Prepared Statements](./documentation/en/Prepared-Statements.md)
|
|
36
39
|
- MySQL Binary Log Protocol
|
|
37
|
-
- [MySQL Server](
|
|
40
|
+
- [MySQL Server](./documentation/en/MySQL-Server.md)
|
|
38
41
|
- Extended support for Encoding and Collation
|
|
39
|
-
- [Promise Wrapper](
|
|
42
|
+
- [Promise Wrapper](./documentation/en/Promise-Wrapper.md)
|
|
40
43
|
- Compression
|
|
41
|
-
- SSL and [Authentication Switch](
|
|
42
|
-
- [Custom Streams](
|
|
44
|
+
- SSL and [Authentication Switch](./documentation/en/Authentication-Switch.md)
|
|
45
|
+
- [Custom Streams](./documentation/en/Extras.md)
|
|
43
46
|
- [Pooling](#using-connection-pools)
|
|
44
47
|
|
|
45
48
|
## Installation
|
|
@@ -51,7 +54,6 @@ npm install --save mysql2
|
|
|
51
54
|
```
|
|
52
55
|
|
|
53
56
|
## First Query
|
|
54
|
-
|
|
55
57
|
```js
|
|
56
58
|
// get the client
|
|
57
59
|
const mysql = require('mysql2');
|
|
@@ -84,11 +86,11 @@ connection.query(
|
|
|
84
86
|
|
|
85
87
|
## Using Prepared Statements
|
|
86
88
|
|
|
87
|
-
With MySQL2 you also get the prepared statements. With prepared statements MySQL doesn't have to prepare plan for same query
|
|
89
|
+
With MySQL2 you also get the prepared statements. With prepared statements MySQL doesn't have to prepare plan for same query every time, this results in better performance. If you don't know why they are important, please check these discussions:
|
|
88
90
|
|
|
89
91
|
- [How prepared statements can protect from SQL Injection attacks](http://stackoverflow.com/questions/8263371/how-can-prepared-statements-protect-from-sql-injection-attacks)
|
|
90
92
|
|
|
91
|
-
|
|
93
|
+
MySQL2 provides `execute` helper which will prepare and query the statement. You can also manually prepare / unprepare statement with `prepare` / `unprepare` methods.
|
|
92
94
|
|
|
93
95
|
```js
|
|
94
96
|
// get the client
|
|
@@ -144,27 +146,25 @@ The pool does not create all connections upfront but creates them on demand unti
|
|
|
144
146
|
You can use the pool in the same way as connections (using `pool.query()` and `pool.execute()`):
|
|
145
147
|
```js
|
|
146
148
|
// For pool initialization, see above
|
|
147
|
-
pool.query("SELECT field FROM
|
|
148
|
-
|
|
149
|
-
})
|
|
149
|
+
pool.query("SELECT `field` FROM `table`", function(err, rows, fields) {
|
|
150
|
+
// Connection is automatically released when query resolves
|
|
151
|
+
});
|
|
150
152
|
```
|
|
151
153
|
|
|
152
154
|
Alternatively, there is also the possibility of manually acquiring a connection from the pool and returning it later:
|
|
153
155
|
```js
|
|
154
156
|
// For pool initialization, see above
|
|
155
157
|
pool.getConnection(function(err, conn) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
})
|
|
158
|
+
// Do something with the connection
|
|
159
|
+
conn.query(/* ... */);
|
|
160
|
+
// Don't forget to release the connection when finished!
|
|
161
|
+
pool.releaseConnection(conn);
|
|
162
|
+
});
|
|
161
163
|
```
|
|
162
164
|
|
|
163
165
|
## Using Promise Wrapper
|
|
164
166
|
|
|
165
167
|
MySQL2 also support Promise API. Which works very well with ES7 async await.
|
|
166
|
-
|
|
167
|
-
<!--eslint-disable-next-block-->
|
|
168
168
|
```js
|
|
169
169
|
async function main() {
|
|
170
170
|
// get the client
|
|
@@ -176,9 +176,7 @@ async function main() {
|
|
|
176
176
|
}
|
|
177
177
|
```
|
|
178
178
|
|
|
179
|
-
MySQL2 use default `Promise` object available in scope. But you can choose which `Promise` implementation you want to use
|
|
180
|
-
|
|
181
|
-
<!--eslint-disable-next-block-->
|
|
179
|
+
MySQL2 use default `Promise` object available in scope. But you can choose which `Promise` implementation you want to use.
|
|
182
180
|
```js
|
|
183
181
|
// get the client
|
|
184
182
|
const mysql = require('mysql2/promise');
|
|
@@ -193,7 +191,7 @@ const connection = await mysql.createConnection({host:'localhost', user: 'root',
|
|
|
193
191
|
const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', ['Morty', 14]);
|
|
194
192
|
```
|
|
195
193
|
|
|
196
|
-
MySQL2 also exposes a .promise() function on Pools, so you can create a promise/non-promise connections from the same pool
|
|
194
|
+
MySQL2 also exposes a .promise() function on Pools, so you can create a promise/non-promise connections from the same pool.
|
|
197
195
|
```js
|
|
198
196
|
async function main() {
|
|
199
197
|
// get the client
|
|
@@ -204,9 +202,10 @@ async function main() {
|
|
|
204
202
|
const promisePool = pool.promise();
|
|
205
203
|
// query database using promises
|
|
206
204
|
const [rows,fields] = await promisePool.query("SELECT 1");
|
|
205
|
+
}
|
|
207
206
|
```
|
|
208
207
|
|
|
209
|
-
MySQL2 exposes a .promise() function on Connections, to "upgrade" an existing non-promise connection to use promise
|
|
208
|
+
MySQL2 exposes a .promise() function on Connections, to "upgrade" an existing non-promise connection to use promise.
|
|
210
209
|
```js
|
|
211
210
|
// get the client
|
|
212
211
|
const mysql = require('mysql2');
|
|
@@ -222,7 +221,7 @@ con.promise().query("SELECT 1")
|
|
|
222
221
|
.then( () => con.end());
|
|
223
222
|
```
|
|
224
223
|
|
|
225
|
-
## Array
|
|
224
|
+
## Array Results
|
|
226
225
|
|
|
227
226
|
If you have two columns with the same name, you might want to get results as an array rather than an object to prevent them from clashing. This is a deviation from the [Node MySQL][node-mysql] library.
|
|
228
227
|
|
|
@@ -230,22 +229,19 @@ For example: `select 1 as foo, 2 as foo`.
|
|
|
230
229
|
|
|
231
230
|
You can enable this setting at either the connection level (applies to all queries), or at the query level (applies only to that specific query).
|
|
232
231
|
|
|
233
|
-
### Connection
|
|
232
|
+
### Connection Level
|
|
234
233
|
```js
|
|
235
234
|
const con = mysql.createConnection(
|
|
236
235
|
{ host: 'localhost', database: 'test', user: 'root', rowsAsArray: true }
|
|
237
236
|
);
|
|
238
|
-
|
|
239
237
|
```
|
|
240
238
|
|
|
241
|
-
### Query
|
|
242
|
-
|
|
239
|
+
### Query Level
|
|
243
240
|
```js
|
|
244
241
|
con.query({ sql: 'select 1 as foo, 2 as foo', rowsAsArray: true }, function(err, results, fields) {
|
|
245
|
-
console.log(results) // will be an array of arrays rather than an array of objects
|
|
246
|
-
console.log(fields) //
|
|
242
|
+
console.log(results); // in this query, results will be an array of arrays rather than an array of objects
|
|
243
|
+
console.log(fields); // fields are unchanged
|
|
247
244
|
});
|
|
248
|
-
|
|
249
245
|
```
|
|
250
246
|
|
|
251
247
|
## API and Configuration
|
|
@@ -258,7 +254,7 @@ If you find any other incompatibility with [Node MySQL][node-mysql], Please repo
|
|
|
258
254
|
|
|
259
255
|
## Documentation
|
|
260
256
|
|
|
261
|
-
You can find more detailed documentation [here](
|
|
257
|
+
You can find more detailed documentation [here](./documentation/en). You should also check various code [examples](./examples) to understand advanced concepts.
|
|
262
258
|
|
|
263
259
|
## Acknowledgements
|
|
264
260
|
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Connection as PromiseConnection,
|
|
3
|
+
Pool as PromisePool,
|
|
3
4
|
PoolConnection as PromisePoolConnection,
|
|
4
5
|
} from './promise';
|
|
5
6
|
|
|
@@ -83,7 +84,7 @@ export interface Connection extends mysql.Connection {
|
|
|
83
84
|
}
|
|
84
85
|
|
|
85
86
|
export interface PoolConnection extends mysql.PoolConnection {
|
|
86
|
-
promise(promiseImpl?: PromiseConstructor):
|
|
87
|
+
promise(promiseImpl?: PromiseConstructor): PromisePool;
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
export interface Pool extends mysql.Connection {
|
|
@@ -152,13 +153,14 @@ export interface Pool extends mysql.Connection {
|
|
|
152
153
|
getConnection(
|
|
153
154
|
callback: (err: NodeJS.ErrnoException, connection: PoolConnection) => any
|
|
154
155
|
): void;
|
|
156
|
+
releaseConnection(connection: PoolConnection | PromisePoolConnection): void;
|
|
155
157
|
on(event: 'connection', listener: (connection: PoolConnection) => any): this;
|
|
156
158
|
on(event: 'acquire', listener: (connection: PoolConnection) => any): this;
|
|
157
159
|
on(event: 'release', listener: (connection: PoolConnection) => any): this;
|
|
158
160
|
on(event: 'enqueue', listener: () => any): this;
|
|
159
161
|
unprepare(sql: string): mysql.PrepareStatementInfo;
|
|
160
162
|
prepare(sql: string, callback?: (err: mysql.QueryError | null, statement: mysql.PrepareStatementInfo) => any): mysql.Prepare;
|
|
161
|
-
promise(promiseImpl?: PromiseConstructor):
|
|
163
|
+
promise(promiseImpl?: PromiseConstructor): PromisePool;
|
|
162
164
|
config: mysql.PoolOptions;
|
|
163
165
|
}
|
|
164
166
|
|
package/lib/commands/query.js
CHANGED
|
@@ -37,7 +37,7 @@ class Query extends Command {
|
|
|
37
37
|
|
|
38
38
|
then() {
|
|
39
39
|
const err =
|
|
40
|
-
"You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface. To learn how to use async/await or Promises check out documentation at https://www.npmjs.com/package/mysql2#using-promise-wrapper, or the mysql2 documentation at https://github.com/sidorares/node-mysql2/tree/master/documentation/Promise-Wrapper.md";
|
|
40
|
+
"You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface. To learn how to use async/await or Promises check out documentation at https://www.npmjs.com/package/mysql2#using-promise-wrapper, or the mysql2 documentation at https://github.com/sidorares/node-mysql2/tree/master/documentation/en/Promise-Wrapper.md";
|
|
41
41
|
// eslint-disable-next-line
|
|
42
42
|
console.log(err);
|
|
43
43
|
throw new Error(err);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mysql2",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.4",
|
|
4
4
|
"description": "fast mysql driver. Implements core protocol, prepared statements, ssl and compression in native JS",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"lint": "npm run lint:docs && npm run lint:code",
|
|
12
12
|
"lint:code": "eslint index.js promise.js \"lib/**/*.js\" \"test/**/*.js\" \"benchmarks/**/*.js\"",
|
|
13
|
-
"lint:docs": "eslint Contributing.md \"documentation/**/*.md\" \"examples/*.js\"",
|
|
13
|
+
"lint:docs": "eslint Contributing.md README.md \"documentation/**/*.md\" \"examples/*.js\"",
|
|
14
14
|
"test": "node ./test/run.js",
|
|
15
15
|
"test:tsc-build": "cd \"test/tsc-build\" && npx tsc -p \"tsconfig.json\"",
|
|
16
16
|
"coverage-test": "c8 -r cobertura -r lcov -r text node ./test/run.js",
|
package/promise.d.ts
CHANGED
|
@@ -83,12 +83,11 @@ export interface Connection extends EventEmitter {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
export interface PoolConnection extends Connection {
|
|
86
|
-
connection: Connection;
|
|
87
|
-
getConnection(): Promise<PoolConnection>;
|
|
88
86
|
release(): void;
|
|
87
|
+
connection: Connection;
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
export interface Pool extends EventEmitter {
|
|
90
|
+
export interface Pool extends EventEmitter, Connection {
|
|
92
91
|
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
|
|
93
92
|
sql: string
|
|
94
93
|
): Promise<[T, FieldPacket[]]>;
|
|
@@ -128,6 +127,7 @@ export interface Pool extends EventEmitter {
|
|
|
128
127
|
): Promise<[T, FieldPacket[]]>;
|
|
129
128
|
|
|
130
129
|
getConnection(): Promise<PoolConnection>;
|
|
130
|
+
releaseConnection(connection: PoolConnection): void;
|
|
131
131
|
on(event: 'connection', listener: (connection: PoolConnection) => any): this;
|
|
132
132
|
on(event: 'acquire', listener: (connection: PoolConnection) => any): this;
|
|
133
133
|
on(event: 'release', listener: (connection: PoolConnection) => any): this;
|
|
@@ -138,7 +138,7 @@ export interface Pool extends EventEmitter {
|
|
|
138
138
|
escapeId(value: string): string;
|
|
139
139
|
escapeId(values: string[]): string;
|
|
140
140
|
format(sql: string, values?: any | any[] | { [param: string]: any }): string;
|
|
141
|
-
|
|
141
|
+
|
|
142
142
|
pool: CorePool;
|
|
143
143
|
}
|
|
144
144
|
|
|
@@ -153,4 +153,3 @@ export interface PreparedStatementInfo {
|
|
|
153
153
|
close(): Promise<void>;
|
|
154
154
|
execute(parameters: any[]): Promise<[RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader, FieldPacket[]]>;
|
|
155
155
|
}
|
|
156
|
-
|
package/promise.js
CHANGED
|
@@ -346,6 +346,10 @@ class PromisePool extends EventEmitter {
|
|
|
346
346
|
});
|
|
347
347
|
}
|
|
348
348
|
|
|
349
|
+
releaseConnection(connection) {
|
|
350
|
+
if (connection instanceof PromisePoolConnection) connection.release();
|
|
351
|
+
}
|
|
352
|
+
|
|
349
353
|
query(sql, args) {
|
|
350
354
|
const corePool = this.pool;
|
|
351
355
|
const localErr = new Error();
|
|
@@ -61,6 +61,8 @@ declare class Pool extends EventEmitter {
|
|
|
61
61
|
|
|
62
62
|
getConnection(callback: (err: NodeJS.ErrnoException | null, connection: PoolConnection) => any): void;
|
|
63
63
|
|
|
64
|
+
releaseConnection(connection: PoolConnection): void;
|
|
65
|
+
|
|
64
66
|
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
|
|
65
67
|
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;
|
|
66
68
|
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(options: Query.QueryOptions, callback?: (err: Query.QueryError | null, result: T, fields?: FieldPacket[]) => any): Query;
|