如何使用节点承诺实现事务

3phpmpom  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(270)

我正在用开发一个应用程序 node , mysql 以及 angular4 . 在那里,我有一组与单个任务相关的函数。所有这些都是异步函数,与如下所示的承诺一起工作

setAprroveTrue(curentHOD, user, group, message, approvemail, approved_byHR = 0) { //HR or super admin can override anyones leave. default it is set as not by and hr.
        return new Promise((resolve, reject) => {
            Leaves.setHodMessage(group, message, curentHOD).then((messageArr) => {
                q = "update leave_taken_superior_map set lstm_col_status= ? ,ltsm_all_approved = 1,ltsm_overide_by_hr = ?,ltsm_message = ? where ltsm_group_id = ?;update leave_taken set alt_leave_approved = 1 where alt_group_by = ?";
                params = [config.key_word_approved_leave, approved_byHR, messageArr, group, group];
                sqlq.executeQuery(q, params).then((data) => {
                    if (approvemail === true) {
                        emails.sendEmail('pavithrarox1@gmail.com', 'pavithra@gmail.lk', 'Good news! Your leave request has been approved', emails.getLeaveApprovedEmail(group))
                    } else {
                        emails.sendEmail('pavithrarox1@gmail.com', 'pavithra@gmail.lk', 'Bad news! Your leave request has been rejected', emails.getLeaveDissapprovedEmail(group))
                    }
                    //no need to wait for email to send.it will work as a background procass
                    resolve(data)
                }).catch((err) => {
                    console.log(err)
                    reject(err)
                });
            }).catch((err) => {
                reject(err)
            })
        })
    }

如何在此函数中实现事务?所以,如果 setHodMessage 函数成功执行,如果 executeQuery 功能。我如何回滚所有内容?

k3bvogb1

k3bvogb11#

您可以处理拒绝案例并同样应用逻辑。
所以每一个承诺都有决心、拒绝和阻碍

promise.then(
      resolved=>{},
      reject=>{}
).catch(error=>{});

所以在你的情况下。

setAprroveTrue(curentHOD, user, group, message, approvemail, approved_byHR = 0) { //HR or super admin can override anyones leave. default it is set as not by and hr.
        return new Promise((resolve, reject) => {
            Leaves.setHodMessage(group, message, curentHOD).then((messageArr) => {
                q = "update leave_taken_superior_map set lstm_col_status= ? ,ltsm_all_approved = 1,ltsm_overide_by_hr = ?,ltsm_message = ? where ltsm_group_id = ?;update leave_taken set alt_leave_approved = 1 where alt_group_by = ?";
                params = [config.key_word_approved_leave, approved_byHR, messageArr, group, group];
                sqlq.executeQuery(q, params).then((data) => {
                    if (approvemail === true) {
                        emails.sendEmail('pavithrarox1@gmail.com', 'pavithra@gmail.lk', 'Good news! Your leave request has been approved', emails.getLeaveApprovedEmail(group))
                    } else {
                        emails.sendEmail('pavithrarox1@gmail.com', 'pavithra@gmail.lk', 'Bad news! Your leave request has been rejected', emails.getLeaveDissapprovedEmail(group))
                    }
                    //no need to wait for email to send.it will work as a background procass
                    resolve(data)
                },()=>{  
                        //do your rollback stuff here
                      }).catch((err) => {
                    console.log(err)
                    reject(err)
                });
            }).catch((err) => {
                reject(err)
            })
        })
    }

让我知道你是否需要帮助。

wlp8pajw

wlp8pajw2#

好吧,我在这里发现了问题。我无法让事务在我的应用程序上工作的原因是我使用了 mysql.createPool 而不是 mysql.createConnection . 在createpool中没有实现事务的选项

相关问题