redirection_host.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 User = require('./user');
  7. const Certificate = require('./certificate');
  8. const now = require('./now_helper');
  9. Model.knex(db);
  10. const boolFields = [
  11. 'is_deleted',
  12. 'enabled',
  13. 'preserve_path',
  14. 'ssl_forced',
  15. 'block_exploits',
  16. ];
  17. class RedirectionHost extends Model {
  18. $beforeInsert () {
  19. this.created_on = now();
  20. this.modified_on = now();
  21. // Default for domain_names
  22. if (typeof this.domain_names === 'undefined') {
  23. this.domain_names = [];
  24. }
  25. // Default for meta
  26. if (typeof this.meta === 'undefined') {
  27. this.meta = {};
  28. }
  29. this.domain_names.sort();
  30. }
  31. $beforeUpdate () {
  32. this.modified_on = now();
  33. // Sort domain_names
  34. if (typeof this.domain_names !== 'undefined') {
  35. this.domain_names.sort();
  36. }
  37. }
  38. $parseDatabaseJson(json) {
  39. json = super.$parseDatabaseJson(json);
  40. return helpers.convertIntFieldsToBool(json, boolFields);
  41. }
  42. $formatDatabaseJson(json) {
  43. json = helpers.convertBoolFieldsToInt(json, boolFields);
  44. return super.$formatDatabaseJson(json);
  45. }
  46. static get name () {
  47. return 'RedirectionHost';
  48. }
  49. static get tableName () {
  50. return 'redirection_host';
  51. }
  52. static get jsonAttributes () {
  53. return ['domain_names', 'meta'];
  54. }
  55. static get relationMappings () {
  56. return {
  57. owner: {
  58. relation: Model.HasOneRelation,
  59. modelClass: User,
  60. join: {
  61. from: 'redirection_host.owner_user_id',
  62. to: 'user.id'
  63. },
  64. modify: function (qb) {
  65. qb.where('user.is_deleted', 0);
  66. }
  67. },
  68. certificate: {
  69. relation: Model.HasOneRelation,
  70. modelClass: Certificate,
  71. join: {
  72. from: 'redirection_host.certificate_id',
  73. to: 'certificate.id'
  74. },
  75. modify: function (qb) {
  76. qb.where('certificate.is_deleted', 0);
  77. }
  78. }
  79. };
  80. }
  81. }
  82. module.exports = RedirectionHost;