redirection_host.js 2.2 KB

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