UNPKG

@casl/prisma

Version:

Allows to query accessible records using Prisma client based on CASL rules

274 lines (258 loc) 7.81 kB
import { CompoundCondition as t, FieldCondition as e, NULL_CONDITION as r, ObjectQueryParser as n, buildAnd as o, createTranslatorFactory as a } from "@ucast/core"; import { and as s, compare as i, createJsInterpreter as c, eq as p, gt as u, gte as f, lt as l, lte as y, ne as d, or as h, within as m } from "@ucast/js"; import { rulesToCondition as w } from "@casl/ability/extra"; import { Ability as g, fieldPatternMatcher as v } from "@casl/ability"; import { Prisma as A } from "@prisma/client/extension"; var b = class extends Error { static invalidArgument(t, e, r) { return new this(`"${t}" expects to receive ${r} but instead got "${`${typeof e}(${JSON.stringify(e, null, 2)})`}"`); } }; const O = t => t && (t.constructor === Object || !t.constructor), N = { type: "field", validate(t, e) { if (Array.isArray(e) || O(e)) throw new b(`"${t.name}" does not supports comparison of arrays and objects`); } }, j = { type: "field", parse(r, n, {hasOperators: o, field: a, parse: s}) { if (O(n) && !o(n) || Array.isArray(n)) throw new b(`"${r.name}" does not supports comparison of arrays and objects`); return O(n) ? new t("NOT", [ s(n, { field: a }) ]) : new e("notEquals", a, n); } }, $ = { type: "field", validate(t, e) { if (!Array.isArray(e)) throw b.invalidArgument(t.name, e, "an array"); } }, q = { type: "field", validate(t, e) { const r = typeof e; if (!("string" === r || "number" === r && Number.isFinite(e) || e instanceof Date)) throw b.invalidArgument(t.name, e, "comparable value"); } }, E = new Set([ "insensitive", "default" ]), x = { type: "field", validate(t, e) { if (!E.has(e)) throw b.invalidArgument(t.name, e, `one of ${Array.from(E).join(", ")}`); }, parse: () => r }, D = { type: "field", validate(t, e) { if ("string" != typeof e) throw b.invalidArgument(t.name, e, "string"); }, parse(t, r, {query: n, field: o}) { const a = "insensitive" === n.mode ? `i${t.name}` : t.name; return new e(a, o, r); } }, R = { type: "compound", validate(t, e) { if (!e || "object" != typeof e) throw b.invalidArgument(t.name, e, "an array or object"); }, parse(e, r, {parse: n}) { const o = (Array.isArray(r) ? r : [ r ]).map(t => n(t)); return new t(e.name, o); } }, T = { type: "field", validate(t, e) { if ("boolean" != typeof e) throw b.invalidArgument(t.name, e, "a boolean"); } }, S = { type: "field", validate(t, e) { if (!Array.isArray(e)) throw b.invalidArgument(t.name, e, "an array"); } }, W = { type: "field", parse(t, r, {field: n, parse: o}) { if (!O(r)) throw b.invalidArgument(t.name, r, "a query for nested relation"); return new e(t.name, n, o(r)); } }, M = (r, n) => { const o = n.parse; return o ? { ...n, parse(e, n, a) { const s = o(e, n, a); if (s.operator !== e.name) throw new Error(`Cannot invert "${r}" operator parser because it returns a complex Condition`); return s.operator = r, new t("NOT", [ s ]); } } : { ...n, parse: (n, o, a) => new t("NOT", [ new e(r, a.field, o) ]) }; }, C = { equals: N, not: j, in: $, notIn: M("in", $), lt: q, lte: q, gt: q, gte: q, mode: x, startsWith: D, endsWith: D, contains: D, isEmpty: T, has: { type: "field" }, hasSome: S, hasEvery: S, NOT: R, AND: R, OR: R, every: W, some: W, none: M("some", W), is: W, isNot: M("is", W), isSet: T }; function I(t) { return t && "object" == typeof t ? t.valueOf() : t; } const J = c({ equals: p, notEquals: d, in: m, lt: l, lte: y, gt: u, gte: f, startsWith: (t, e, {get: r}) => r(e, t.field).startsWith(t.value), istartsWith: (t, e, {get: r}) => r(e, t.field).toLowerCase().startsWith(t.value.toLowerCase()), endsWith: (t, e, {get: r}) => r(e, t.field).endsWith(t.value), iendsWith: (t, e, {get: r}) => r(e, t.field).toLowerCase().endsWith(t.value.toLowerCase()), contains: (t, e, {get: r}) => r(e, t.field).includes(t.value), icontains: (t, e, {get: r}) => r(e, t.field).toLowerCase().includes(t.value.toLowerCase()), isEmpty: (t, e, {get: r}) => { const n = r(e, t.field); return (Array.isArray(n) && 0 === n.length) === t.value; }, has: (t, e, {get: r}) => { const n = r(e, t.field); return Array.isArray(n) && n.includes(t.value); }, hasSome: (t, e, {get: r}) => { const n = r(e, t.field); return Array.isArray(n) && t.value.some(t => n.includes(t)); }, hasEvery: (t, e, {get: r}) => { const n = r(e, t.field); return Array.isArray(n) && t.value.every(t => n.includes(t)); }, and: s, or: h, AND: s, OR: h, NOT: (t, e, {interpret: r}) => t.value.every(t => !r(t, e)), every: (t, e, {get: r, interpret: n}) => { const o = r(e, t.field); return Array.isArray(o) && o.every(e => n(t.value, e)); }, some: (t, e, {get: r, interpret: n}) => { const o = r(e, t.field); return Array.isArray(o) && o.some(e => n(t.value, e)); }, is: (t, e, {get: r, interpret: n}) => { const o = r(e, t.field); return o && "object" == typeof o && n(t.value, o); }, isSet: (t, e, {get: r}) => void 0 !== r(e, t.field) }, { get: (t, e) => t[e], compare: (t, e) => i(I(t), I(e)) }), k = a((new class extends n { constructor() { super(C, { defaultOperatorName: "equals" }); } parse(t, e) { return e && e.field ? o(this.parseFieldOperators(e.field, t)) : super.parse(t); } }).parse, J); function z(t) { return t.inverted ? { NOT: t.conditions } : t.conditions; } const B = { and: t => ({ AND: t }), or: t => ({ OR: t }), empty: () => ({}) }; var F = class { constructor(t, e) { this.t = t, this.o = e; } ofType(t) { const e = this.t.rulesFor(this.o, t), r = w(e, z, B); return null === r ? { OR: [] } : r; } }; function G(t, e = "read") { return new F(t, e); } function H(t = [], e = {}) { return new g(t, { ...e, conditionsMatcher: k, fieldMatcher: v }); } function K() { return A.defineExtension({ name: "casl", query: { $allModels: { $allOperations({args: t, query: e}) { const r = t?.where; let n = t; return L(r) && (n = { ...t }, Array.isArray(r) ? n.where = { OR: [], AND: r } : n.where = "object" == typeof r ? { ...r, OR: [], AND: [ r ] } : { OR: [], AND: [ r ] }), e(n); } } } }); } function L(t) { if (!t || "object" != typeof t) return !1; const e = t; if (Array.isArray(e)) { for (let t = 0, r = e.length; t < r; t++) if (L(e[t])) return !0; return !1; } if (0 === e.OR?.length) return !0; for (const t in e) { if (!Object.hasOwn(e, t)) continue; const r = e[t]; if (r && "object" == typeof r && L(r)) return !0; } return !1; } export { b as a, k as i, H as n, G as r, K as t }; //# sourceMappingURL=runtime-CsD-HGw1.mjs.map