dead_host.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. import { Model } from "objection";
  4. import db from "../db.js";
  5. import { castJsonIfNeed, convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js";
  6. import Certificate from "./certificate.js";
  7. import now from "./now_helper.js";
  8. import User from "./user.js";
  9. Model.knex(db());
  10. const boolFields = ["is_deleted", "ssl_forced", "http2_support", "enabled", "hsts_enabled", "hsts_subdomains"];
  11. class DeadHost extends Model {
  12. $beforeInsert() {
  13. this.created_on = now();
  14. this.modified_on = now();
  15. // Default for domain_names
  16. if (typeof this.domain_names === "undefined") {
  17. this.domain_names = [];
  18. }
  19. // Default for meta
  20. if (typeof this.meta === "undefined") {
  21. this.meta = {};
  22. }
  23. this.domain_names.sort();
  24. }
  25. $beforeUpdate() {
  26. this.modified_on = now();
  27. // Sort domain_names
  28. if (typeof this.domain_names !== "undefined") {
  29. this.domain_names.sort();
  30. }
  31. }
  32. $parseDatabaseJson(json) {
  33. const thisJson = super.$parseDatabaseJson(json);
  34. return convertIntFieldsToBool(thisJson, boolFields);
  35. }
  36. $formatDatabaseJson(json) {
  37. const thisJson = convertBoolFieldsToInt(json, boolFields);
  38. return super.$formatDatabaseJson(thisJson);
  39. }
  40. static get name() {
  41. return "DeadHost";
  42. }
  43. static get tableName() {
  44. return "dead_host";
  45. }
  46. static get jsonAttributes() {
  47. return ["domain_names", "meta"];
  48. }
  49. static get defaultAllowGraph() {
  50. return "[owner,certificate]";
  51. }
  52. static get defaultExpand() {
  53. return ["certificate", "owner"];
  54. }
  55. static get defaultOrder() {
  56. return [castJsonIfNeed("domain_names"), "ASC"];
  57. }
  58. static get relationMappings() {
  59. return {
  60. owner: {
  61. relation: Model.HasOneRelation,
  62. modelClass: User,
  63. join: {
  64. from: "dead_host.owner_user_id",
  65. to: "user.id",
  66. },
  67. modify: (qb) => {
  68. qb.where("user.is_deleted", 0);
  69. },
  70. },
  71. certificate: {
  72. relation: Model.HasOneRelation,
  73. modelClass: Certificate,
  74. join: {
  75. from: "dead_host.certificate_id",
  76. to: "certificate.id",
  77. },
  78. modify: (qb) => {
  79. qb.where("certificate.is_deleted", 0);
  80. },
  81. },
  82. };
  83. }
  84. }
  85. export default DeadHost;