fix: ignore metadata when computing relationships from payload (#8895)

This commit is contained in:
Harminder Virk
2024-08-30 13:32:25 +05:30
committed by GitHub
parent 170429a9b3
commit 9b6c2e5efa
2 changed files with 20 additions and 4 deletions

View File

@@ -16,6 +16,12 @@ describe("getSelectsAndRelationsFromObjectArray", function () {
attr_null: null,
attr_undefined: undefined,
},
metadata: {
is_test: true,
nested_object: {
deeply_nested: true,
},
},
attr_array: [
{
attr_object: {
@@ -43,6 +49,8 @@ describe("getSelectsAndRelationsFromObjectArray", function () {
"attr_object.attr_boolean",
"attr_object.attr_null",
"attr_object.attr_undefined",
"metadata.is_test",
"metadata.nested_object.deeply_nested",
"attr_array.attr_object.attr_string",
"attr_array.attr_object.attr_boolean",
"attr_array.attr_object.attr_null",

View File

@@ -1,6 +1,8 @@
import { deduplicate } from "./deduplicate"
import { isObject } from "./is-object"
const KEYS_THAT_ARE_NOT_RELATIONS = ["metadata"]
export function getSelectsAndRelationsFromObjectArray(
dataArray: object[],
options: { objectFields: string[] } = {
@@ -17,23 +19,29 @@ export function getSelectsAndRelationsFromObjectArray(
for (const data of dataArray) {
for (const [key, value] of Object.entries(data)) {
if (isObject(value) && !options.objectFields.includes(key)) {
relations.push(setKey(key, prefix))
const res = getSelectsAndRelationsFromObjectArray(
[value],
options,
setKey(key, prefix)
)
selects.push(...res.selects)
relations.push(...res.relations)
if (!KEYS_THAT_ARE_NOT_RELATIONS.includes(key)) {
relations.push(setKey(key, prefix))
relations.push(...res.relations)
}
} else if (Array.isArray(value)) {
relations.push(setKey(key, prefix))
const res = getSelectsAndRelationsFromObjectArray(
value,
options,
setKey(key, prefix)
)
selects.push(...res.selects)
relations.push(...res.relations)
if (!KEYS_THAT_ARE_NOT_RELATIONS.includes(key)) {
relations.push(setKey(key, prefix))
relations.push(...res.relations)
}
} else {
selects.push(setKey(key, prefix))
}