3

是否可以使用 Sequelize.js 进行异步验证器?我想在保存模型之前检查是否存在关联。像这样的东西:

User = db.define("user", {
    name: Sequelize.STRING
},
{
    validate:
        hasDevice: ->
            @getDevices().success (devices) ->
                throw Exception if (devices.length < 1)
              return
})

# .... Device is just another model

User.hasMany(Device)

或者有没有办法强制检查同步运行?(不理想)

4

1 回答 1

7

您可以在 v2.0.0 中使用异步验证。它是这样工作的:

var Model = sequelize.define('Model', {
  attr: Sequelize.STRING
}, {
  validate: {
    hasAssociation: function(next) {
      functionThatChecksTheAssociation(function(ok) {
        if (ok) {
          next()
        } else {
          next('Ooops. Something is wrong!')
        }
      })
    }
  }
})
于 2013-10-29T06:27:22.213 回答