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

185
Visualizações
Nodejs + Postgresql - how can i handle Insert multi rows query when is duplicate or violates foreign key

I'm using nodejs, express, postgres as sql server. I created Javascript API that sends queries to the server, And i want when i send a multi-line insert query to the server if one or more of the lines already in the system the query will continue without those in the server And if foreign key error happen(is not present in table "tablename") it will ask the client(wait for response) if to add it to the foreign table and then continue with the query.

So far i did this code, but can not figure out where to start with this problem:

insert(){
    return new Promise( async (resolve, reject) => {
        const client = await this.PoolCONN.connect();

        client.query(query, (err, results)=>{
            
            if(err){
                const error = this.HandlError(err)
                client.release();
                return reject(error);
            };
            
            client.release();
            return resolve(results);

        });
    })
}

HandlError(error){
    const ErrorCode = error.code;
    let errorName = "";
    let msg = "";
    switch (ErrorCode) {
        case '23505': // duplicate values
            
            msg = ``;
            errorName = "duplicate";
            break;                
        case '23503': // foreign_key_violation
            msg = ``
            errorName = "foreign key"
            break;                
        default:
            break;
    }

    return {
        error: errorName,
        errorMSG: msg,
    }

}

NOTE: I know I'm asking for a lot I'm a bit lost

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