Skip to content

Commit

Permalink
Release 0.5.9
Browse files Browse the repository at this point in the history
  • Loading branch information
dennemark committed Aug 16, 2024
1 parent 97136c1 commit 6229c1e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 49 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@


## [0.5.9](https://github.com/dennemark/prisma-extension-casl/compare/0.5.8...0.5.9) (2024-08-16)


### Bug Fixes

* :bug: properly include create relations ([97136c1](https://github.com/dennemark/prisma-extension-casl/commit/97136c1f0226750f2a58bee1b72596051ba2033c))

## [0.5.8](https://github.com/dennemark/prisma-extension-casl/compare/0.5.7...0.5.8) (2024-08-16)


Expand Down
3 changes: 2 additions & 1 deletion dist/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { PrismaQuery } from '@casl/prisma';
import { Prisma } from '@prisma/client';

type CreationTree = {
type: string;
action: string;
model: Prisma.ModelName;
children: Record<string, CreationTree>;
};

Expand Down
3 changes: 2 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { PrismaQuery } from '@casl/prisma';
import { Prisma } from '@prisma/client';

type CreationTree = {
type: string;
action: string;
model: Prisma.ModelName;
children: Record<string, CreationTree>;
};

Expand Down
52 changes: 29 additions & 23 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,8 @@ function getFluentModel(startModel, data) {
}

// src/applyDataQuery.ts
function applyDataQuery(abilities, args, action, model, creationTree = { type: "create", children: {} }) {
creationTree.type = action;
function applyDataQuery(abilities, args, action, model, creationTree) {
const tree = creationTree ? creationTree : { action, model, children: {} };
const permittedFields = getPermittedFields(abilities, action, model);
const accessibleQuery = m5(abilities, action)[model];
const mutationArgs = [];
Expand Down Expand Up @@ -964,8 +964,8 @@ function applyDataQuery(abilities, args, action, model, creationTree = { type: "
if (nestedAction in caslNestedOperationDict) {
const mutationAction = caslNestedOperationDict[nestedAction];
const isConnection = nestedAction === "connect" || nestedAction === "disconnect";
creationTree.children[field] = { type: mutationAction, children: {} };
const dataQuery = applyDataQuery(abilities, nestedArgs, mutationAction, relationModel.type, creationTree.children[field]);
tree.children[field] = { action: mutationAction, model: relationModel.type, children: {} };
const dataQuery = applyDataQuery(abilities, nestedArgs, mutationAction, relationModel.type, tree.children[field]);
mutation[field][nestedAction] = dataQuery.args;
if (isConnection) {
const accessibleQuery2 = m5(abilities, mutationAction)[relationModel.type];
Expand All @@ -982,7 +982,7 @@ function applyDataQuery(abilities, args, action, model, creationTree = { type: "
}
});
});
return { args, creationTree };
return { args, creationTree: tree };
}

// src/applyWhereQuery.ts
Expand Down Expand Up @@ -1028,22 +1028,7 @@ function applyIncludeSelectQuery(abilities, args, model) {
return args;
}

// src/convertCreationTreeToSelect.ts
function convertCreationTreeToSelect(relationQuery) {
if (Object.keys(relationQuery.children).length === 0) {
return relationQuery.type === "create" ? {} : null;
}
const relationResult = {};
for (const key in relationQuery.children) {
const childRelation = convertCreationTreeToSelect(relationQuery.children[key]);
if (childRelation !== null) {
relationResult[key] = { select: childRelation };
}
}
return Object.keys(relationResult).length > 0 ? relationResult : relationQuery.type === "create" ? {} : null;
}

// src/applyRuleRelationsQuery.ts
// src/getRuleRelationsQuery.ts
function flattenAst(ast) {
if (["and", "or"].includes(ast.operator.toLowerCase())) {
return ast.value.flatMap((childAst) => flattenAst(childAst));
Expand Down Expand Up @@ -1074,6 +1059,27 @@ function getRuleRelationsQuery(model, ast, dataRelationQuery = {}) {
}
return obj;
}

// src/convertCreationTreeToSelect.ts
function convertCreationTreeToSelect(abilities, relationQuery) {
let relationResult = {};
if (relationQuery.action === "create") {
const ast = d4(abilities, relationQuery.action, relationQuery.model);
relationResult = getRuleRelationsQuery(relationQuery.model, ast, {});
}
if (Object.keys(relationQuery.children).length === 0) {
return relationQuery.action === "create" ? relationResult : null;
}
for (const key in relationQuery.children) {
const childRelation = convertCreationTreeToSelect(abilities, relationQuery.children[key]);
if (childRelation !== null) {
relationResult[key] = { select: childRelation };
}
}
return Object.keys(relationResult).length > 0 ? relationResult : relationQuery.action === "create" ? {} : null;
}

// src/applyRuleRelationsQuery.ts
function mergeArgsAndRelationQuery(args, relationQuery) {
const mask = {};
let found = false;
Expand Down Expand Up @@ -1138,7 +1144,7 @@ function applyRuleRelationsQuery(args, abilities, action, model, creationTree) {
};
}));
const ast = d4(ability, action, model);
const creationSelectQuery = creationTree ? convertCreationTreeToSelect(creationTree) ?? {} : {};
const creationSelectQuery = creationTree ? convertCreationTreeToSelect(abilities, creationTree) ?? {} : {};
const queryRelations = getRuleRelationsQuery(model, ast, creationSelectQuery === true ? {} : creationSelectQuery);
if (!("select" in args) && !("include" in args)) {
args.include = {};
Expand Down Expand Up @@ -1186,7 +1192,7 @@ function filterQueryResults(result, mask, creationTree, abilities, model) {
if (!entry) {
return null;
}
if (creationTree?.type === "create") {
if (creationTree?.action === "create") {
try {
if (!abilities.can("create", getSubject(model, entry))) {
throw new Error("");
Expand Down
52 changes: 29 additions & 23 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@ function getFluentModel(startModel, data) {
}

// src/applyDataQuery.ts
function applyDataQuery(abilities, args, action, model, creationTree = { type: "create", children: {} }) {
creationTree.type = action;
function applyDataQuery(abilities, args, action, model, creationTree) {
const tree = creationTree ? creationTree : { action, model, children: {} };
const permittedFields = getPermittedFields(abilities, action, model);
const accessibleQuery = m5(abilities, action)[model];
const mutationArgs = [];
Expand Down Expand Up @@ -939,8 +939,8 @@ function applyDataQuery(abilities, args, action, model, creationTree = { type: "
if (nestedAction in caslNestedOperationDict) {
const mutationAction = caslNestedOperationDict[nestedAction];
const isConnection = nestedAction === "connect" || nestedAction === "disconnect";
creationTree.children[field] = { type: mutationAction, children: {} };
const dataQuery = applyDataQuery(abilities, nestedArgs, mutationAction, relationModel.type, creationTree.children[field]);
tree.children[field] = { action: mutationAction, model: relationModel.type, children: {} };
const dataQuery = applyDataQuery(abilities, nestedArgs, mutationAction, relationModel.type, tree.children[field]);
mutation[field][nestedAction] = dataQuery.args;
if (isConnection) {
const accessibleQuery2 = m5(abilities, mutationAction)[relationModel.type];
Expand All @@ -957,7 +957,7 @@ function applyDataQuery(abilities, args, action, model, creationTree = { type: "
}
});
});
return { args, creationTree };
return { args, creationTree: tree };
}

// src/applyWhereQuery.ts
Expand Down Expand Up @@ -1003,22 +1003,7 @@ function applyIncludeSelectQuery(abilities, args, model) {
return args;
}

// src/convertCreationTreeToSelect.ts
function convertCreationTreeToSelect(relationQuery) {
if (Object.keys(relationQuery.children).length === 0) {
return relationQuery.type === "create" ? {} : null;
}
const relationResult = {};
for (const key in relationQuery.children) {
const childRelation = convertCreationTreeToSelect(relationQuery.children[key]);
if (childRelation !== null) {
relationResult[key] = { select: childRelation };
}
}
return Object.keys(relationResult).length > 0 ? relationResult : relationQuery.type === "create" ? {} : null;
}

// src/applyRuleRelationsQuery.ts
// src/getRuleRelationsQuery.ts
function flattenAst(ast) {
if (["and", "or"].includes(ast.operator.toLowerCase())) {
return ast.value.flatMap((childAst) => flattenAst(childAst));
Expand Down Expand Up @@ -1049,6 +1034,27 @@ function getRuleRelationsQuery(model, ast, dataRelationQuery = {}) {
}
return obj;
}

// src/convertCreationTreeToSelect.ts
function convertCreationTreeToSelect(abilities, relationQuery) {
let relationResult = {};
if (relationQuery.action === "create") {
const ast = d4(abilities, relationQuery.action, relationQuery.model);
relationResult = getRuleRelationsQuery(relationQuery.model, ast, {});
}
if (Object.keys(relationQuery.children).length === 0) {
return relationQuery.action === "create" ? relationResult : null;
}
for (const key in relationQuery.children) {
const childRelation = convertCreationTreeToSelect(abilities, relationQuery.children[key]);
if (childRelation !== null) {
relationResult[key] = { select: childRelation };
}
}
return Object.keys(relationResult).length > 0 ? relationResult : relationQuery.action === "create" ? {} : null;
}

// src/applyRuleRelationsQuery.ts
function mergeArgsAndRelationQuery(args, relationQuery) {
const mask = {};
let found = false;
Expand Down Expand Up @@ -1113,7 +1119,7 @@ function applyRuleRelationsQuery(args, abilities, action, model, creationTree) {
};
}));
const ast = d4(ability, action, model);
const creationSelectQuery = creationTree ? convertCreationTreeToSelect(creationTree) ?? {} : {};
const creationSelectQuery = creationTree ? convertCreationTreeToSelect(abilities, creationTree) ?? {} : {};
const queryRelations = getRuleRelationsQuery(model, ast, creationSelectQuery === true ? {} : creationSelectQuery);
if (!("select" in args) && !("include" in args)) {
args.include = {};
Expand Down Expand Up @@ -1161,7 +1167,7 @@ function filterQueryResults(result, mask, creationTree, abilities, model) {
if (!entry) {
return null;
}
if (creationTree?.type === "create") {
if (creationTree?.action === "create") {
try {
if (!abilities.can("create", getSubject(model, entry))) {
throw new Error("");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prisma-extension-casl",
"version": "0.5.8",
"version": "0.5.9",
"description": "Enforce casl abilities on prisma client ",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 6229c1e

Please sign in to comment.