redirection_host.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. const db = require('../db');
  4. const Model = require('objection').Model;
  5. const User = require('./user');
  6. const Certificate = require('./certificate');
  7. const now = require('./now_helper');
  8. Model.knex(db);
  9. class RedirectionHost extends Model {
  10. $beforeInsert () {
  11. this.created_on = now();
  12. this.modified_on = now();
  13. // Default for domain_names
  14. if (typeof this.domain_names === 'undefined') {
  15. this.domain_names = [];
  16. }
  17. // Default for meta
  18. if (typeof this.meta === 'undefined') {
  19. this.meta = {};
  20. }
  21. this.domain_names.sort();
  22. }
  23. $beforeUpdate () {
  24. this.modified_on = now();
  25. // Sort domain_names
  26. if (typeof this.domain_names !== 'undefined') {
  27. this.domain_names.sort();
  28. }
  29. }
  30. static get name () {
  31. return 'RedirectionHost';
  32. }
  33. static get tableName () {
  34. return 'redirection_host';
  35. }
  36. static get jsonAttributes () {
  37. return ['domain_names', 'meta'];
  38. }
  39. static get relationMappings () {
  40. return {
  41. owner: {
  42. relation: Model.HasOneRelation,
  43. modelClass: User,
  44. join: {
  45. from: 'redirection_host.owner_user_id',
  46. to: 'user.id'
  47. },
  48. modify: function (qb) {
  49. qb.where('user.is_deleted', 0);
  50. qb.omit(['id', 'created_on', 'modified_on', 'is_deleted', 'email', 'roles']);
  51. }
  52. },
  53. certificate: {
  54. relation: Model.HasOneRelation,
  55. modelClass: Certificate,
  56. join: {
  57. from: 'redirection_host.certificate_id',
  58. to: 'certificate.id'
  59. },
  60. modify: function (qb) {
  61. qb.where('certificate.is_deleted', 0);
  62. qb.omit(['id', 'created_on', 'modified_on', 'is_deleted']);
  63. }
  64. }
  65. };
  66. }
  67. }
  68. module.exports = RedirectionHost;