Skip to content

Commit 74b6050

Browse files
committed
extra doc cleanup
1 parent a867233 commit 74b6050

File tree

4 files changed

+207
-196
lines changed

4 files changed

+207
-196
lines changed

README.md

Lines changed: 17 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -5,214 +5,34 @@
55
[![Node.js Version][node-version-image]][node-version-url]
66
[![Linux Build][travis-image]][travis-url]
77
[![Windows Build][appveyor-image]][appveyor-url]
8+
[![License][license-link]][license-url]
89

910
MySql client for node.js. Written in native JavaScript and aims to be mostly api compatible with [node-mysql](https://github.com/felixge/node-mysql)
1011

1112
[![NPM](https://nodei.co/npm/mysql2.png?downloads=true&stars=true)](https://nodei.co/npm/mysql2/)
1213
[![NPM](https://nodei.co/npm-dl/mysql2.png?months=6)](https://nodei.co/npm/mysql2/)
1314

14-
## Features
15-
16-
In addition to client-side query/escape and connection pooling
15+
## Features and Documentation
1716

1817
- [Fast](https://gist.github.com/sidorares/ffe9ee9c423f763e3b6b)
19-
- MySQL server API for proxies and mocks
20-
- SSL and compression
21-
- Prepared statements
22-
- Binary log protocol client
23-
24-
## Documentation
25-
26-
27-
## Known incompatibilities with node-mysql
28-
29-
In contrast to node-mysql, `zeroFill` flag is ignored in type conversion.
30-
You need to check corresponding field zeroFill flag and convert to string manually if this is of importance to you.
31-
32-
DECIMAL and NEWDECIMAL types always returned as string unless you pass this config option:
33-
```javascript
34-
{
35-
decimalNumbers: true
36-
}
37-
```
38-
**_Warning this option could lose precision on the number as Javascript Number is a Float!_**
39-
40-
## Examples
41-
42-
Simple select:
43-
44-
```js
45-
var mysql = require('mysql2');
46-
var connection = mysql.createConnection({ user: 'test', database: 'test'});
47-
48-
connection.query('SELECT 1+1 as test1', function(err, rows) {
49-
//
50-
});
51-
```
52-
53-
Prepared statement and parameters:
54-
55-
```js
56-
var mysql = require('mysql2');
57-
var connection = mysql.createConnection({ user: 'test', database: 'test'});
58-
59-
connection.execute('SELECT 1+? as test1', [10], function(err, rows) {
60-
//
61-
});
62-
```
63-
64-
Connecting over encrypted connection:
65-
66-
```js
67-
var fs = require('fs');
68-
var mysql = require('mysql2');
69-
var connection = mysql.createConnection({
70-
user: 'test',
71-
database: 'test',
72-
ssl: {
73-
key: fs.readFileSync('./certs/client-key.pem'),
74-
cert: fs.readFileSync('./certs/client-cert.pem')
75-
}
76-
});
77-
connection.query('SELECT 1+1 as test1', console.log);
78-
```
79-
80-
You can use 'Amazon RDS' string as value to ssl property to connect to Amazon RDS mysql over ssl (in that case http://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA cert is used)
81-
82-
```js
83-
var mysql = require('mysql2');
84-
var connection = mysql.createConnection({
85-
user: 'foo',
86-
password: 'bar',
87-
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
88-
ssl: 'Amazon RDS'
89-
});
90-
91-
conn.query('show status like \'Ssl_cipher\'', function(err, res) {
92-
console.log(err, res);
93-
conn.end();
94-
});
95-
```
96-
97-
98-
Simple mysql proxy server:
99-
100-
```js
101-
var mysql = require('mysql2');
102-
103-
var server = mysql.createServer();
104-
server.listen(3307);
105-
server.on('connection', function(conn) {
106-
console.log('connection');
18+
- Prepared Statements
19+
- Promise Wrapper
20+
- Authentication Switch
21+
- MySQL Server (API and Mocks)
22+
- Pooling
23+
- SSL
24+
- MySQL Compression
25+
- Binary Log Protocol Client
10726

108-
conn.serverHandshake({
109-
protocolVersion: 10,
110-
serverVersion: 'node.js rocks',
111-
connectionId: 1234,
112-
statusFlags: 2,
113-
characterSet: 8,
114-
capabilityFlags: 0xffffff
115-
});
116-
117-
conn.on('field_list', function(table, fields) {
118-
console.log('field list:', table, fields);
119-
conn.writeEof();
120-
});
121-
122-
var remote = mysql.createConnection({user: 'root', database: 'dbname', host:'server.example.com', password: 'secret'});
123-
124-
conn.on('query', function(sql) {
125-
console.log('proxying query:' + sql);
126-
remote.query(sql, function(err) { // overloaded args, either (err, result :object)
127-
// or (err, rows :array, columns :array)
128-
if (Array.isArray(arguments[1])) {
129-
// response to a 'select', 'show' or similar
130-
var rows = arguments[1], columns = arguments[2];
131-
console.log('rows', rows);
132-
console.log('columns', columns);
133-
conn.writeTextResult(rows, columns);
134-
} else {
135-
// response to an 'insert', 'update' or 'delete'
136-
var result = arguments[1];
137-
console.log('result', result);
138-
conn.writeOk(result);
139-
}
140-
});
141-
});
142-
143-
conn.on('end', remote.end.bind(remote));
144-
});
145-
```
146-
## MySQL Server API
147-
148-
### Server
149-
150-
* **createServer()** - creates server instance
151-
* **Server.listen** - listen port / unix socket (same arguments as [net.Server.listen](http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback))
152-
153-
events:
154-
155-
* **connect** - new incoming connection.
156-
157-
### Connection
158-
159-
* **serverHandshake({serverVersion, protocolVersion, connectionId, statusFlags, characterSet, capabilityFlags})** - send server handshake initialisation packet, wait handshake response and start listening for commands
160-
* **writeOk({affectedRows: num, insertId: num})** - send [OK packet](http://dev.mysql.com/doc/internals/en/overview.html#packet-OK_Packet) to client
161-
* **writeEof(warnings, statusFlags)** - send EOF packet
162-
* **writeTextResult(rows, fields)** - write query result to client. Rows and fields are in the same format as in `connection.query` callback.
163-
* **writeColumns(fields)** - write fields + EOF packets.
164-
* **writeTextRow(row)** - write array (not hash!) of values as result row
165-
* TODO: binary protocol
166-
167-
events:
168-
169-
Every command packet received by the server will be emitted as a **packet** event with the parameters:
170-
171-
* packet: Packet - The packet itself
172-
* knownCommand: boolean - is this command known to the server
173-
* commandCode: number - the parsed command code (first byte)
174-
175-
In addition special events are emitted for [commands](https://dev.mysql.com/doc/internals/en/text-protocol.html) received from the client. If no listener is present a fallback behavior will be invoked.
176-
177-
* **quit**() - Default: close the connection
178-
* **init_db**(schemaName: string) - Default: return OK
179-
* **query**(sql: string) - Please attach a listener to this. Default: return HA_ERR_INTERNAL_ERROR
180-
* **field_list**(table: string, fields: string) - Default: return ER_WARN_DEPRECATED_SYNTAX
181-
* **ping**() - Default: return OK
182-
183-
## License
184-
185-
MIT
27+
Please check [documentation](https://github.com/sidorares/node-mysql2/tree/master/documentation) to get started.
18628

18729
## Acknowledgements
18830

189-
- Internal protocol is written from scratch using my experience with [mysql-native](https://github.com/sidorares/nodejs-mysql-native)
190-
- constants, sql parameters interpolation, pool, connection config class taken from [node-mysql](https://github.com/felixge/node-mysql) (I tried to preserve git history)
31+
- Internal protocol is written by @sidorares [MySQL-native](https://github.com/sidorares/nodejs-mysql-native)
32+
- Constants, SQL parameters interpolation, Pooling, `ConnectionConfig` class taken from [node-mysql](https://github.com/felixge/node-mysql)
19133
- SSL upgrade code based on @TooTallNate [code](https://gist.github.com/TooTallNate/848444)
192-
- Secure connection / compressed connection api flags compatible to [mariasql](https://github.com/mscdex/node-mariasql/) client.
193-
- [contributors](https://github.com/sidorares/node-mysql2/graphs/contributors)
194-
195-
## Benchmarks
196-
- https://gist.github.com/sidorares/ffe9ee9c423f763e3b6b
197-
- `npm run benchmarks`
198-
- [node-mysql-benchmarks](https://github.com/mscdex/node-mysql-benchmarks)
199-
- try to run example [benchmarks](https://github.com/sidorares/node-mysql2/tree/master/benchmarks) on your system
200-
201-
## Examples using MySQL server API:
202-
203-
- [Mysql-pg-proxy](https://github.com/sidorares/mysql-pg-proxy) - mysql to postgres proxy server.
204-
- [Mysqlite.js](https://github.com/sidorares/mysqlite.js) - mysql server with JS-only (emscripten compiled) sqlite backend.
205-
- [sql-engine](https://github.com/eugeneware/sql-engine) - mysql server with LevelDB backend.
206-
- [mysql-osquery-proxy](https://github.com/sidorares/mysql-osquery-proxy) - connect to [facebook osquery](https://osquery.io/) using MySQL client
207-
- [PlyQL](https://github.com/implydata/plyql) - connect to [Druid](http://druid.io/) using MySQL client
208-
209-
## See also:
210-
211-
- [wire protocol documentation](http://dev.mysql.com/doc/internals/en/client-server-protocol.html)
212-
- [node-mysql](https://github.com/felixge/node-mysql) - most popular node.js mysql client library
213-
- [node-mariasql](https://github.com/mscdex/node-mariasql/) - bindings to libmariasql. One of the fastest clients
214-
- [node-libmysqlclident](https://github.com/Sannis/node-mysql-libmysqlclient) - bindings to libmysqlclient
215-
- [go-mysql](https://github.com/siddontang/go-mysql) - Mysql Go client (prepared statements, binlog protocol, server)
34+
- Secure connection / compressed connection api flags compatible to [MariaSQL](https://github.com/mscdex/node-mariasql/) client.
35+
- [Contributors](https://github.com/sidorares/node-mysql2/graphs/contributors)
21636

21737
## Contributing
21838

@@ -229,3 +49,5 @@ Want to improve something in `node-mysql2`. Please check [Contributing.md](https
22949
[appveyor-url]: https://ci.appveyor.com/project/sidorares/node-mysql2
23050
[downloads-image]: https://img.shields.io/npm/dm/mysql2.svg
23151
[downloads-url]: https://npmjs.org/package/mysql2
52+
[license-link]: https://github.com/sidorares/node-mysql2/blob/master/License
53+
[license-image]: https://img.shields.io/npm/l/mysql2.svg?maxAge=2592000

documentation/Examples.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Examples
2+
3+
## Simple SELECT
4+
5+
```js
6+
var mysql = require('mysql2');
7+
var connection = mysql.createConnection({ user: 'test', database: 'test'});
8+
9+
connection.query('SELECT 1+1 as test1', function(err, rows) {
10+
//
11+
});
12+
```
13+
14+
## Prepared statement and parameters
15+
16+
```js
17+
var mysql = require('mysql2');
18+
var connection = mysql.createConnection({ user: 'test', database: 'test'});
19+
20+
connection.execute('SELECT 1+? as test1', [10], function(err, rows) {
21+
//
22+
});
23+
```
24+
25+
## Connecting over encrypted connection
26+
27+
```js
28+
var fs = require('fs');
29+
var mysql = require('mysql2');
30+
var connection = mysql.createConnection({
31+
user: 'test',
32+
database: 'test',
33+
ssl: {
34+
key: fs.readFileSync('./certs/client-key.pem'),
35+
cert: fs.readFileSync('./certs/client-cert.pem')
36+
}
37+
});
38+
connection.query('SELECT 1+1 as test1', console.log);
39+
```
40+
41+
You can use 'Amazon RDS' string as value to ssl property to connect to Amazon RDS mysql over ssl (in that case http://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA cert is used)
42+
43+
```js
44+
var mysql = require('mysql2');
45+
var connection = mysql.createConnection({
46+
user: 'foo',
47+
password: 'bar',
48+
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
49+
ssl: 'Amazon RDS'
50+
});
51+
52+
conn.query('show status like \'Ssl_cipher\'', function(err, res) {
53+
console.log(err, res);
54+
conn.end();
55+
});
56+
```
57+
58+
59+
## Simple MySQL proxy server
60+
61+
```js
62+
var mysql = require('mysql2');
63+
64+
var server = mysql.createServer();
65+
server.listen(3307);
66+
server.on('connection', function(conn) {
67+
console.log('connection');
68+
69+
conn.serverHandshake({
70+
protocolVersion: 10,
71+
serverVersion: 'node.js rocks',
72+
connectionId: 1234,
73+
statusFlags: 2,
74+
characterSet: 8,
75+
capabilityFlags: 0xffffff
76+
});
77+
78+
conn.on('field_list', function(table, fields) {
79+
console.log('field list:', table, fields);
80+
conn.writeEof();
81+
});
82+
83+
var remote = mysql.createConnection({user: 'root', database: 'dbname', host:'server.example.com', password: 'secret'});
84+
85+
conn.on('query', function(sql) {
86+
console.log('proxying query:' + sql);
87+
remote.query(sql, function(err) { // overloaded args, either (err, result :object)
88+
// or (err, rows :array, columns :array)
89+
if (Array.isArray(arguments[1])) {
90+
// response to a 'select', 'show' or similar
91+
var rows = arguments[1], columns = arguments[2];
92+
console.log('rows', rows);
93+
console.log('columns', columns);
94+
conn.writeTextResult(rows, columns);
95+
} else {
96+
// response to an 'insert', 'update' or 'delete'
97+
var result = arguments[1];
98+
console.log('result', result);
99+
conn.writeOk(result);
100+
}
101+
});
102+
});
103+
104+
conn.on('end', remote.end.bind(remote));
105+
});
106+
```
107+
108+
## Examples using MySQL server API
109+
110+
- [MySQL-pg-proxy](https://github.com/sidorares/mysql-pg-proxy) - MySQL to Postgres proxy server.
111+
- [MySQLite.js](https://github.com/sidorares/mysqlite.js) - MySQL server with JS-only (emscripten compiled) sqlite backend.
112+
- [SQL-engine](https://github.com/eugeneware/sql-engine) - MySQL server with LevelDB backend.
113+
- [MySQL-osquery-proxy](https://github.com/sidorares/mysql-osquery-proxy) - Connect to [facebook osquery](https://osquery.io/) using MySQL client
114+
- [PlyQL](https://github.com/implydata/plyql) - Connect to [Druid](http://druid.io/) using MySQL client

documentation/MySQL-Server.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# MySQL Server API
2+
3+
## Server
4+
5+
* **createServer()** - creates server instance
6+
* **Server.listen** - listen port / unix socket (same arguments as [net.Server.listen](http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback))
7+
8+
#### Events
9+
10+
* **connect** - new incoming connection.
11+
12+
## Connection
13+
14+
* **serverHandshake({serverVersion, protocolVersion, connectionId, statusFlags, characterSet, capabilityFlags})** - send server handshake initialisation packet, wait handshake response and start listening for commands
15+
* **writeOk({affectedRows: num, insertId: num})** - send [OK packet](http://dev.mysql.com/doc/internals/en/overview.html#packet-OK_Packet) to client
16+
* **writeEof(warnings, statusFlags)** - send EOF packet
17+
* **writeTextResult(rows, fields)** - write query result to client. Rows and fields are in the same format as in `connection.query` callback.
18+
* **writeColumns(fields)** - write fields + EOF packets.
19+
* **writeTextRow(row)** - write array (not hash!) of values as result row
20+
* TODO: binary protocol
21+
22+
#### Events
23+
24+
Every command packet received by the server will be emitted as a **packet** event with the parameters:
25+
26+
* packet: Packet - The packet itself
27+
* knownCommand: boolean - is this command known to the server
28+
* commandCode: number - the parsed command code (first byte)
29+
30+
In addition special events are emitted for [commands](https://dev.mysql.com/doc/internals/en/text-protocol.html) received from the client. If no listener is present a fallback behavior will be invoked.
31+
32+
* **quit**() - Default: close the connection
33+
* **init_db**(schemaName: string) - Default: return OK
34+
* **query**(sql: string) - Please attach a listener to this. Default: return HA_ERR_INTERNAL_ERROR
35+
* **field_list**(table: string, fields: string) - Default: return ER_WARN_DEPRECATED_SYNTAX
36+
* **ping**() - Default: return OK

0 commit comments

Comments
 (0)