certificate.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. import { Model } from "objection";
  4. import db from "../db.js";
  5. import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js";
  6. import deadHostModel from "./dead_host.js";
  7. import now from "./now_helper.js";
  8. import proxyHostModel from "./proxy_host.js";
  9. import redirectionHostModel from "./redirection_host.js";
  10. import streamModel from "./stream.js";
  11. import userModel from "./user.js";
  12. Model.knex(db);
  13. const boolFields = ["is_deleted"];
  14. class Certificate extends Model {
  15. $beforeInsert() {
  16. this.created_on = now();
  17. this.modified_on = now();
  18. // Default for expires_on
  19. if (typeof this.expires_on === "undefined") {
  20. this.expires_on = now();
  21. }
  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. const thisJson = super.$parseDatabaseJson(json);
  41. return convertIntFieldsToBool(thisJson, boolFields);
  42. }
  43. $formatDatabaseJson(json) {
  44. const thisJson = convertBoolFieldsToInt(json, boolFields);
  45. return super.$formatDatabaseJson(thisJson);
  46. }
  47. static get name() {
  48. return "Certificate";
  49. }
  50. static get tableName() {
  51. return "certificate";
  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: userModel,
  61. join: {
  62. from: "certificate.owner_user_id",
  63. to: "user.id",
  64. },
  65. modify: (qb) => {
  66. qb.where("user.is_deleted", 0);
  67. },
  68. },
  69. proxy_hosts: {
  70. relation: Model.HasManyRelation,
  71. modelClass: proxyHostModel,
  72. join: {
  73. from: "certificate.id",
  74. to: "proxy_host.certificate_id",
  75. },
  76. modify: (qb) => {
  77. qb.where("proxy_host.is_deleted", 0);
  78. },
  79. },
  80. dead_hosts: {
  81. relation: Model.HasManyRelation,
  82. modelClass: deadHostModel,
  83. join: {
  84. from: "certificate.id",
  85. to: "dead_host.certificate_id",
  86. },
  87. modify: (qb) => {
  88. qb.where("dead_host.is_deleted", 0);
  89. },
  90. },
  91. redirection_hosts: {
  92. relation: Model.HasManyRelation,
  93. modelClass: redirectionHostModel,
  94. join: {
  95. from: "certificate.id",
  96. to: "redirection_host.certificate_id",
  97. },
  98. modify: (qb) => {
  99. qb.where("redirection_host.is_deleted", 0);
  100. },
  101. },
  102. streams: {
  103. relation: Model.HasManyRelation,
  104. modelClass: streamModel,
  105. join: {
  106. from: "certificate.id",
  107. to: "stream.certificate_id",
  108. },
  109. modify: (qb) => {
  110. qb.where("stream.is_deleted", 0);
  111. },
  112. },
  113. };
  114. }
  115. }
  116. export default Certificate;