skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    if (code) {
        const neighbors: Country[] = GetCountryNeighbors(code);
    
        if (neighbors.length > 0) {
            neighbors.forEach((neighbor, ind) => {
                query.orWhere('entity.country = :neighbor' + ind, {
                    ['neighbor' + ind]: neighbor.name,
                });
            });
        }
    } else if (country) {
        query.andWhere('entity.country = :country', { country });
    }
    

  2. 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;

    import { SelectQueryBuilder } from 'typeorm';
    
    // Assuming you have a query instance like this
    const query: SelectQueryBuilder<Entity> = /* Initialize your query here */;
    
    if (code) {
        const neighbors: Country[] = GetCountryNeighbors(code);
    
        if (neighbors.length > 0) {
            query.andWhere('('); // Start an OR block
            neighbors.forEach((neighbor, ind) => {
                if (ind > 0) {
                    query.orWhere('entity.country = :neighbor' + ind, { ['neighbor' + ind]: neighbor.name });
                } else {
                    query.andWhere('entity.country = :neighbor' + ind, { ['neighbor' + ind]: neighbor.name });
                }
            });
            query.andWhere(')'); // Close the OR block
        }
    } else if (country) {
        query.andWhere('entity.country = :country', { country });
    }
    
    const result = await query.getMany(); // Execute the query
    
    // Use the result as needed
    

    Hope it works 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search