I tried to get data from PostgreSQL by using TypeOrm – NestJS. My problem is I cannot get multiple match conditions from a column.
My code tries to get Egypt, Jordan and Israel from country column. However, this countries are dynamically changed.
The code below is written to get data.
—–Descriptions—–
code: Universal country code (Israel = ISR)
GetCountryNeighbors: Returns to an array of neighbors of the given country code.
interface Country {
name: string;
code: string;
borders: string[];
}
if (code) {
const neighbors: Country[] = GetCountryNeighbors(code);
if (neighbors[0]) {
let text = '';
neighbors.map((neighbor, ind) => {
// Trying to get all neighbor countries. Not works...
query.orWhere('entity.country= :neighbor', { neighbor: neighbor.name });
});
}
} else if (country) {
query.andWhere('entity.country= :country', { country });
}
2
Answers
Okey, I solved the problem with the help of Maimoona Abid's answer. The problem is we cannot provide same dynamic variable for one query builder. Therefore 'neighbor' + ind system works.
Adjusted code:
This can be done by creating a dynamic query based on the list of neighboring countries, which is always updating. To build a query with multiple OR conditions, use the createQueryBuilder function. You can try this code;
Hope it works 🙂