stream.js 1.5 KB

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