Files

90 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2025-09-02 21:43:00 +10:00
import { Model } from "objection";
import db from "../db.js";
2026-03-03 08:44:42 +10:00
import { castJsonIfNeed, convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js";
2025-09-02 21:43:00 +10:00
import Certificate from "./certificate.js";
import now from "./now_helper.js";
import User from "./user.js";
2020-02-19 15:55:06 +11:00
Model.knex(db());
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
const boolFields = ["is_deleted", "enabled", "tcp_forwarding", "udp_forwarding"];
2024-10-10 15:53:11 +10:00
2020-02-19 15:55:06 +11:00
class Stream extends Model {
2025-09-02 21:43:00 +10:00
$beforeInsert() {
this.created_on = now();
this.modified_on = now();
2020-02-19 15:55:06 +11:00
// Default for meta
2025-09-02 21:43:00 +10:00
if (typeof this.meta === "undefined") {
2020-02-19 15:55:06 +11:00
this.meta = {};
}
}
2025-09-02 21:43:00 +10:00
$beforeUpdate() {
this.modified_on = now();
2020-02-19 15:55:06 +11:00
}
2024-10-10 15:53:11 +10:00
$parseDatabaseJson(json) {
2025-09-02 21:43:00 +10:00
const thisJson = super.$parseDatabaseJson(json);
return convertIntFieldsToBool(thisJson, boolFields);
2024-10-10 15:53:11 +10:00
}
$formatDatabaseJson(json) {
2025-09-02 21:43:00 +10:00
const thisJson = convertBoolFieldsToInt(json, boolFields);
return super.$formatDatabaseJson(thisJson);
2024-10-10 15:53:11 +10:00
}
2025-09-02 21:43:00 +10:00
static get name() {
return "Stream";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get tableName() {
return "stream";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get jsonAttributes() {
return ["meta"];
2020-02-19 15:55:06 +11:00
}
2026-03-03 08:44:42 +10:00
static get defaultAllowGraph() {
return "[owner,certificate]";
}
static get defaultExpand() {
return ["certificate", "owner"];
}
static get defaultOrder() {
return [castJsonIfNeed("incoming_port"), "ASC"];
}
2025-09-02 21:43:00 +10:00
static get relationMappings() {
2020-02-19 15:55:06 +11:00
return {
owner: {
2025-09-02 21:43:00 +10:00
relation: Model.HasOneRelation,
2020-02-19 15:55:06 +11:00
modelClass: User,
2025-09-02 21:43:00 +10:00
join: {
from: "stream.owner_user_id",
to: "user.id",
},
modify: (qb) => {
qb.where("user.is_deleted", 0);
2020-02-19 15:55:06 +11:00
},
2024-06-02 20:03:28 +01:00
},
certificate: {
2025-09-02 21:43:00 +10:00
relation: Model.HasOneRelation,
2024-06-02 20:03:28 +01:00
modelClass: Certificate,
2025-09-02 21:43:00 +10:00
join: {
from: "stream.certificate_id",
to: "certificate.id",
},
modify: (qb) => {
qb.where("certificate.is_deleted", 0);
2024-06-02 20:03:28 +01:00
},
2025-09-02 21:43:00 +10:00
},
2020-02-19 15:55:06 +11:00
};
}
}
2025-09-02 21:43:00 +10:00
export default Stream;