From 234fbbe5b8e37228ae9d9f98d8c61b584ca7964d Mon Sep 17 00:00:00 2001 From: Benoit Essiambre Date: Thu, 12 Dec 2019 20:59:55 +0000 Subject: [PATCH 1/2] template literal for use with callbacks --- README.md | 8 ++++++++ lib/base/request.js | 12 ++++++++++++ package-lock.json | 5 +++++ package.json | 1 + test/common/unit.js | 7 +++++++ 5 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 45e3dbbc..52aee2ed 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,14 @@ sql.connect(config, err => { console.dir(result) }) + + // Using template literal + + const request = new sql.Request() + request.query(request.template`select * from mytable where id = ${value}`, (err, result) => { + // ... error checks + console.dir(result) + }) }) sql.on('error', err => { diff --git a/lib/base/request.js b/lib/base/request.js index ff524222..05d75828 100644 --- a/lib/base/request.js +++ b/lib/base/request.js @@ -40,6 +40,18 @@ class Request extends EventEmitter { this.parameters = {} } + /** + * Generate sql string and set imput parameters from tagged template string. + * + * @param {Template literal} template + * @return {String} + */ + template () { + const values = Array.prototype.slice.call(arguments) + const strings = values.shift() + return this._template(strings, values) + } + /** * Fetch request from tagged template string. * diff --git a/package-lock.json b/package-lock.json index 773a5ce2..31d414bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1594,6 +1594,11 @@ "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==", "dev": true }, + "msnodesqlv8": { + "version": "0.8.6", + "resolved": "https://registry.npmjs.org/msnodesqlv8/-/msnodesqlv8-0.8.6.tgz", + "integrity": "sha512-zfgh5Y2wmot3+fj+K+5vDjszI8ZTvjwu7H2dNiTwxRipPF166F0SdS6worQCHOVo1clZh9dKaBTolX7VQm/iNQ==" + }, "mute-stream": { "version": "0.0.7", "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.7.tgz", diff --git a/package.json b/package.json index f67e81f7..7d7e0075 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "license": "MIT", "dependencies": { "debug": "^4", + "msnodesqlv8": "^0.8.6", "tarn": "^1.1.5", "tedious": "^6.6.2" }, diff --git a/test/common/unit.js b/test/common/unit.js index 8df698a6..c4dd0e10 100644 --- a/test/common/unit.js +++ b/test/common/unit.js @@ -273,4 +273,11 @@ describe('Unit', () => { command: 'select * from myTable where id in (@param1_0, @param1_1, @param1_2)' }) }) + + it('tagged template literal request', () => { + const req = new sql.Request() + const sqlstr = req.template`select * from myTable where id = ${123}` + assert.strictEqual(sqlstr, 'select * from myTable where id = @param1') + assert.strictEqual(req.parameters.param1.value, 123) + }) }) From 1d3d45334f04336066c3f15793c625d8232bfc3e Mon Sep 17 00:00:00 2001 From: Benoit Essiambre Date: Thu, 12 Dec 2019 21:07:29 +0000 Subject: [PATCH 2/2] reverted npm files --- package-lock.json | 5 ----- package.json | 1 - 2 files changed, 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 31d414bb..773a5ce2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1594,11 +1594,6 @@ "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==", "dev": true }, - "msnodesqlv8": { - "version": "0.8.6", - "resolved": "https://registry.npmjs.org/msnodesqlv8/-/msnodesqlv8-0.8.6.tgz", - "integrity": "sha512-zfgh5Y2wmot3+fj+K+5vDjszI8ZTvjwu7H2dNiTwxRipPF166F0SdS6worQCHOVo1clZh9dKaBTolX7VQm/iNQ==" - }, "mute-stream": { "version": "0.0.7", "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.7.tgz", diff --git a/package.json b/package.json index 7d7e0075..f67e81f7 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "license": "MIT", "dependencies": { "debug": "^4", - "msnodesqlv8": "^0.8.6", "tarn": "^1.1.5", "tedious": "^6.6.2" },