access_list.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 AccessListAuth from "./access_list_auth.js";
  7. import AccessListClient from "./access_list_client.js";
  8. import now from "./now_helper.js";
  9. import ProxyHostModel from "./proxy_host.js";
  10. import User from "./user.js";
  11. Model.knex(db);
  12. const boolFields = ["is_deleted", "satisfy_any", "pass_auth"];
  13. class AccessList extends Model {
  14. $beforeInsert() {
  15. this.created_on = now();
  16. this.modified_on = now();
  17. // Default for meta
  18. if (typeof this.meta === "undefined") {
  19. this.meta = {};
  20. }
  21. }
  22. $beforeUpdate() {
  23. this.modified_on = now();
  24. }
  25. $parseDatabaseJson(json) {
  26. const thisJson = super.$parseDatabaseJson(json);
  27. return convertIntFieldsToBool(thisJson, boolFields);
  28. }
  29. $formatDatabaseJson(json) {
  30. const thisJson = convertBoolFieldsToInt(json, boolFields);
  31. return super.$formatDatabaseJson(thisJson);
  32. }
  33. static get name() {
  34. return "AccessList";
  35. }
  36. static get tableName() {
  37. return "access_list";
  38. }
  39. static get jsonAttributes() {
  40. return ["meta"];
  41. }
  42. static get relationMappings() {
  43. return {
  44. owner: {
  45. relation: Model.HasOneRelation,
  46. modelClass: User,
  47. join: {
  48. from: "access_list.owner_user_id",
  49. to: "user.id",
  50. },
  51. modify: (qb) => {
  52. qb.where("user.is_deleted", 0);
  53. },
  54. },
  55. items: {
  56. relation: Model.HasManyRelation,
  57. modelClass: AccessListAuth,
  58. join: {
  59. from: "access_list.id",
  60. to: "access_list_auth.access_list_id",
  61. },
  62. },
  63. clients: {
  64. relation: Model.HasManyRelation,
  65. modelClass: AccessListClient,
  66. join: {
  67. from: "access_list.id",
  68. to: "access_list_client.access_list_id",
  69. },
  70. },
  71. proxy_hosts: {
  72. relation: Model.HasManyRelation,
  73. modelClass: ProxyHostModel,
  74. join: {
  75. from: "access_list.id",
  76. to: "proxy_host.access_list_id",
  77. },
  78. modify: (qb) => {
  79. qb.where("proxy_host.is_deleted", 0);
  80. },
  81. },
  82. };
  83. }
  84. }
  85. export default AccessList;