I am trying to prepare some SQL query for Amazon Athena with dynamic 'WHERE' conditions in JavaScript. The query was saved on one variable in another file. How to add custom WHERE condition to it?
Pseudocode that I am trying to achieve,
file1.js
module.exports.queryFromFile1 = 'SELECT * Name FROM Contact WHERE Name = ?';
file2.js
const {queryFromFile1} = require('./file1.js');
const newQuery = queryFromFile1,['Jane'];
console.log(newQuery); // 'SELECT * Name FROM Contact WHERE Name = 'Jane'
Can anyone suggest to me a proper method to do it. my current solution is posted below.
file1.js
module.exports.queryFromFile1 = 'SELECT * Name FROM Contact WHERE Name = {stringToReplace}';
file2.js
const {queryFromFile1} = require('./file1.js');
const newQuery = queryFromFile1.replace("{stringToReplace}", "'Jane");
console.log(newQuery); // 'SELECT * Name FROM Contact WHERE Name = 'Jane'
db.execute(queryFromFile1, ['Jane'])SELECT * FROM Contact?