dead_host.js 1.9 KB

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