user.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. import { Model } from "objection";
  4. import db from "../db.js";
  5. import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js";
  6. import now from "./now_helper.js";
  7. import UserPermission from "./user_permission.js";
  8. Model.knex(db);
  9. const boolFields = ["is_deleted", "is_disabled"];
  10. class User extends Model {
  11. $beforeInsert() {
  12. this.created_on = now();
  13. this.modified_on = now();
  14. // Default for roles
  15. if (typeof this.roles === "undefined") {
  16. this.roles = [];
  17. }
  18. }
  19. $beforeUpdate() {
  20. this.modified_on = now();
  21. }
  22. $parseDatabaseJson(json) {
  23. const thisJson = super.$parseDatabaseJson(json);
  24. return convertIntFieldsToBool(thisJson, boolFields);
  25. }
  26. $formatDatabaseJson(json) {
  27. const thisJson = convertBoolFieldsToInt(json, boolFields);
  28. return super.$formatDatabaseJson(thisJson);
  29. }
  30. static get name() {
  31. return "User";
  32. }
  33. static get tableName() {
  34. return "user";
  35. }
  36. static get jsonAttributes() {
  37. return ["roles"];
  38. }
  39. static get relationMappings() {
  40. return {
  41. permissions: {
  42. relation: Model.HasOneRelation,
  43. modelClass: UserPermission,
  44. join: {
  45. from: "user.id",
  46. to: "user_permission.user_id",
  47. },
  48. },
  49. };
  50. }
  51. }
  52. export default User;