proxy_host.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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 defaultAllowGraph() {
  62. return "[owner,access_list.[clients,items],certificate]";
  63. }
  64. static get defaultExpand() {
  65. return ["owner", "certificate", "access_list.[clients,items]"];
  66. }
  67. static get defaultOrder() {
  68. return [castJsonIfNeed("domain_names"), "ASC"];
  69. }
  70. static get relationMappings() {
  71. return {
  72. owner: {
  73. relation: Model.HasOneRelation,
  74. modelClass: User,
  75. join: {
  76. from: "proxy_host.owner_user_id",
  77. to: "user.id",
  78. },
  79. modify: (qb) => {
  80. qb.where("user.is_deleted", 0);
  81. },
  82. },
  83. access_list: {
  84. relation: Model.HasOneRelation,
  85. modelClass: AccessList,
  86. join: {
  87. from: "proxy_host.access_list_id",
  88. to: "access_list.id",
  89. },
  90. modify: (qb) => {
  91. qb.where("access_list.is_deleted", 0);
  92. },
  93. },
  94. certificate: {
  95. relation: Model.HasOneRelation,
  96. modelClass: Certificate,
  97. join: {
  98. from: "proxy_host.certificate_id",
  99. to: "certificate.id",
  100. },
  101. modify: (qb) => {
  102. qb.where("certificate.is_deleted", 0);
  103. },
  104. },
  105. };
  106. }
  107. }
  108. export default ProxyHost;