I'm trying to use the sql template strings npm package to use template literals in my SQL queries securely, but I keep getting an error that states that there is a problem with the sql syntax. When I omit the "SQL" bit, everything works. I've installed the sql-template-strings package. What am I missing? Help appreciated!
// Import MySQL connection.
const connection = require("../config/connection.js");
let SQL = require('sql-template-strings');
//SQL queries
let orm = {
all: (table, cb) => {
var queryString = SQL`SELECT * FROM ${table}`;
connection.query(queryString, (err, result) => {
if(err) throw err;
return cb(result);
})
}
};
module.exports = orm;
Error I get: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''reading'' at line 1
Reading is the name of the table.
Console.logging queryString renders:
$ SQLStatement { strings: [ 'SELECT * FROM ', '' ], values: [ 'reading' ] }
And the log from omitting the SQL keyword:
$ SELECT * FROM reading
I read through all the documentation here.