Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
Creating a "multi-row, multi-column, multi-table" Sequelize Query

I have to get data from a Postgres DB for my project. The API is supplying a list of country codes in the form of a string like "US, IN, PK". The Table Countries has the country ID for each country code. The table user has the User ID for each user. Now the table User_Country contains the mapping i.e. if a user has permission for a certain country signified by the has_approval flag which is a Boolean. The query has to return the information of all the users who have the has_approval flag true for all the given countries I have written the following code which runs three different queries for the same. However, I think we can write a single query using Outer join.

 let countryList = countryIds.split(",");  // countryIds = "US,IN,PK"
        let countryIdList = [];
        let countryId = "";

        for(let i = 0; i < countryList.length; i++){
            countryId = await Country.findOne({
                                       attributes: ['id'],
                                       where: {
                                               ctrn_cd : countryList[i]
                                                },
                                         raw: true
                                         });
            countryIdList.push(countryId.id);
        }

        let users = await Users.findAll({
            attributes: ['id'],
            raw: true
        })

        let UserIds = [];
        for(let i = 0; i < users.length; i ++){
            let countryCount = await UserCountry.count({
                where: {
                    ctrn_id : countryIdList,
                    user_id: users[i].id,
                    has_approval: true
                },
                raw: true
            });

        if(countryCount == countryIdList.length) UserIds.push(users[i].id);
        }

        let layoutapprovers = await User.findAll({
            attributes: ['id', 'guid', ['role_id','roleId'], 'name', ['email','emailId']],
            where:{
                id: UserIds
            },
            raw: true
        });

        return layoutapprovers;```
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda