diff --git a/CHANGELOG.md b/CHANGELOG.md index f08597f..4e1efe4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ +## [1.1.1](https://github.com/dennemark/prisma-extension-casl/compare/1.1.0...1.1.1) (2024-11-27) + +### Bug Fixes + +* :bug: deep merge nested rule relations ([bb933e1](https://github.com/dennemark/prisma-extension-casl/commit/bb933e1921a096d9cd08fba83da783a5bcd36a8b)) + ## [1.1.0](https://github.com/dennemark/prisma-extension-casl/compare/1.0.1...1.1.0) (2024-11-13) ### Features diff --git a/dist/index.js b/dist/index.js index 6e1381a..49bd519 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1167,6 +1167,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 = {}; @@ -1263,10 +1283,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 cb687ff..d1dca88 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -1142,6 +1142,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 = {}; @@ -1238,10 +1258,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 6f74b90..89abd5b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prisma-extension-casl", - "version": "1.1.0", + "version": "1.1.1", "description": "Enforce casl abilities on prisma client ", "main": "dist/index.js", "types": "dist/index.d.ts",