Home > Enterprise >  How to use IN prepared statement with single variable in typeorm
How to use IN prepared statement with single variable in typeorm

Time:01-30

I have a .sql file containing a query such as

SELECT * WHERE id IN ($1)

The sql query is read and passed into a typeorm query with an array of parameters.

const result = await this.entityManager.query(myQuery, parameters);

I would like to have parameters be an array with a single value that would be a string concatenation of all the ids I want to find. This would allow me to use my sql file no matter the number of ids I want to filter by.

I have tried formatting parameters in such a way that I can have $1 only no matter the number of items in my array:

const ids = ['1', '2', '3'];

const parameters = [ids.join("', '")];

or

const parameters = ["'"   ids.join("', '")   "'"];

etc

I have not found a syntax that works.

Is this feasible somehow ?

CodePudding user response:

I have replaced the IN ($1) by = ANY($1) and am now passing the ids as an array. This works as I intend.

SELECT * WHERE id = ANY($1);
const ids = ['1', '2', '3'];
const result = await this.entityManager.query(myQuery, [ids]);
  • Related