Objection.js 扩展多个插件

Objection.js Extends Multiple Plugins

我想使用 2 个插件,objection-password and objection-paginator,但所有插件都需要扩展一些基础 classes,即用户 class。如何在 objectionjs 中扩展 2 classes?

我的代码

const Password = require('objection-password')()

const Paginator = require('objection-paginator')
const { Model } = require('objection')

// Users class also must extends Passoword to use the method
class Users extends Paginator(Model) {
  static get tableName () {
    return 'users'
  }
}

module.exports = Users

为了使用多个插件,您需要使用反对包中的 mixins

下面是使用多个插件后您的代码应该是什么样子。

const Password = require('objection-password')()
const Paginator = require('objection-paginator')
const { mixin, Model } = require('objection')

// Users class also must extends Passoword to use the method
class Users extends mixin(Model, [
    Password,
    Paginator
]) {
  static get tableName () {
    return 'users'
  }
}

module.exports = Users

Reference from objection documentation