Newer
Older
# mysql-promise
Small promises wrapper for [`mysql2`](https://github.com/sidorares/node-mysql2),
it's forked and compatible with [`mysql-promise`](https://github.com/martinj/node-mysql-promise).
[](http://travis-ci.org/namshi/node-mysql2-promise)
[](https://www.npmjs.com/package/mysql2-promise)
[](https://www.npmjs.com/package/mysql2-promise)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
## Installation
This module is installed via npm:
``` bash
$ npm install mysql2-promise --save
```
## Example Usage of query
``` js
var db = require('mysql2-promise')();
db.configure({
"host": "localhost",
"user": "foo",
"password": "bar",
"database": "db"
});
db.query('UPDATE foo SET key = ?', ['value']).then(function () {
return db.query('SELECT * FROM foo');
}).spread(function (rows) {
console.log('Look at all the foo', rows);
});
//using multiple databases, giving it a name 'second-db' so it can be retrieved inside other modules/files.
var db2 = require('mysql-promise')('second-db');
db2.configure({
"host": "localhost",
"user": "foo",
"password": "bar",
"database": "another-db"
});
db2.query('SELECT * FROM users').spread(function (users) {
console.log('Hello users', users);
});
```
## Example Usage of execute
`execute()` function is similar to `query` but it use [prepared-statements](https://github.com/sidorares/node-mysql2#prepared-statements).
``` js
var db = require('mysql2-promise')();
db.configure({
"host": "localhost",
"user": "foo",
"password": "bar",
"database": "db"
});
db.execute('SELECT * FROM users WHERE LIMIT = ?', [10]).spread(function (users) {
console.log('Hello users', users);
});
```
## Example usage of [namedPlaceholders]((https://github.com/sidorares/node-mysql2#named-placeholders))
``` js
var db = require('mysql2-promise')();
db.configure({
"host": "localhost",
"user": "foo",
"password": "bar",
"database": "db"
});
db.pool.on('connection', function (poolConnection) {
db.execute('SELECT * FROM users WHERE LIMIT = :limit', {limit: 10}).spread(function (users) {
console.log('Hello users', users);
});
```
## Credits
This library is forked from [`mysql-promise`](https://github.com/martinj/node-mysql-promise)