Skip to content

Commit

Permalink
Added support for squel to build query for promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferriel Melarpis committed Jul 21, 2017
1 parent fbff2b5 commit 38c5847
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ node_modules

trash
sblm.sublime-workspace

.idea

package-lock.json
yarn.lock
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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
else
@NODE_ENV=test ./node_modules/.bin/mocha -R spec $(mocha_option)
endif

.PHONY: test
.PHONY: test
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion lib/CustomMySQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}, {
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 13 additions & 1 deletion src/CustomMySQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 23 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -683,7 +705,7 @@ describe('Overall test', () => {
});



it ('mysql.query should return the mysql object', (done) => {
const mysql = new CustomMySQL();
const key = 'key';
Expand Down

0 comments on commit 38c5847

Please sign in to comment.