0

Edit: I want to ask one more question, can this be achievable from the javascript part only, wanted to know for learning purpose

I'm inserting data in MySQL iterating data array in Nodejs.

 const customers = [{
    email : "rachel@friends.com" ,
    name : "rachel"
    },
   {
    email : "chandler@friends.com" ,
    name : "chandler"
    },
   {
    email : "chandler@friends.com" ,
    name : "friends"
    },
];

if the same email id arrive then I have to update the name of the existing record to newly-arrived name In above case before 3rd record insertion

name     | email
Rachel   | rachel@friends.com
chandler | chandler@friends.com

after the 3rd index iteration records should look like this

    name     | email
    Rachel   | rachel@friends.com
    friends  | chandler@friends.com

I have used promises async, await, and tried everything but it doesn't seem to be working as expected

async function insertCustomer(data) {
    
    data.forEach(async (person,index)=>{
        
        const promise1 = checkDuplicate(person.email);
        try{
            await Promise.resolve(promise1).then(async result=>{
                // console.log(result);
                if(result[0]['count'] == 0){
                   const promise2 = insert(person.email,person.name);
                   await Promise.resolve(promise2).then(result2=>{
                        // console.log(result2);
                        connection.commit();
                   });
                }else{
                   const promise2 = update(person.email,person.name);
                   await Promise.resolve(promise2).then( async result2=>{
                        // console.log(result2);
                        connection.commit();

                    });
                }
            });
             
             
            } catch(error){
            console.log(error)
            }
    console.log("finished");
});
}

Promises implementation

checkDuplicate = (email) =>{
    var sql = `SELECT count(*) as count from friends where email = '${email}'`;
    return new Promise((resolve, reject)=>{
        connection.query(sql,  (error, results)=>{
            if(error){
                return reject(error);
            }
            return resolve(results);
        });
    });
};
insert = (email,name) =>{
    var sql = `INSERT INTO friends(name1,email) values('${name}','${email}')`;
    console.log(sql);
    return new Promise((resolve, reject)=>{
        connection.query(sql,  (error, results)=>{
            if(error){
                if(error['code'] = 'ER_DUP_ENTRY') console.log("duplicate occured herre");
                return reject(error);
            }
            return resolve(results);
        });
    });
};
update = (email,name) =>{
    
    var sql = `update  friends set name1 = '${name}' where email = '${email}'`;
    console.log(sql);
    return new Promise((resolve, reject)=>{
        connection.query(sql,  (error, results)=>{
            if(error){
                return reject(error);
            }
            return resolve(results);
        });
    });
};

The result from the above implementation

        name     | email
        Rachel   | rachel@friends.com
        chandler | chandler@friends.com
        friends  | chandler@friends.com
Milind Modi
  • 321
  • 1
  • 4
  • 12
  • Thanks for pointing out the answer, yes this resolves the issue but is there any way that I can handle it from the coding part, Just wanted to know is it possible or not ? @msanford – Milind Modi Sep 16 '21 at 23:38
  • use the insert on duplicate key. The code change you're asking, will eliminate the need for the update clause. just use the insert code and it handles both insert/duplicate cases. – G-Force Sep 17 '21 at 00:13
  • @G-Force can this be achieved using javascript only? Just wanted to know – Milind Modi Oct 22 '21 at 20:22
  • 1
    yes it is JS only. just need to change the call to the DB. – G-Force Oct 27 '21 at 21:56

0 Answers0