proxy_host.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. const db = require('../db');
  4. const Model = require('objection').Model;
  5. const User = require('./user');
  6. const AccessList = require('./access_list');
  7. const Certificate = require('./certificate');
  8. const now = require('./now_helper');
  9. Model.knex(db);
  10. class ProxyHost extends Model {
  11. $beforeInsert () {
  12. this.created_on = now();
  13. this.modified_on = now();
  14. // Default for domain_names
  15. if (typeof this.domain_names === 'undefined') {
  16. this.domain_names = [];
  17. }
  18. // Default for meta
  19. if (typeof this.meta === 'undefined') {
  20. this.meta = {};
  21. }
  22. this.domain_names.sort();
  23. }
  24. $beforeUpdate () {
  25. this.modified_on = now();
  26. // Sort domain_names
  27. if (typeof this.domain_names !== 'undefined') {
  28. this.domain_names.sort();
  29. }
  30. }
  31. static get name () {
  32. return 'ProxyHost';
  33. }
  34. static get tableName () {
  35. return 'proxy_host';
  36. }
  37. static get jsonAttributes () {
  38. return ['domain_names', 'meta', 'locations'];
  39. }
  40. static get relationMappings () {
  41. return {
  42. owner: {
  43. relation: Model.HasOneRelation,
  44. modelClass: User,
  45. join: {
  46. from: 'proxy_host.owner_user_id',
  47. to: 'user.id'
  48. },
  49. modify: function (qb) {
  50. qb.where('user.is_deleted', 0);
  51. }
  52. },
  53. access_list: {
  54. relation: Model.HasOneRelation,
  55. modelClass: AccessList,
  56. join: {
  57. from: 'proxy_host.access_list_id',
  58. to: 'access_list.id'
  59. },
  60. modify: function (qb) {
  61. qb.where('access_list.is_deleted', 0);
  62. }
  63. },
  64. certificate: {
  65. relation: Model.HasOneRelation,
  66. modelClass: Certificate,
  67. join: {
  68. from: 'proxy_host.certificate_id',
  69. to: 'certificate.id'
  70. },
  71. modify: function (qb) {
  72. qb.where('certificate.is_deleted', 0);
  73. }
  74. }
  75. };
  76. }
  77. }
  78. module.exports = ProxyHost;