【问题标题】:Sequelize - mongoose like pluginsSequelize - 像猫鼬一样的插件
【发布时间】:2013-12-15 04:31:04
【问题描述】:

我需要一种方法将 classMethodsinstanceMethods 添加到已定义的 Sequelize 模型中

可以在猫鼬中使用Plugins 功能实现的功能


我在 models/user.js 中为用户定义了一个模型。

现在,我想在我的自定义 nodejs 库中添加类方法,如 findByUsername、注册、登录到此用户模型。

如果目前不支持插件,是否有任何解决方法,例如在自定义库中重新定义用户模型?

我尝试使用将函数直接附加到用户模型

User.findByUsername = User.options.classMethods.findByUsername = function(username, callback) {
    ...
}

它的作用类似于 classMethod 和某种作品,但我认为这不是正确的方法。

【问题讨论】:

    标签: node.js mongoose sequelize.js


    【解决方案1】:

    虽然(目前)还没有任何记录在案的方法来实现这一点,但根据 mickhansen 对 github 问题 here 的评论,以下解决方法效果很好。

    classMethod

    User.findByUsername = function(username, callback) {
      ...
    }
    

    instanceMethod

    User.DAO.prototype.authenticate = function(password, callback) {
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-26
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多