在 pg-promise 中编写子查询的好方法

Good way to write sub queries in pg-promise

Incase 我必须在单个 API 中执行更多查询,我必须以什么方法继续 这是在主查询中编写子查询的更好方法吗 不使用 db.task 或 db.tx 否则我必须使用这些方法。 像下面这样

function userChangePassword(req, res) {
    const changePwd = req.swagger.params.body.value;

    const token = req.headers.authorization;
    token.replace('Bearer ', '');
    const decoded = jwt_decode(token);

    const newpass = changePwd.newPassword;
    const user_id = decoded.userId;

    const userSel = `select * from xx."yy" where "userId" = '${user_id}' AND "status" = 1`;
    const updatePwd = `update xx."yy" set "password" = '${newpass}' where "userId" = '${user_id}' `;

    db.query(userSel).then((usrResult) => {
            if (usrResult.length > 0) {
                db.query(updatePwd).then(() => {
                    res.send(response.success("The password has been changed successfully", []));
                });
            } else {
                res.send(response.success("The password has not changed successfully", []));
            }
        })
        .catch((err) => {
            if (util.isError(err)) res.error('NotFoundError', err); // return 404
            else res.error('InternalServerError', err); // else 500
        });
}    

请帮助我解决这个困惑.. 谢谢

如果查询之间存在依赖关系,则必须使用方法task/tx。否则,如果您希望从多查询中获得 return 数据,则可以使用 helpers.concat, and execute them as one query, which will be faster. And you can use methods multi / multiResult 将查询串联成一个查询。