From de1d86bf46269c3a1b581470139aa166fa0a25d6 Mon Sep 17 00:00:00 2001 From: dennemark Date: Thu, 28 Nov 2024 17:52:06 +0100 Subject: [PATCH] Release 1.0.2 --- CHANGELOG.md | 6 ++++++ dist/index.js | 28 ++++++++++++++++++++++++---- dist/index.mjs | 28 ++++++++++++++++++++++++---- package.json | 2 +- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ef0ce2..c3bdabc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ +## [1.0.2](https://github.com/dennemark/prisma-extension-casl/compare/1.0.1...1.0.2) (2024-11-28) + +### Bug Fixes + +* :bug: deep merge nested rule relations ([ecd8741](https://github.com/dennemark/prisma-extension-casl/commit/ecd87416b69ed3c477629bbaf891e182070283f5)) + ## [1.0.1](https://github.com/dennemark/prisma-extension-casl/compare/1.0.0...1.0.1) (2024-10-29) ### Bug Fixes diff --git a/dist/index.js b/dist/index.js index f6a00d0..2a59baf 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1159,6 +1159,26 @@ function convertCreationTreeToSelect(abilities, relationQuery) { return Object.keys(relationResult).length > 0 ? relationResult : relationQuery.action === "create" ? {} : null; } +// src/deepMerge.ts +function isObject(item) { + return item && typeof item === "object" && !Array.isArray(item); +} +function deepMerge(target, ...sources) { + if (!sources.length) return target; + const source = sources.shift(); + if (isObject(target) && isObject(source)) { + for (const key in source) { + if (isObject(source[key])) { + if (!target[key]) Object.assign(target, { [key]: {} }); + deepMerge(target[key], source[key]); + } else { + Object.assign(target, { [key]: source[key] }); + } + } + } + return deepMerge(target, ...sources); +} + // src/applyRuleRelationsQuery.ts function mergeArgsAndRelationQuery(args, relationQuery) { const mask = {}; @@ -1255,10 +1275,10 @@ function getNestedQueryRelations(args, abilities, action, model, creationSelectQ if (model in relationFieldsByModel && relation in relationFieldsByModel[model]) { const relationField = relationFieldsByModel[model][relation]; if (relationField) { - const nestedQueryRelations = { - ...getNestedQueryRelations(args[method][relation], abilities, action === "all" ? "all" : "read", relationField.type), - ...queryRelations[relation]?.select ?? {} - }; + const nestedQueryRelations = deepMerge( + getNestedQueryRelations(args[method][relation], abilities, action === "all" ? "all" : "read", relationField.type), + typeof queryRelations[relation]?.select === "object" ? queryRelations[relation]?.select : {} + ); if (nestedQueryRelations && Object.keys(nestedQueryRelations).length > 0) { queryRelations[relation] = { ...queryRelations[relation] ?? {}, diff --git a/dist/index.mjs b/dist/index.mjs index 4abb97a..917c083 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -1134,6 +1134,26 @@ function convertCreationTreeToSelect(abilities, relationQuery) { return Object.keys(relationResult).length > 0 ? relationResult : relationQuery.action === "create" ? {} : null; } +// src/deepMerge.ts +function isObject(item) { + return item && typeof item === "object" && !Array.isArray(item); +} +function deepMerge(target, ...sources) { + if (!sources.length) return target; + const source = sources.shift(); + if (isObject(target) && isObject(source)) { + for (const key in source) { + if (isObject(source[key])) { + if (!target[key]) Object.assign(target, { [key]: {} }); + deepMerge(target[key], source[key]); + } else { + Object.assign(target, { [key]: source[key] }); + } + } + } + return deepMerge(target, ...sources); +} + // src/applyRuleRelationsQuery.ts function mergeArgsAndRelationQuery(args, relationQuery) { const mask = {}; @@ -1230,10 +1250,10 @@ function getNestedQueryRelations(args, abilities, action, model, creationSelectQ if (model in relationFieldsByModel && relation in relationFieldsByModel[model]) { const relationField = relationFieldsByModel[model][relation]; if (relationField) { - const nestedQueryRelations = { - ...getNestedQueryRelations(args[method][relation], abilities, action === "all" ? "all" : "read", relationField.type), - ...queryRelations[relation]?.select ?? {} - }; + const nestedQueryRelations = deepMerge( + getNestedQueryRelations(args[method][relation], abilities, action === "all" ? "all" : "read", relationField.type), + typeof queryRelations[relation]?.select === "object" ? queryRelations[relation]?.select : {} + ); if (nestedQueryRelations && Object.keys(nestedQueryRelations).length > 0) { queryRelations[relation] = { ...queryRelations[relation] ?? {}, diff --git a/package.json b/package.json index e32794d..71fcead 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prisma-extension-casl", - "version": "1.0.1", + "version": "1.0.2", "description": "Enforce casl abilities on prisma client ", "main": "dist/index.js", "types": "dist/index.d.ts",