Files

114 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2020-02-19 15:55:06 +11:00
// Objection Docs:
// http://vincit.github.io/objection.js/
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
2024-10-10 15:53:11 +10:00
const boolFields = [
2025-09-02 21:43:00 +10:00
"is_deleted",
"enabled",
"preserve_path",
"ssl_forced",
"block_exploits",
"hsts_enabled",
"hsts_subdomains",
"http2_support",
2024-10-10 15:53:11 +10:00
];
2020-02-19 15:55:06 +11:00
class RedirectionHost 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 domain_names
2025-09-02 21:43:00 +10:00
if (typeof this.domain_names === "undefined") {
2020-02-19 15:55:06 +11:00
this.domain_names = [];
}
// 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 = {};
}
this.domain_names.sort();
}
2025-09-02 21:43:00 +10:00
$beforeUpdate() {
this.modified_on = now();
2020-02-19 15:55:06 +11:00
// Sort domain_names
2025-09-02 21:43:00 +10:00
if (typeof this.domain_names !== "undefined") {
2020-02-19 15:55:06 +11:00
this.domain_names.sort();
}
}
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 "RedirectionHost";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get tableName() {
return "redirection_host";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get jsonAttributes() {
return ["domain_names", "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("domain_names"), "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: "redirection_host.owner_user_id",
to: "user.id",
},
modify: (qb) => {
qb.where("user.is_deleted", 0);
2020-02-19 15:55:06 +11:00
},
},
certificate: {
2025-09-02 21:43:00 +10:00
relation: Model.HasOneRelation,
2020-02-19 15:55:06 +11:00
modelClass: Certificate,
2025-09-02 21:43:00 +10:00
join: {
from: "redirection_host.certificate_id",
to: "certificate.id",
2020-02-19 15:55:06 +11:00
},
2025-09-02 21:43:00 +10:00
modify: (qb) => {
qb.where("certificate.is_deleted", 0);
},
},
2020-02-19 15:55:06 +11:00
};
}
}
2025-09-02 21:43:00 +10:00
export default RedirectionHost;