groups.mjs 1.1 KB

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