proxy_host.js 2.3 KB

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