certificate.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 userModel from "./user.js";
  11. Model.knex(db);
  12. const boolFields = ["is_deleted"];
  13. class Certificate extends Model {
  14. $beforeInsert() {
  15. this.created_on = now();
  16. this.modified_on = now();
  17. // Default for expires_on
  18. if (typeof this.expires_on === "undefined") {
  19. this.expires_on = now();
  20. }
  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. const thisJson = super.$parseDatabaseJson(json);
  40. return convertIntFieldsToBool(thisJson, boolFields);
  41. }
  42. $formatDatabaseJson(json) {
  43. const thisJson = convertBoolFieldsToInt(json, boolFields);
  44. return super.$formatDatabaseJson(thisJson);
  45. }
  46. static get name() {
  47. return "Certificate";
  48. }
  49. static get tableName() {
  50. return "certificate";
  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: userModel,
  60. join: {
  61. from: "certificate.owner_user_id",
  62. to: "user.id",
  63. },
  64. modify: (qb) => {
  65. qb.where("user.is_deleted", 0);
  66. },
  67. },
  68. proxy_hosts: {
  69. relation: Model.HasManyRelation,
  70. modelClass: proxyHostModel,
  71. join: {
  72. from: "certificate.id",
  73. to: "proxy_host.certificate_id",
  74. },
  75. modify: (qb) => {
  76. qb.where("proxy_host.is_deleted", 0);
  77. },
  78. },
  79. dead_hosts: {
  80. relation: Model.HasManyRelation,
  81. modelClass: deadHostModel,
  82. join: {
  83. from: "certificate.id",
  84. to: "dead_host.certificate_id",
  85. },
  86. modify: (qb) => {
  87. qb.where("dead_host.is_deleted", 0);
  88. },
  89. },
  90. redirection_hosts: {
  91. relation: Model.HasManyRelation,
  92. modelClass: redirectionHostModel,
  93. join: {
  94. from: "certificate.id",
  95. to: "redirection_host.certificate_id",
  96. },
  97. modify: (qb) => {
  98. qb.where("redirection_host.is_deleted", 0);
  99. },
  100. },
  101. };
  102. }
  103. }
  104. export default Certificate;