proxy_host.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. qb.omit(['id', 'created_on', 'modified_on', 'is_deleted', 'email', 'roles']);
  52. }
  53. },
  54. access_list: {
  55. relation: Model.HasOneRelation,
  56. modelClass: AccessList,
  57. join: {
  58. from: 'proxy_host.access_list_id',
  59. to: 'access_list.id'
  60. },
  61. modify: function (qb) {
  62. qb.where('access_list.is_deleted', 0);
  63. qb.omit(['id', 'created_on', 'modified_on', 'is_deleted']);
  64. }
  65. },
  66. certificate: {
  67. relation: Model.HasOneRelation,
  68. modelClass: Certificate,
  69. join: {
  70. from: 'proxy_host.certificate_id',
  71. to: 'certificate.id'
  72. },
  73. modify: function (qb) {
  74. qb.where('certificate.is_deleted', 0);
  75. qb.omit(['id', 'created_on', 'modified_on', 'is_deleted']);
  76. }
  77. }
  78. };
  79. }
  80. }
  81. module.exports = ProxyHost;