From 38c5847678435a80b64213c0b049e036f0a4aab5 Mon Sep 17 00:00:00 2001 From: Ferriel Melarpis Date: Fri, 21 Jul 2017 18:14:34 +0800 Subject: [PATCH] Added support for squel to build query for promise --- .gitignore | 5 +++++ Makefile | 4 ++-- README.md | 13 +++++++++++++ lib/CustomMySQL.js | 14 +++++++++++++- package.json | 10 ++++++---- src/CustomMySQL.js | 14 +++++++++++++- test/test.js | 24 +++++++++++++++++++++++- 7 files changed, 75 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index d4cd470..eff2358 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,8 @@ node_modules trash sblm.sublime-workspace + +.idea + +package-lock.json +yarn.lock diff --git a/Makefile b/Makefile index e296367..02bee1f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -mocha_option := --recursive -t 5000 -s 100 +mocha_option := --recursive -t 5000 -s 100 --compilers js:babel-core/register $(mocha_opt) test: ifeq ($(TRAVIS),1) @NODE_ENV=test ./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- $(mocha_option) && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js @@ -6,4 +6,4 @@ else @NODE_ENV=test ./node_modules/.bin/mocha -R spec $(mocha_option) endif -.PHONY: test \ No newline at end of file +.PHONY: test diff --git a/README.md b/README.md index 0da0cee..d8a62ea 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,19 @@ mysql.squel(query, callback) .end(); ``` +### Building a query using [squel](http://hiddentao.com/squel) for using promise +```javascript +const query = squel.select() + .field('name') + .from('users') + .where('name = ?', 'name'); + +mysql.build(query) + .promise() + .then() + .catch(); +``` + **Note:** Single connections will only be created on `mysql.query(...)` while pooled connections will be created on `mysql.use('db1', config.DB, true)`. ### Doing multiple queries using 1 connection diff --git a/lib/CustomMySQL.js b/lib/CustomMySQL.js index 775844d..2cf276d 100644 --- a/lib/CustomMySQL.js +++ b/lib/CustomMySQL.js @@ -145,7 +145,19 @@ var CustomMySQL = function () { }, { key: 'build', value: function build() { - this._cache = Array.from(arguments); + + var args = Array.from(arguments); + + if (args.length === 1 && typeof args[0].toParam === 'function') { + + var query = args[0].toParam(); + + this._cache = [query.text, query.values]; + + return this; + } + + this._cache = args; return this; } }, { diff --git a/package.json b/package.json index 000df54..bdae32f 100644 --- a/package.json +++ b/package.json @@ -7,20 +7,22 @@ "mysql": "^2.11.1" }, "devDependencies": { - "babel": "^6.1.18", + "babel": "^6.23.0", + "babel-cli": "^6.24.1", "babel-preset-es2015": "^6.1.18", - "istanbul": "^0.4.0", "chai": "^3.4.1", "coveralls": "^2.11.4", "esdoc": "^0.4.3", + "istanbul": "^0.4.0", "mocha": "^2.3.3", "mocha-lcov-reporter": "^1.0.0", "squel": "^5.6.0" }, "scripts": { + "build": "babel src --out-dir lib", "test": "make test", - "coverage": "export NODE_ENV=test && istanbul cover _mocha -- --recursive -R spec -t 5000 -s 100", - "test-dev": "export NODE_ENV=test && mocha --watch --recursive -R spec -t 5000 -s 100" + "test-dev": "make test mocha_opt=--watch", + "coverage": "export NODE_ENV=test && istanbul cover _mocha -- --recursive -R spec -t 5000 -s 100" }, "repository": { "type": "git", diff --git a/src/CustomMySQL.js b/src/CustomMySQL.js index c137f14..f98369e 100644 --- a/src/CustomMySQL.js +++ b/src/CustomMySQL.js @@ -115,7 +115,19 @@ export default class CustomMySQL { } build () { - this._cache = Array.from(arguments); + + const args = Array.from(arguments); + + if (args.length === 1 && typeof args[0].toParam === 'function') { + + const query = args[0].toParam(); + + this._cache = [query.text, query.values]; + + return this; + } + + this._cache = args; return this; } diff --git a/test/test.js b/test/test.js index f984e6b..26c2412 100644 --- a/test/test.js +++ b/test/test.js @@ -541,6 +541,28 @@ describe('Overall test', () => { + it ('mysql.build should support squel query', (done) => { + const mysql = new CustomMySQL(); + const key = 'key'; + + const query = squel.select().function('1'); + + mysql.set_logger(noop_logger) + .add(key, FREE_DB) + .build(query) + .promise() + .then(result => { + result.length.should.equal(1); + done(); + }) + .catch(err => { + should.equal(err, null); + done(); + }); + }); + + + it ('mysql.query should accept query placeholder values', (done) => { const mysql = new CustomMySQL(); const value = 'hi'; @@ -683,7 +705,7 @@ describe('Overall test', () => { }); - + it ('mysql.query should return the mysql object', (done) => { const mysql = new CustomMySQL(); const key = 'key';