user.js 1.3 KB

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