UNPKG

@casl/mongoose

Version:

Allows to query accessible records from MongoDB based on CASL rules

86 lines (73 loc) 2.17 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); var t = Object.create, e = Object.defineProperty, r = Object.getOwnPropertyDescriptor, n = Object.getOwnPropertyNames, o = Object.getPrototypeOf, c = Object.prototype.hasOwnProperty; let u = require("@casl/ability/extra"), s = require("mongoose"); var i, a, l; function f(t) { const e = t.conditions; return t.inverted ? { $nor: [ e ] } : e; } l = null != (i = s) ? t(o(i)) : {}, s = ((t, o, u, s) => { if (o && "object" == typeof o || "function" == typeof o) for (var i, a = n(o), l = 0, f = a.length; l < f; l++) i = a[l], c.call(t, i) || i === u || e(t, i, { get: (t => o[t]).bind(null, i), enumerable: !(s = r(o, i)) || s.enumerable }); return t; })(!a && i && i.t ? l : e(l, "default", { value: i, enumerable: !0 }), i); const b = { and: t => ({ $and: t }), or: t => ({ $or: t }), empty: () => ({}) }, p = { $expr: { $eq: [ 0, 1 ] } }; var j = class { constructor(t, e) { this.o = t, this.u = e; } ofType(t) { const e = this.o.rulesFor(this.u, t), r = (0, u.rulesToCondition)(e, f, b); return null === r ? p : r; } }; function d(t, e = "read") { return new j(t, e); } function h(t, e, r) { const n = e.detectSubjectType({ constructor: t.model }); if (!n) throw new TypeError(`Cannot detect subject type of "${t.model.modelName}" to return accessible records`); const o = d(e, r).ofType(n); return t.and([ o ]); } function y(t, e) { return h(this.where(), t, e); } function m(t, e) { return h(this, t, e); } const O = t => { const e = "string" == typeof t ? s.default.models[t] : t; if (!e) throw new Error(`Unknown mongoose model "${t}"`); return "schema" in e ? Object.keys(e.schema.paths) : []; }; exports.accessibleBy = d, exports.accessibleFieldsBy = function(t, e = "read") { return new u.AccessibleFields(t, e, O); }, exports.accessibleRecordsPlugin = function(t) { t.query.accessibleBy = m, t.statics.accessibleBy = y; }; //# sourceMappingURL=index.cjs.map