Home > front end >  translate the postgres ANY() operator into Node js using Massive library
translate the postgres ANY() operator into Node js using Massive library

Time:02-02

I am trying to update an existing postgres query in the Nodejs code base.

Below is the existing query to select based on name

constructor(schema){
 this.table = schema.employee_tb;
}

row = await this.table.findOne({ name });

Below is the Postgres query I am trying to translate and update the above code into...

select * from table where name = 'john' and '3467' = ANY(group_number)

I tried

row = await this.table.findOne({ name : 'john' && ANY(group_number): '3467' });

For which it is throwing an error.

Basically, I am trying to have the ANY operator in using the massive library.

NOTE: group_number is character_varying[] which has data like {3467, 3455, 3421}..... the query should return the row which contains the group number in the group_number column.

Please help, Thanks in advance.

CodePudding user response:

If you are looking to findOne that has the name 'john' AND the group_number '3467', you need to update your object syntax.

row = await this.table.findOne({ name : 'john', group_number : '3467' });

If you are using Sequelize here for the findOne method, I would take a look at the docs here where you can add in the ANY operator with Op.any in your query, but you will need to have that character_varying[] ready to pass into the function.

CodePudding user response:

This worked!!

row = await this.table.where(`name = 'jhon' and '3467' = ANY(group_number)`);
  • Related