Skip to content

Instantly share code, notes, and snippets.

@ryanguill
Last active May 22, 2016 18:50
Show Gist options
  • Save ryanguill/8c5332278276c3a767b5 to your computer and use it in GitHub Desktop.
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.
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