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