Postgres jsonb query for dynamic values
In the users table I have a jsob column experience
with following json structure:
[
{
"field": "devops",
"years": 9
},
{
"field": "backend dev",
"years": 7
}
... // could be N number of objects with different values
]
Business requirement
Client can request for people with experience in any field and with their respective years experience in each
This is an example query
SELECT * FROM users
WHERE
jsonb_path_exists(experience, '$[*] ? (@.field == "devops" && @.years > 5)') and
jsonb_path_exists(experience, '$[*] ? (@.field == "backend dev" && @.years > 5)')
LIMIT 3;
Issue
Lets say if I get a request for
[
{ field: "devops", years: 5 },
{ field: "java", years: 6 },
{ field: "ui/ux", years: 2 }] // and so on
How do I dynamically create a query without worrying about sql injection?
Techstack
- Nodejs
- Typescript
- TypeORM
- Postgres
from Recent Questions - Stack Overflow https://ift.tt/3Glsb5U
https://ift.tt/eA8V8J
Comments
Post a Comment