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/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) + }) })