使用事务时Sequlize回调

时间:2016-03-09 13:19:46

标签: node.js orm sequelize.js

在我的应用程序中,我有一个模型说它是Record,而Record可能有几个Attachment可以上传到服务器。

通常,在使用Record(文件)创建Attachment时,我会先上传并保存文件,然后保存记录,如下所示:

function createOrUpdateInfo(req, res, next) {
    var record = req.body;
    var attachmentIds = (record.files || []).map(function (a) {
        return a.id;
    });
    var attachmentFilter = {
        where: {
            id: {
                $in: attachmentIds || []
            }
        }
    };
    DB.sequelize.transaction(function (t) {
        var pro;
        if (record.id) {
            //update

            //update the basic information first
            return Record.update(record, {
                where: {
                    id: req.params.id
                }, transaction: t
            }).then(function (num, infos) {

                //find the record just saved.
                return Record.findById(req.params.id).then(function (record) {

                    //find the attachmens which have been saved
                    return Attachment.findAll(attachmentFilter).then(function (atts) {

                        //update the record, create the association.
                        return record.setFiles(atts, {transaction: t});
                    });
                })
            });
        } else {

            //save
            return Record.create(record, {transaction: t}).then(function (record) {
                return Attachment.findAll(attachmentFilter).then(function (atts) {
                    return record.setFiles(atts, {transaction: t});
                });
            });
        }

    }).then(function (result) {
        Util.sendJson(res, result)
    }).catch(function (err) {
        next({message: err.message, code: 500});
    });
}

如图所示,创建或更新Record时,嵌套回调过多。

这可以修复吗?

1 个答案:

答案 0 :(得分:0)

我已经重新整理了你的代码了。希望它有所帮助。

function createOrUpdateInfo(req, res, next) {
    var record = req.body;
    var attachmentIds = (record.files || []).map(function (a) {
        return a.id;
    });
    var attachmentFilter = {
        where: {
            id: {
                $in: attachmentIds || []
            }
        }
    };
    DB.sequelize.transaction(function (t) {
        var pro;
        return (function () {
            if (record.id) {
                //update
                //update the basic information first
                return Record.update(record, {
                    where: {
                        id: req.params.id
                    }, transaction: t
                }).then(function (num, infos) {
                    //find the record just saved.
                    return Record.findById(req.params.id);
                });
            } else {
                //save
                return Record.create(record, {transaction: t});
            }
        }()).then(function () {
            //find the attachmens which have been saved
            return Attachment.findAll(attachmentFilter);
        }).then(function (atts) {
            //update the record, create the association.
            return record.setFiles(atts, {transaction: t});
        });

    }).then(function (result) {
        Util.sendJson(res, result)
    }).catch(function (err) {
        next({message: err.message, code: 500});
    });
}