user.js 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. const db = require('../db');
  4. const Model = require('objection').Model;
  5. const UserPermission = require('./user_permission');
  6. const now = require('./now_helper');
  7. Model.knex(db);
  8. class User extends Model {
  9. $beforeInsert () {
  10. this.created_on = now();
  11. this.modified_on = now();
  12. // Default for roles
  13. if (typeof this.roles === 'undefined') {
  14. this.roles = [];
  15. }
  16. }
  17. $beforeUpdate () {
  18. this.modified_on = now();
  19. }
  20. static get name () {
  21. return 'User';
  22. }
  23. static get tableName () {
  24. return 'user';
  25. }
  26. static get jsonAttributes () {
  27. return ['roles'];
  28. }
  29. static get relationMappings () {
  30. return {
  31. permissions: {
  32. relation: Model.HasOneRelation,
  33. modelClass: UserPermission,
  34. join: {
  35. from: 'user.id',
  36. to: 'user_permission.user_id'
  37. },
  38. modify: function (qb) {
  39. qb.omit(['id', 'created_on', 'modified_on', 'user_id']);
  40. }
  41. }
  42. };
  43. }
  44. }
  45. module.exports = User;