groups.js 1.02 KB
Newer Older
1 2 3
const Model = require('objection').Model

/**
4
 * Groups model
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 */
module.exports = class Group extends Model {
  static get tableName() { return 'groups' }

  static get jsonSchema () {
    return {
      type: 'object',
      required: ['name'],

      properties: {
        id: {type: 'integer'},
        name: {type: 'string'},
        createdAt: {type: 'string'},
        updatedAt: {type: 'string'}
      }
    }
  }

23 24 25 26
  static get jsonAttributes() {
    return ['permissions', 'pageRules']
  }

27 28 29 30
  static get relationMappings() {
    return {
      users: {
        relation: Model.ManyToManyRelation,
31
        modelClass: require('./users'),
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        join: {
          from: 'groups.id',
          through: {
            from: 'userGroups.groupId',
            to: 'userGroups.userId'
          },
          to: 'users.id'
        }
      }
    }
  }

  $beforeUpdate() {
    this.updatedAt = new Date().toISOString()
  }
  $beforeInsert() {
    this.createdAt = new Date().toISOString()
    this.updatedAt = new Date().toISOString()
  }
}