-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Request] Let client.query support passing values with an object #1560
Copy link
Copy link
Closed
Labels
Description
Something like:
client.query(`
SELECT id, name
FROM table1
WHERE gender={gender} AND age>{age}
`, { gender: 'M', age: 20});
I wrote a function to do the translation:
const replaceEngine = (() => {
return (template, data) => {
let index = 0;
const values = [];
return {
text: template.replace(/\{(\w+)\}/g, (match, key) => {
index++;
values.push(data && data.hasOwnProperty(key) ? data[key] : null);
return `$${index}`;
}),
values,
};
};
})();
======
replaceEngine(`
SELECT id, name
FROM table1
WHERE gender={gender} AND age>{age}
`, { gender: 'M', age: 20})
you will get:
{
text: 'SELECT id, name FROM table1 WHERE gender=$1 AND age>$2',
values: ['M', 20]
}
Reactions are currently unavailable