加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 高频率调用导致在Waterline&Sails中使用findO

发布时间:2020-12-13 18:06:49 所属栏目:百科 来源:网络整理
导读:如何在 Postgresql数据库的Sails中使用Waterline处理高频updateOrCreate请求? 我尝试使用findOrCreate然后更新项目,我尝试了findOne然后更新或创建项目,我试图放置一个beforeCreate,一个beforeValidation钩子方法来检查项目是否存在但没有任何成功. 我是否
如何在 Postgresql数据库的Sails中使用Waterline处理高频updateOrCreate请求?

我尝试使用findOrCreate然后更新项目,我尝试了findOne然后更新或创建项目,我试图放置一个beforeCreate,一个beforeValidation钩子方法来检查项目是否存在但没有任何成功.
我是否应该添加错误处理程序以从唯一索引中获取错误并再试一次?

在Waterline docs中,有一个警告,但没有解决这个问题的方向.

谢谢你的任何提示.

Should I add an error handler to get errors from the unique index and try again?

在Waterline实施交易之前,这将是唯一的选择.就像是:

// This will hold the found or created user
var user;

// Keep repeating until we find or create a user,or get an error we dont expect

async.doUntil(

    function findOrCreate(cb) {

        // Try findOrCreate
        User.findOrCreate(criteria,values).exec(function(err,_user) {

            // If we get an error that is not a uniqueness error on the
            // attribute we expect collisions on,bail out of the doUntil
            if (err && 
                (
                    !err.invalidAttributes["myUniqueAttribute"] ||
                    !_.find(err.invalidAttributes["myUniqueAttribute"],{rule: 'unique'})
                )
               ) {
               return cb(err);
            } 

            // Otherwise set the user var
            // It may still be undefined if a uniqueness error occurred;
            // this will just cause doUntil to run this function again
            else {
               user = _user;
               return cb();
            }                
        },// If we have a user,we are done.  Otherwise go again.
    function test() {return user},// We are done!
    function done(err) {
        if (err) {return res.serverError(err);}
        // "user" now contains the found or created user
    }

});

不是最漂亮的,但它应该做的伎俩.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读