When Recieving Key-Value pair, Where the Value is Array. Use IN Instead of =
See original GitHub issueFeature Request:
It would be nice if this same query could be mapped to either =
or IN
depending on the type of input (array vs string/number)
SELECT * FROM users WHERE ?;
const userIds = [1, 2, 3, 4]; // or const userIds = 1;
const users = (await con.query(sql, [{userId: userIds}]))[0]
This currently maps to the following string
SELECT * FROM users WHERE `userId` = 1, 2, 3, 4;
Which works fine for the single value example. But it would be nice if the value was an array it could map to:
SELECT * FROM users WHERE `userId` in (1, 2, 3, 4);
This seems like it wouldn’t be too hard, but I am unaware of any security vulnerabilities such a change could make.
I just want to cut down on the amount of functions I need to have to call specific tables.
For the first query I could obviously call it with a different key and it would still work such as
const emails = "email@example.com";
const users = (await con.query(sql, [{email: emails}]))[0]
But to my knowledge you cannot currently dynamically change the search param when using IN
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9 (1 by maintainers)
Top Results From Across the Web
How to get the value from an array which has key/value pair ...
ES6 has the find-function for arrays: var val = options.find(function(o){ return o.key==="select" }).value;. And maybe wrap it in a function ...
Read more >JavaScript: Obtain key-value pairs - Flexiple
Method 1: Using an object to store key => value pairs. In this method we store the elements from the “keys” array &...
Read more >Convert an object into an array of values, keys, or ... - YouTube
Turn any object into an array using #JavaScript's built-in functions. Use Object.values to get an array of values in an object. Use Object....
Read more >key - Manual - PHP
Return Values ¶. The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer....
Read more >Object.keys, values, entries - The Modern JavaScript Tutorial
Use Object.entries(obj) to get an array of key/value pairs from obj . Use array methods on that array, e.g. map , to transform...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In case anyone finds this and wants the issue in the correct repo: https://github.com/mysqljs/sqlstring/issues/66
You can use in with single values. Need to have one query for = and one for in; just have one with in and it will work for both use cases.