redirection_host.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 Certificate = require('./certificate');
  7. const now = require('./now_helper');
  8. Model.knex(db);
  9. class RedirectionHost extends Model {
  10. $beforeInsert () {
  11. this.created_on = now();
  12. this.modified_on = now();
  13. // Default for domain_names
  14. if (typeof this.domain_names === 'undefined') {
  15. this.domain_names = [];
  16. }
  17. // Default for meta
  18. if (typeof this.meta === 'undefined') {
  19. this.meta = {};
  20. }
  21. this.domain_names.sort();
  22. }
  23. $beforeUpdate () {
  24. this.modified_on = now();
  25. // Sort domain_names
  26. if (typeof this.domain_names !== 'undefined') {
  27. this.domain_names.sort();
  28. }
  29. }
  30. static get name () {
  31. return 'RedirectionHost';
  32. }
  33. static get tableName () {
  34. return 'redirection_host';
  35. }
  36. static get jsonAttributes () {
  37. return ['domain_names', 'meta'];
  38. }
  39. static get relationMappings () {
  40. return {
  41. owner: {
  42. relation: Model.HasOneRelation,
  43. modelClass: User,
  44. join: {
  45. from: 'redirection_host.owner_user_id',
  46. to: 'user.id'
  47. },
  48. modify: function (qb) {
  49. qb.where('user.is_deleted', 0);
  50. }
  51. },
  52. certificate: {
  53. relation: Model.HasOneRelation,
  54. modelClass: Certificate,
  55. join: {
  56. from: 'redirection_host.certificate_id',
  57. to: 'certificate.id'
  58. },
  59. modify: function (qb) {
  60. qb.where('certificate.is_deleted', 0);
  61. }
  62. }
  63. };
  64. }
  65. }
  66. module.exports = RedirectionHost;