You can use it that way with the mysql
(link). The idea is how you show in code, use markers ?
in the query and then pass an array with the positions whose value should be used instead of the marker.
According to the example of the documentation:
connection.query('UPDATE users SET foo=?, bar=?, baz=? WHERE id=?', ['a', 'b', 'c', userId], (error, results, fields) => {
if (error) throw error;
// ...
});
To implement this in mssql you could make your own wrapper:
const superQuery = (conn, query, values) => {
let req = pool.request();
let value = values.shift();
let counter = 1;
while (typeof value !== 'undefined') {
let sep = 'input_parameter' + (counter++);
req = req.input(sep, value);
query = query.replace('?', '@' + sep);
value = values.shift()
}
return req.query(query);
}
And then wear it like this:
superQuery(
connection,
'UPDATE users SET foo=?, bar=?, baz=? WHERE id=?',
['a', 'b', 'c', userId]
).then(res => {
...etc
});
Note: the mssql
has a concept of prototypes as the second optional argument of . input()
. It would be simple to incorporate this into my wrapper so as not to lose this level of security. However the array passed instead of simple would be an object with the prototype as well.
Be careful to change the form of the query, because in the first code "mssql" implements a protection against Sqli. See here: https://github.com/patriksimek/node-mssql#sql-Injection
– Marcelo Junior
@Marcelojunior if you know an alternative to solve this better than mine puts an answer, I don’t know the library so there may be better ways.
– Sergio
Thanks for the tip @Marcelojunior, I hadn’t thought about it....
– Lucas Souza
@Sergio also do not know, I looked for alternatives to help in the answer and found nothing that has already been implemented.
– Marcelo Junior
@Lucassouza For nothing :)
– Marcelo Junior