我正在尝试使用 JavaScript 中的动态“WHERE”条件为 Amazon Athena 准备一些 SQL 查询。查询保存在另一个文件中的一个变量上。如何为其添加自定义 WHERE 条件?
我试图实现的伪代码,
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'
任何人都可以向我建议一种正确的方法来做到这一点。我目前的解决方案发布在下面。
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'