mysql2 3.6.4 → 3.7.0

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 CHANGED
@@ -1,49 +1,72 @@
1
- ## MySQL 2
1
+ [npm-image]: https://img.shields.io/npm/v/mysql2.svg
2
+ [npm-url]: https://npmjs.com/package/mysql2
3
+ [node-version-image]: https://img.shields.io/node/v/mysql2.svg
4
+ [node-version-url]: https://nodejs.org/en/download
5
+ [downloads-image]: https://img.shields.io/npm/dm/mysql2.svg
6
+ [downloads-url]: https://npmjs.com/package/mysql2
7
+ [license-url]: https://github.com/sidorares/node-mysql2/blob/master/License
8
+ [license-image]: https://img.shields.io/npm/l/mysql2.svg?maxAge=2592000
9
+ [node-mysql]: https://github.com/mysqljs/mysql
10
+ [mysqljs]: https://github.com/mysqljs
11
+ [mysql-native]: https://github.com/sidorares/nodejs-mysql-native
12
+ [sidorares]: https://github.com/sidorares
13
+ [TooTallNate]: https://gist.github.com/TooTallNate
14
+ [starttls.js]: https://gist.github.com/TooTallNate/848444
15
+ [node-mariasql]: https://github.com/mscdex/node-mariasql
16
+ [contributors]: https://github.com/sidorares/node-mysql2/graphs/contributors
17
+ [contributing]: https://github.com/sidorares/node-mysql2/blob/master/Contributing.md
18
+ [docs-base]: https://sidorares.github.io/node-mysql2/docs
19
+ [docs-base-zh-CN]: https://sidorares.github.io/node-mysql2/zh-CN/docs
20
+ [docs-base-pt-BR]: https://sidorares.github.io/node-mysql2/pt-BR/docs
21
+ [docs-prepared-statements]: https://sidorares.github.io/node-mysql2/docs/documentation/prepared-statements
22
+ [docs-mysql-server]: https://sidorares.github.io/node-mysql2/docs/documentation/mysql-server
23
+ [docs-promise-wrapper]: https://sidorares.github.io/node-mysql2/docs/documentation/promise-wrapper
24
+ [docs-authentication-switch]: https://sidorares.github.io/node-mysql2/docs/documentation/authentication-switch
25
+ [docs-streams]: https://sidorares.github.io/node-mysql2/docs/documentation/extras
26
+ [docs-typescript-docs]: https://sidorares.github.io/node-mysql2/docs/documentation/typescript-examples
27
+ [docs-qs-pooling]: https://sidorares.github.io/node-mysql2/docs#using-connection-pools
28
+ [docs-qs-first-query]: https://sidorares.github.io/node-mysql2/docs#first-query
29
+ [docs-qs-using-prepared-statements]: https://sidorares.github.io/node-mysql2/docs#using-prepared-statements
30
+ [docs-examples]: https://sidorares.github.io/node-mysql2/docs/examples
31
+ [docs-faq]: https://sidorares.github.io/node-mysql2/docs/faq
32
+ [docs-documentation]: https://sidorares.github.io/node-mysql2/docs/documentation
33
+ [docs-contributing]: https://sidorares.github.io/node-mysql2/docs/contributing/website
34
+
35
+ # MySQL2
2
36
 
3
- [![Greenkeeper badge](https://badges.greenkeeper.io/sidorares/node-mysql2.svg)](https://greenkeeper.io/)
4
37
  [![NPM Version][npm-image]][npm-url]
5
38
  [![NPM Downloads][downloads-image]][downloads-url]
6
39
  [![Node.js Version][node-version-image]][node-version-url]
7
- [![Linux Build][travis-image]][travis-url]
8
- [![Windows Build][appveyor-image]][appveyor-url]
9
- [![License][license-image]][license-url]
40
+ [![License][license-image]][license-url]
10
41
 
11
- English | [简体中文](./documentation/zh-cn/) | [Português (BR)](./documentation/pt-br/)
42
+ [English][docs-base] | [简体中文][docs-base-zh-CN] | [Português (BR)][docs-base-pt-BR]
12
43
 
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).
44
+ > MySQL client for Node.js with focus on performance. Supports prepared statements, non-utf8 encodings, binary log protocol, compression, ssl [much more][docs-documentation].
14
45
 
15
- __Table of contents__
46
+ **Table of Contents**
16
47
 
17
- - [History and Why MySQL2](#history-and-why-mysql2)
18
- - [Installation](#installation)
19
- - [First Query](#first-query)
20
- - [Using Prepared Statements](#using-prepared-statements)
21
- - [Using connection pools](#using-connection-pools)
22
- - [Using Promise Wrapper](#using-promise-wrapper)
23
- - [Array Results](#array-results)
24
- - [Connection Level](#connection-level)
25
- - [Query Level](#query-level)
26
- - [API and Configuration](#api-and-configuration)
27
- - [Documentation](#documentation)
28
- - [Acknowledgements](#acknowledgements)
29
- - [Contributing](#contributing)
48
+ - [History and Why MySQL2](#history-and-why-mysql2)
49
+ - [Installation](#installation)
50
+ - [Documentation](#documentation)
51
+ - [Acknowledgements](#acknowledgements)
52
+ - [Contributing](#contributing)
30
53
 
31
54
  ## History and Why MySQL2
32
55
 
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.
56
+ MySQL2 project is a continuation of [MySQL-Native][mysql-native]. Protocol parser code was rewritten from scratch and api changed to match popular [Node MySQL][node-mysql]. MySQL2 team is working together with [Node MySQL][node-mysql] team to factor out shared code and move it under [mysqljs][mysqljs] organization.
34
57
 
35
- MySQL2 is mostly API compatible with [mysqljs][node-mysql] and supports majority of features. MySQL2 also offers these additional features:
58
+ MySQL2 is mostly API compatible with [Node MySQL][node-mysql] and supports majority of features. MySQL2 also offers these additional features:
36
59
 
37
- - Faster / Better Performance
38
- - [Prepared Statements](./documentation/en/Prepared-Statements.md)
39
- - MySQL Binary Log Protocol
40
- - [MySQL Server](./documentation/en/MySQL-Server.md)
41
- - Extended support for Encoding and Collation
42
- - [Promise Wrapper](./documentation/en/Promise-Wrapper.md)
43
- - Compression
44
- - SSL and [Authentication Switch](./documentation/en/Authentication-Switch.md)
45
- - [Custom Streams](./documentation/en/Extras.md)
46
- - [Pooling](#using-connection-pools)
60
+ - Faster / Better Performance
61
+ - [Prepared Statements][docs-prepared-statements]
62
+ - MySQL Binary Log Protocol
63
+ - [MySQL Server][docs-mysql-server]
64
+ - Extended support for Encoding and Collation
65
+ - [Promise Wrapper][docs-promise-wrapper]
66
+ - Compression
67
+ - SSL and [Authentication Switch][docs-authentication-switch]
68
+ - [Custom Streams][docs-streams]
69
+ - [Pooling][docs-qs-pooling]
47
70
 
48
71
  ## Installation
49
72
 
@@ -59,235 +82,27 @@ If you are using TypeScript, you will need to install `@types/node`.
59
82
  npm install --save-dev @types/node
60
83
  ```
61
84
 
62
- > For TypeScript documentation and examples, see [here](./documentation/en/TypeScript-Examples.md).
63
-
64
- ## First Query
65
- ```js
66
- // get the client
67
- const mysql = require('mysql2');
68
-
69
- // create the connection to database
70
- const connection = mysql.createConnection({
71
- host: 'localhost',
72
- user: 'root',
73
- database: 'test'
74
- });
75
-
76
- // simple query
77
- connection.query(
78
- 'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
79
- function(err, results, fields) {
80
- console.log(results); // results contains rows returned by server
81
- console.log(fields); // fields contains extra meta data about results, if available
82
- }
83
- );
84
-
85
- // with placeholder
86
- connection.query(
87
- 'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
88
- ['Page', 45],
89
- function(err, results) {
90
- console.log(results);
91
- }
92
- );
93
- ```
94
-
95
- ## Using Prepared Statements
96
-
97
- 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:
98
-
99
- - [How prepared statements can protect from SQL Injection attacks](http://stackoverflow.com/questions/8263371/how-can-prepared-statements-protect-from-sql-injection-attacks)
100
-
101
- MySQL2 provides `execute` helper which will prepare and query the statement. You can also manually prepare / unprepare statement with `prepare` / `unprepare` methods.
102
-
103
- ```js
104
- // get the client
105
- const mysql = require('mysql2');
106
-
107
- // create the connection to database
108
- const connection = mysql.createConnection({
109
- host: 'localhost',
110
- user: 'root',
111
- database: 'test'
112
- });
113
-
114
- // execute will internally call prepare and query
115
- connection.execute(
116
- 'SELECT * FROM `table` WHERE `name` = ? AND `age` > ?',
117
- ['Rick C-137', 53],
118
- function(err, results, fields) {
119
- console.log(results); // results contains rows returned by server
120
- console.log(fields); // fields contains extra meta data about results, if available
121
-
122
- // If you execute same statement again, it will be picked from a LRU cache
123
- // which will save query preparation time and give better performance
124
- }
125
- );
126
- ```
127
-
128
- ## Using connection pools
129
-
130
- Connection pools help reduce the time spent connecting to the MySQL server by reusing a previous connection, leaving them open instead of closing when you are done with them.
131
-
132
- This improves the latency of queries as you avoid all of the overhead that comes with establishing a new connection.
133
-
134
- ```js
135
- // get the client
136
- const mysql = require('mysql2');
137
-
138
- // Create the connection pool. The pool-specific settings are the defaults
139
- const pool = mysql.createPool({
140
- host: 'localhost',
141
- user: 'root',
142
- database: 'test',
143
- waitForConnections: true,
144
- connectionLimit: 10,
145
- maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit`
146
- idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000
147
- queueLimit: 0,
148
- enableKeepAlive: true,
149
- keepAliveInitialDelay: 0
150
- });
151
- ```
152
- The pool does not create all connections upfront but creates them on demand until the connection limit is reached.
153
-
154
- You can use the pool in the same way as connections (using `pool.query()` and `pool.execute()`):
155
- ```js
156
- // For pool initialization, see above
157
- pool.query("SELECT `field` FROM `table`", function(err, rows, fields) {
158
- // Connection is automatically released when query resolves
159
- });
160
- ```
161
-
162
- Alternatively, there is also the possibility of manually acquiring a connection from the pool and returning it later:
163
- ```js
164
- // For pool initialization, see above
165
- pool.getConnection(function(err, conn) {
166
- // Do something with the connection
167
- conn.query(/* ... */);
168
- // Don't forget to release the connection when finished!
169
- pool.releaseConnection(conn);
170
- });
171
- ```
172
-
173
- ## Using Promise Wrapper
174
-
175
- MySQL2 also support Promise API. Which works very well with ES7 async await.
176
- ```js
177
- async function main() {
178
- // get the client
179
- const mysql = require('mysql2/promise');
180
- // create the connection
181
- const connection = await mysql.createConnection({host:'localhost', user: 'root', database: 'test'});
182
- // query database
183
- const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', ['Morty', 14]);
184
- }
185
- ```
186
-
187
- MySQL2 use default `Promise` object available in scope. But you can choose which `Promise` implementation you want to use.
188
- ```js
189
- // get the client
190
- const mysql = require('mysql2/promise');
191
-
192
- // get the promise implementation, we will use bluebird
193
- const bluebird = require('bluebird');
194
-
195
- // create the connection, specify bluebird as Promise
196
- const connection = await mysql.createConnection({host:'localhost', user: 'root', database: 'test', Promise: bluebird});
197
-
198
- // query database
199
- const [rows, fields] = await connection.execute('SELECT * FROM `table` WHERE `name` = ? AND `age` > ?', ['Morty', 14]);
200
- ```
201
-
202
- MySQL2 also exposes a .promise() function on Pools, so you can create a promise/non-promise connections from the same pool.
203
- ```js
204
- async function main() {
205
- // get the client
206
- const mysql = require('mysql2');
207
- // create the pool
208
- const pool = mysql.createPool({host:'localhost', user: 'root', database: 'test'});
209
- // now get a Promise wrapped instance of that pool
210
- const promisePool = pool.promise();
211
- // query database using promises
212
- const [rows,fields] = await promisePool.query("SELECT 1");
213
- }
214
- ```
215
-
216
- MySQL2 exposes a .promise() function on Connections, to "upgrade" an existing non-promise connection to use promise.
217
- ```js
218
- // get the client
219
- const mysql = require('mysql2');
220
- // create the connection
221
- const con = mysql.createConnection(
222
- {host:'localhost', user: 'root', database: 'test'}
223
- );
224
- con.promise().query("SELECT 1")
225
- .then( ([rows,fields]) => {
226
- console.log(rows);
227
- })
228
- .catch(console.log)
229
- .then( () => con.end());
230
- ```
231
-
232
- ## Array Results
233
-
234
- 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.
235
-
236
- For example: `select 1 as foo, 2 as foo`.
237
-
238
- 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).
239
-
240
- ### Connection Level
241
- ```js
242
- const con = mysql.createConnection(
243
- { host: 'localhost', database: 'test', user: 'root', rowsAsArray: true }
244
- );
245
- ```
246
-
247
- ### Query Level
248
- ```js
249
- con.query({ sql: 'select 1 as foo, 2 as foo', rowsAsArray: true }, function(err, results, fields) {
250
- console.log(results); // in this query, results will be an array of arrays rather than an array of objects
251
- console.log(fields); // fields are unchanged
252
- });
253
- ```
254
-
255
- ## API and Configuration
256
-
257
- MySQL2 is mostly API compatible with [Node MySQL][node-mysql]. You should check their API documentation to see all available API options.
258
-
259
- One known incompatibility is that `DECIMAL` values are returned as strings whereas in [Node MySQL][node-mysql] they are returned as numbers. This includes the result of `SUM()` and `AVG()` functions when applied to `INTEGER` arguments. This is done deliberately to avoid loss of precision - see https://github.com/sidorares/node-mysql2/issues/935.
260
-
261
- If you find any other incompatibility with [Node MySQL][node-mysql], Please report via Issue tracker. We will fix reported incompatibility on priority basis.
85
+ > For TypeScript documentation and examples, see [here][docs-typescript-docs].
262
86
 
263
87
  ## Documentation
264
88
 
265
- You can find more detailed documentation [here](./documentation/en). You should also check various code [examples](./examples) to understand advanced concepts.
89
+ - [Quickstart][docs-base]
90
+ - [First Query][docs-qs-first-query], [Using Prepared Statements][docs-qs-using-prepared-statements], [Using Connection Pools][docs-qs-pooling] and more.
91
+ - [Documentation][docs-documentation]
92
+ - [Examples][docs-examples]
93
+ - [FAQ][docs-faq]
266
94
 
267
95
  ## Acknowledgements
268
96
 
269
- - Internal protocol is written by @sidorares [MySQL-Native](https://github.com/sidorares/nodejs-mysql-native)
270
- - Constants, SQL parameters interpolation, Pooling, `ConnectionConfig` class taken from [node-mysql](https://github.com/mysqljs/mysql)
271
- - SSL upgrade code based on @TooTallNate [code](https://gist.github.com/TooTallNate/848444)
272
- - Secure connection / compressed connection api flags compatible to [MariaSQL](https://github.com/mscdex/node-mariasql/) client.
273
- - [Contributors](https://github.com/sidorares/node-mysql2/graphs/contributors)
97
+ - Internal protocol is written by [@sidorares][sidorares] [MySQL-Native][mysql-native].
98
+ - Constants, SQL parameters interpolation, Pooling, `ConnectionConfig` class taken from [Node MySQL][node-mysql].
99
+ - SSL upgrade code based on [@TooTallNate][TooTallNate] [code][starttls.js].
100
+ - Secure connection / compressed connection api flags compatible to [MariaSQL][node-mariasql] client.
101
+ - [Contributors][contributors].
274
102
 
275
103
  ## Contributing
276
104
 
277
- Want to improve something in `node-mysql2`. Please check [Contributing.md](https://github.com/sidorares/node-mysql2/blob/master/Contributing.md) for detailed instruction on how to get started.
105
+ Want to improve something in **MySQL2**?
106
+ Please check [Contributing.md][contributing] for detailed instruction on how to get started.
278
107
 
279
-
280
- [npm-image]: https://img.shields.io/npm/v/mysql2.svg
281
- [npm-url]: https://npmjs.org/package/mysql2
282
- [node-version-image]: http://img.shields.io/node/v/mysql2.svg
283
- [node-version-url]: http://nodejs.org/download/
284
- [travis-image]: https://img.shields.io/travis/sidorares/node-mysql2/master.svg?label=linux
285
- [travis-url]: https://travis-ci.org/sidorares/node-mysql2
286
- [appveyor-image]: https://img.shields.io/appveyor/ci/sidorares/node-mysql2/master.svg?label=windows
287
- [appveyor-url]: https://ci.appveyor.com/project/sidorares/node-mysql2
288
- [downloads-image]: https://img.shields.io/npm/dm/mysql2.svg
289
- [downloads-url]: https://npmjs.org/package/mysql2
290
- [license-url]: https://github.com/sidorares/node-mysql2/blob/master/License
291
- [license-image]: https://img.shields.io/npm/l/mysql2.svg?maxAge=2592000
292
- [node-mysql]: https://github.com/mysqljs/mysql
293
- [mysql-native]: https://github.com/sidorares/nodejs-mysql-native
108
+ To contribute in **MySQL2 Documentation**, please visit the [Website Contributing Guidelines][docs-contributing] for detailed instruction on how to get started.
@@ -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/en/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://sidorares.github.io/node-mysql2/docs#using-promise-wrapper, or the mysql2 documentation at https://sidorares.github.io/node-mysql2/docs/documentation/promise-wrapper";
41
41
  // eslint-disable-next-line
42
42
  console.log(err);
43
43
  throw new Error(err);
@@ -260,11 +260,11 @@ class ConnectionConfig {
260
260
  static parseUrl(url) {
261
261
  const parsedUrl = new URL(url);
262
262
  const options = {
263
- host: parsedUrl.hostname,
263
+ host: decodeURIComponent(parsedUrl.hostname),
264
264
  port: parseInt(parsedUrl.port, 10),
265
- database: parsedUrl.pathname.slice(1),
266
- user: parsedUrl.username,
267
- password: parsedUrl.password
265
+ database: decodeURIComponent(parsedUrl.pathname.slice(1)),
266
+ user: decodeURIComponent(parsedUrl.username),
267
+ password: decodeURIComponent(parsedUrl.password),
268
268
  };
269
269
  parsedUrl.searchParams.forEach((value, key) => {
270
270
  try {
package/package.json CHANGED
@@ -1,23 +1,20 @@
1
1
  {
2
2
  "name": "mysql2",
3
- "version": "3.6.4",
3
+ "version": "3.7.0",
4
4
  "description": "fast mysql driver. Implements core protocol, prepared statements, ssl and compression in native JS",
5
5
  "main": "index.js",
6
- "directories": {
7
- "example": "examples"
8
- },
9
6
  "typings": "typings/mysql/index",
10
7
  "scripts": {
11
8
  "lint": "npm run lint:docs && npm run lint:code",
12
9
  "lint:code": "eslint index.js promise.js index.d.ts promise.d.ts \"typings/**/*.ts\" \"lib/**/*.js\" \"test/**/*.{js,ts}\" \"benchmarks/**/*.js\"",
13
- "lint:docs": "eslint Contributing.md README.md \"documentation/**/*.md\" \"examples/*.js\"",
10
+ "lint:docs": "eslint Contributing.md README.md",
14
11
  "test": "node ./test/run.js",
15
12
  "test:builtin-node-runner": "NODE_V8_COVERAGE=./coverage node --test --experimental-test-coverage test/builtin-runner",
16
13
  "test:tsc-build": "cd \"test/tsc-build\" && npx tsc -p \"tsconfig.json\"",
17
14
  "coverage-test": "c8 -r cobertura -r lcov -r text node ./test/run.js",
18
15
  "benchmark": "node ./benchmarks/benchmark.js",
19
- "prettier": "prettier --single-quote --trailing-comma none --write \"{lib,examples,test}/**/*.js\"",
20
- "prettier:docs": "prettier --single-quote --trailing-comma none --write README.md documentation/*",
16
+ "prettier": "prettier --single-quote --trailing-comma none --write \"{lib,test}/**/*.js\"",
17
+ "prettier:docs": "prettier --single-quote --trailing-comma none --write README.md",
21
18
  "precommit": "lint-staged",
22
19
  "eslint-check": "eslint --print-config .eslintrc | eslint-config-prettier-check",
23
20
  "wait-port": "wait-on"
@@ -32,6 +29,7 @@
32
29
  "type": "git",
33
30
  "url": "https://github.com/sidorares/node-mysql2"
34
31
  },
32
+ "homepage": "https://sidorares.github.io/node-mysql2/docs",
35
33
  "keywords": [
36
34
  "mysql",
37
35
  "client",