rls.cjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var rls_exports = {};
  20. __export(rls_exports, {
  21. anonymousRole: () => anonymousRole,
  22. authUid: () => authUid,
  23. authenticatedRole: () => authenticatedRole,
  24. crudPolicy: () => crudPolicy
  25. });
  26. module.exports = __toCommonJS(rls_exports);
  27. var import_entity = require("../entity.cjs");
  28. var import_pg_core = require("../pg-core/index.cjs");
  29. var import_roles = require("../pg-core/roles.cjs");
  30. var import_sql = require("../sql/sql.cjs");
  31. const crudPolicy = (options) => {
  32. if (options.read === void 0) {
  33. throw new Error("crudPolicy requires a read policy");
  34. }
  35. if (options.modify === void 0) {
  36. throw new Error("crudPolicy requires a modify policy");
  37. }
  38. let read;
  39. if (options.read === true) {
  40. read = import_sql.sql`true`;
  41. } else if (options.read === false) {
  42. read = import_sql.sql`false`;
  43. } else if (options.read !== null) {
  44. read = options.read;
  45. }
  46. let modify;
  47. if (options.modify === true) {
  48. modify = import_sql.sql`true`;
  49. } else if (options.modify === false) {
  50. modify = import_sql.sql`false`;
  51. } else if (options.modify !== null) {
  52. modify = options.modify;
  53. }
  54. let rolesName = "";
  55. if (Array.isArray(options.role)) {
  56. rolesName = options.role.map((it) => {
  57. return (0, import_entity.is)(it, import_roles.PgRole) ? it.name : it;
  58. }).join("-");
  59. } else {
  60. rolesName = (0, import_entity.is)(options.role, import_roles.PgRole) ? options.role.name : options.role;
  61. }
  62. return [
  63. read && (0, import_pg_core.pgPolicy)(`crud-${rolesName}-policy-select`, {
  64. for: "select",
  65. to: options.role,
  66. using: read
  67. }),
  68. modify && (0, import_pg_core.pgPolicy)(`crud-${rolesName}-policy-insert`, {
  69. for: "insert",
  70. to: options.role,
  71. withCheck: modify
  72. }),
  73. modify && (0, import_pg_core.pgPolicy)(`crud-${rolesName}-policy-update`, {
  74. for: "update",
  75. to: options.role,
  76. using: modify,
  77. withCheck: modify
  78. }),
  79. modify && (0, import_pg_core.pgPolicy)(`crud-${rolesName}-policy-delete`, {
  80. for: "delete",
  81. to: options.role,
  82. using: modify
  83. })
  84. ].filter(Boolean);
  85. };
  86. const authenticatedRole = (0, import_roles.pgRole)("authenticated").existing();
  87. const anonymousRole = (0, import_roles.pgRole)("anonymous").existing();
  88. const authUid = (userIdColumn) => import_sql.sql`(select auth.user_id() = ${userIdColumn})`;
  89. // Annotate the CommonJS export names for ESM import in node:
  90. 0 && (module.exports = {
  91. anonymousRole,
  92. authUid,
  93. authenticatedRole,
  94. crudPolicy
  95. });
  96. //# sourceMappingURL=rls.cjs.map