proxy_host.js 2.3 KB

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