You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
966 B

  1. const Model = require('objection').Model
  2. /**
  3. * Groups 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. return {
  21. users: {
  22. relation: Model.ManyToManyRelation,
  23. modelClass: require('./users'),
  24. join: {
  25. from: 'groups.id',
  26. through: {
  27. from: 'userGroups.groupId',
  28. to: 'userGroups.userId'
  29. },
  30. to: 'users.id'
  31. }
  32. }
  33. }
  34. }
  35. $beforeUpdate() {
  36. this.updatedAt = new Date().toISOString()
  37. }
  38. $beforeInsert() {
  39. this.createdAt = new Date().toISOString()
  40. this.updatedAt = new Date().toISOString()
  41. }
  42. }