stream.js 1.6 KB

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