question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When Recieving Key-Value pair, Where the Value is Array. Use IN Instead of =

See original GitHub issue

Feature 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
DanielBailey-webcommented, Sep 15, 2021

In case anyone finds this and wants the issue in the correct repo: https://github.com/mysqljs/sqlstring/issues/66

1reaction
dougwilsoncommented, Sep 14, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found