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;

Review comment (suggestion):
I think it wouldn't hurt to be explicit regarding the datatypes of the maps, to get type safety and directly see what map will be build. It would be this:
```ts
import type { PrimaryKey } from '@directus/types';

const parentsByForeignKey: Map<PrimaryKey, Item[]> = new Map();
```

similarly for the other new map `parentsByPrimaryKey`. 

```ts
const parentsByPrimaryKey: Map<PrimaryKey, Item> = new Map();
```

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;