From bb933e1921a096d9cd08fba83da783a5bcd36a8b Mon Sep 17 00:00:00 2001 From: dennemark Date: Wed, 27 Nov 2024 11:06:06 +0100 Subject: [PATCH] fix: :bug: deep merge nested rule relations --- src/applyRuleRelationsQuery.ts | 9 +++++---- src/deepMerge.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/deepMerge.ts diff --git a/src/applyRuleRelationsQuery.ts b/src/applyRuleRelationsQuery.ts index 21ea7a3..b2ab1a8 100644 --- a/src/applyRuleRelationsQuery.ts +++ b/src/applyRuleRelationsQuery.ts @@ -3,6 +3,7 @@ import { rulesToAST } from '@casl/ability/extra'; import { createPrismaAbility, PrismaQuery } from '@casl/prisma'; import { Prisma } from '@prisma/client'; import { convertCreationTreeToSelect, CreationTree } from './convertCreationTreeToSelect'; +import { deepMerge } from './deepMerge'; import { getRuleRelationsQuery } from './getRuleRelationsQuery'; import { relationFieldsByModel } from './helpers'; @@ -174,10 +175,10 @@ function getNestedQueryRelations(args: any, abilities: PureAbility 0) { queryRelations[relation] = { ...(queryRelations[relation] ?? {}), diff --git a/src/deepMerge.ts b/src/deepMerge.ts new file mode 100644 index 0000000..9fe801d --- /dev/null +++ b/src/deepMerge.ts @@ -0,0 +1,32 @@ +/** + * Simple object check. + * @param item + * @returns {boolean} + */ +export function isObject(item: any) { + return (item && typeof item === 'object' && !Array.isArray(item)); +} + +/** + * https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge + * Deep merge two objects. + * @param target + * @param ...sources + */ +export function deepMerge(target: any, ...sources: any[]) { + 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); +} \ No newline at end of file