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

Code before:
			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;

		const parentsByPrimaryKey = new Map();

		parentItems.forEach((parentItem: (typeof parentItems)[number]) => {
			if (!parentItem[parentRelationField]) parentItem[parentRelationField] = [];

			const parentPrimaryKey = parentItem[parentPrimaryKeyField];

			if (parentsByPrimaryKey.has(parentPrimaryKey)) {
				throw new Error(
					`Duplicate parent primary key '${parentPrimaryKey}' of '${parentCollectionName}' when merging o2m nested items`,
				);
			}

			parentsByPrimaryKey.set(parentPrimaryKey, parentItem);
		});

		const toAddToAllParents: typeof nestedItems = [];

		nestedItems.forEach((nestedItem) => {
			if (nestedItem === null) return;

			if (Array.isArray(nestedItem[nestedParentKeyField])) {
				toAddToAllParents.push(nestedItem); // TODO explain this odd case
				return; // Avoids adding the nestedItem twice
			}

			const parentPrimaryKey =
				nestedItem[nestedParentKeyField]?.[parentPrimaryKeyField] ?? nestedItem[nestedParentKeyField];

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

Code after:
			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;

		const parentsByPrimaryKey = new Map<PrimaryKey, Item>();

		parentItems.forEach((parentItem: Item) => {
			if (!parentItem[parentRelationField]) parentItem[parentRelationField] = [];

			const parentPrimaryKey = parentItem[parentPrimaryKeyField];

			if (parentsByPrimaryKey.has(parentPrimaryKey)) {
				throw new Error(
					`Duplicate parent primary key '${parentPrimaryKey}' of '${parentCollectionName}' when merging o2m nested items`,
				);
			}

			parentsByPrimaryKey.set(parentPrimaryKey, parentItem);
		});

		const toAddToAllParents: Item[] = [];

		nestedItems.forEach((nestedItem) => {
			if (nestedItem === null) return;

			if (Array.isArray(nestedItem[nestedParentKeyField])) {
				toAddToAllParents.push(nestedItem); // TODO explain this odd case
				return; // Avoids adding the nestedItem twice
			}

			const parentPrimaryKey =
				nestedItem[nestedParentKeyField]?.[parentPrimaryKeyField] ?? nestedItem[nestedParentKeyField];