Last active
May 22, 2016 18:50
-
-
Save ryanguill/8c5332278276c3a767b5 to your computer and use it in GitHub Desktop.
with node v4 this becomes possible without any external library to do the es6 stuff.
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
function param (name, type) { | |
return {name: name, type: type}; | |
} | |
function sql (template) { | |
//this line goes away entirely with the ...spread operator, the function becomes `sql (template, ...values)` | |
var values = Array.prototype.slice.call(arguments, 1); | |
//console.log("template", template); | |
//console.log("values", values); | |
return { | |
sql: template.slice(1).reduce((acc, part, i) => { | |
return acc + '@' + values[i].name + part | |
}, template[0]), | |
params: values | |
}; | |
} | |
function run () { | |
var output = sql` | |
SELECT | |
* | |
FROM | |
foo | |
WHERE | |
x = ${param('x', 'VarChar')} | |
OR | |
y = ${param('y', 'Int')}; | |
`; | |
console.log(output.sql); | |
console.log(output.params); | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment