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