-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connection: Extend with the .pquery() method
Fix up related .pquery() stuff in PoolPlus
- Loading branch information
Showing
8 changed files
with
162 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const Connection = require('mysql/lib/Connection'); | ||
|
||
/** | ||
* @class Connection | ||
* @classdesc The `mysql` module's `Connection` class extended with one extra method. Returned by | ||
* {@link https://github.com/mysqljs/mysql#establishing-connections|`mysql.createConnection()`}, | ||
* and {@link https://github.com/mysqljs/mysql#pooling-connections|`pool.getConnection()`}. | ||
*/ | ||
|
||
/** | ||
* The same as the `query` method except when not passed a callback it returns | ||
* a promise that resolves with the results of the query. | ||
* | ||
* @param {string|Object} sql - An SqlString or options object. | ||
* @param {Array} [values] - Values to replace placeholders in the SqlString. | ||
* @param {module:mysql-plus~queryCallback} [cb] - An optional callback that gets called with | ||
* the results of the query. | ||
* @return {?Promise} If the `cb` parameter is omitted, a promise that will resolve with the results | ||
* of the query is returned. | ||
* @see {@link https://github.com/mysqljs/mysql#performing-queries} | ||
* | ||
* @example | ||
* connection.pquery('SELECT * FROM `books` WHERE `author` = "David"') | ||
* .then((results) => { | ||
* // results will contain the results of the query | ||
* }) | ||
* .catch((error) => { | ||
* // error will be the Error that occurred during the query | ||
* }); | ||
*/ | ||
Connection.prototype.pquery = function pquery(sql, values, cb) { | ||
if (typeof (cb || values || sql) === 'function') { | ||
return this.query(sql, values, cb); | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
this.query(sql, values, (error, results) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(results); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
const config = require('../config'); | ||
const mysql = require('mysql'); | ||
const sinon = require('sinon'); | ||
|
||
describe('Connection', () => { | ||
|
||
const connection = mysql.createConnection(config); | ||
|
||
describe('#pquery()', () => { | ||
|
||
it('should behave like #query() when passed a callback', () => { | ||
const expectedSql = 'SELECT 1 as ??'; | ||
const expectedValues = ['solution']; | ||
function expectedCb() { /* no-op */ } | ||
|
||
sinon.stub(connection, 'query', function(sql, values, cb) { | ||
sql.should.equal(expectedSql); | ||
values.should.equal(expectedValues); | ||
cb.should.equal(expectedCb); | ||
}); | ||
|
||
connection.pquery(expectedSql, expectedValues, expectedCb); | ||
connection.query.should.be.called(); | ||
|
||
connection.query.restore(); | ||
}); | ||
|
||
it('should return a working Promise', done => { | ||
connection.pquery('SELECT "a" as solution') | ||
.then(results => { | ||
results.length.should.equal(1); | ||
results[0].solution.should.equal('a'); | ||
|
||
connection.pquery('SELECT "a" as ??', ['solution']) | ||
.then(results2 => { | ||
results2.length.should.equal(1); | ||
results2[0].solution.should.equal('a'); | ||
|
||
connection.pquery('SELECT a as solution') | ||
.then(() => done(new Error())) | ||
.catch(() => done()); | ||
}) | ||
.catch(done); | ||
}) | ||
.catch(done); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters