certificate.js 2.6 KB

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