stream.js 1.6 KB

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