Repository: directus/directus
File: api/src/database/run-ast/utils/merge-with-parent-items.ts (TypeScript)

Code before:
import { useEnv } from '@directus/env';
import type { Item, SchemaOverview } from '@directus/types';
import { toArray } from '@directus/utils';
import { clone, isArray } from 'lodash-es';
import type { NestedCollectionNode } from '../../../types/ast.js';

export function mergeWithParentItems(
	schema: SchemaOverview,
	nestedItem: Item | Item[],
	parentItem: Item | Item[],
	nestedNode: NestedCollectionNode,
	fieldAllowed: boolean | boolean[],
) {
	const env = useEnv();
	const nestedItems = toArray(nestedItem);
	const parentItems = clone(toArray(parentItem));

	if (nestedNode.type === 'm2o') {
		const parentsByForeignKey = new Map();

		parentItems.forEach((parentItem: (typeof parentItems)[number]) => {
			const relationKey = parentItem[nestedNode.relation.field];

			if (!parentsByForeignKey.has(relationKey)) {
				parentsByForeignKey.set(relationKey, []);
			}

			parentItem[nestedNode.fieldKey] = null;
			parentsByForeignKey.get(relationKey).push(parentItem);
		});

		const nestPrimaryKeyField = schema.collections[nestedNode.relation.related_collection!]!.primary;

		for (const nestedItem of nestedItems) {
			const nestedPK = nestedItem[nestPrimaryKeyField];

			for (const parentItem of parentsByForeignKey.get(nestedPK)) {
				parentItem[nestedNode.fieldKey] = nestedItem;
			}
		}
	} else if (nestedNode.type === 'o2m') {
		const parentCollectionName = nestedNode.relation.related_collection;
		const parentPrimaryKeyField = schema.collections[parentCollectionName!]!.primary;
		const parentRelationField = nestedNode.fieldKey;
		const nestedParentKeyField = nestedNode.relation.field;


Review comment (suggestion):
```suggestion
		parentItems.forEach((parentItem: Item) => {
```

Code after:
import { useEnv } from '@directus/env';
import type { Item, SchemaOverview, PrimaryKey } from '@directus/types';
import { toArray } from '@directus/utils';
import { clone, isArray } from 'lodash-es';
import type { NestedCollectionNode } from '../../../types/ast.js';

export function mergeWithParentItems(
	schema: SchemaOverview,
	nestedItem: Item | Item[],
	parentItem: Item | Item[],
	nestedNode: NestedCollectionNode,
	fieldAllowed: boolean | boolean[],
) {
	const env = useEnv();
	const nestedItems = toArray(nestedItem);
	const parentItems = clone(toArray(parentItem));

	if (nestedNode.type === 'm2o') {
		const parentsByForeignKey = new Map<PrimaryKey, Item[]>();

		parentItems.forEach((parentItem: Item) => {
			const relationKey = parentItem[nestedNode.relation.field];

			if (!parentsByForeignKey.has(relationKey)) {
				parentsByForeignKey.set(relationKey, []);
			}

			parentItem[nestedNode.fieldKey] = null;
			parentsByForeignKey.get(relationKey)!.push(parentItem);
		});

		const nestPrimaryKeyField = schema.collections[nestedNode.relation.related_collection!]!.primary;

		for (const nestedItem of nestedItems) {
			const nestedPK = nestedItem[nestPrimaryKeyField];

			for (const parentItem of parentsByForeignKey.get(nestedPK)!) {
				parentItem[nestedNode.fieldKey] = nestedItem;
			}
		}
	} else if (nestedNode.type === 'o2m') {
		const parentCollectionName = nestedNode.relation.related_collection;
		const parentPrimaryKeyField = schema.collections[parentCollectionName!]!.primary;
		const parentRelationField = nestedNode.fieldKey;
		const nestedParentKeyField = nestedNode.relation.field;