Files

66 lines
1.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";
import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js";
import now from "./now_helper.js";
import UserPermission from "./user_permission.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", "is_disabled"];
2024-10-10 15:53:11 +10:00
2020-02-19 15:55:06 +11:00
class User 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 roles
2025-09-02 21:43:00 +10:00
if (typeof this.roles === "undefined") {
2020-02-19 15:55:06 +11:00
this.roles = [];
}
}
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 "User";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get tableName() {
return "user";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get jsonAttributes() {
return ["roles"];
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get relationMappings() {
2020-02-19 15:55:06 +11:00
return {
permissions: {
2025-09-02 21:43:00 +10:00
relation: Model.HasOneRelation,
2020-02-19 15:55:06 +11:00
modelClass: UserPermission,
2025-09-02 21:43:00 +10:00
join: {
from: "user.id",
to: "user_permission.user_id",
},
},
2020-02-19 15:55:06 +11:00
};
}
}
2025-09-02 21:43:00 +10:00
export default User;