Node.js sequelize raw query with condition in array of string

Example use case of how to use sequelize raw query for retrieving results from Postgresql database with condition that the where condition must be in array of string.

Donald Le
1 min readFeb 1, 2023
Photo by <a href=”https://unsplash.com/@rayyu?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Rayyu Maldives</a> on <a href=”https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

For example, we want to do a query string like this in SQL:

select id from projects where id in (
'fe200cbb-9e4a-40c9-bfd6-8c0273067c35',
'8a136294-2a95-4cba-a0c2-16a3e887fc7a',
'2352f84b-b0e7-42eb-a578-d8121b2f427e',
'82846e7b-528c-43d9-9236-882e8ca16997',
'f539e021-8dcc-4ccc-ba4f-1d5a4723e44b',
'e2d7ebb1-5ce2-4e58-8c98-51c1a1a0654b',
'60bd4058-a9cb-4f77-a945-71ab927aff50',
'6f01bde6-a8c2-45ba-9787-1ee39fef65c7',
'b59b628c-2b9b-4307-bb46-fc3d28e1a7f6',
'b9a67481-919b-4f31-9bda-442caf3f3cf4',
'dfa7708c-eccc-4638-b4e8-36c29b46e60d',
'ebbba5e5-bc1c-4804-b61b-bbbe9e8c5c25'
) and labeling_type='type';

To do that in Node.js sequelize, we write the code like below:

const directionsArray=Array.from(directions.values())


const [resultsFilterBySingleFrameProject] = await (await postgresqlClient).query(`select id from projects where id in (:id) and labeling_type='${projectType}';`,{
replacements: {id:directionsArray},
})

Hope it helps~~

~~PEACE~~

--

--

Donald Le
Donald Le

Written by Donald Le

A passionate automation engineer who strongly believes in “A man can do anything he wants if he puts in the work”.

No responses yet