Skip to content

Instantly share code, notes, and snippets.

@charmander
Created March 28, 2017 16:53
Show Gist options
  • Save charmander/8f7b05f8d2b2e4e3c190c7a9e1bec1f2 to your computer and use it in GitHub Desktop.
Save charmander/8f7b05f8d2b2e4e3c190c7a9e1bec1f2 to your computer and use it in GitHub Desktop.
class Query {
constructor(text, values) {
this.text = text;
this.values = values;
}
}
const sql = (parts, ...values) => {
let text = parts[0];
for (let i = 1; i < parts.length; i++) {
text += '$' + i + parts[i];
}
return new Query(text, values);
};
const query = q => {
if (!(q instanceof Query)) {
throw new TypeError('query must be a Query object');
}
return db.query(q);
};
query(sql`INSERT INTO t (c) VALUES (${'safe'})`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment