groups.js 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const Model = require('objection').Model
  2. /**
  3. * Settings model
  4. */
  5. module.exports = class Group extends Model {
  6. static get tableName() { return 'groups' }
  7. static get jsonSchema () {
  8. return {
  9. type: 'object',
  10. required: ['name'],
  11. properties: {
  12. id: {type: 'integer'},
  13. name: {type: 'string'},
  14. createdAt: {type: 'string'},
  15. updatedAt: {type: 'string'}
  16. }
  17. }
  18. }
  19. static get relationMappings() {
  20. const User = require('./users')
  21. return {
  22. users: {
  23. relation: Model.ManyToManyRelation,
  24. modelClass: User,
  25. join: {
  26. from: 'groups.id',
  27. through: {
  28. from: 'userGroups.groupId',
  29. to: 'userGroups.userId'
  30. },
  31. to: 'users.id'
  32. }
  33. }
  34. }
  35. }
  36. $beforeUpdate() {
  37. this.updatedAt = new Date().toISOString()
  38. }
  39. $beforeInsert() {
  40. this.createdAt = new Date().toISOString()
  41. this.updatedAt = new Date().toISOString()
  42. }
  43. }