dead_host.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 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 relationMappings() {
  50. return {
  51. owner: {
  52. relation: Model.HasOneRelation,
  53. modelClass: User,
  54. join: {
  55. from: "dead_host.owner_user_id",
  56. to: "user.id",
  57. },
  58. modify: (qb) => {
  59. qb.where("user.is_deleted", 0);
  60. },
  61. },
  62. certificate: {
  63. relation: Model.HasOneRelation,
  64. modelClass: Certificate,
  65. join: {
  66. from: "dead_host.certificate_id",
  67. to: "certificate.id",
  68. },
  69. modify: (qb) => {
  70. qb.where("certificate.is_deleted", 0);
  71. },
  72. },
  73. };
  74. }
  75. }
  76. export default DeadHost;