Repository: twentyhq/twenty
File: packages/twenty-server/src/engine/api/common/common-query-runners/common-create-many-query-runner/common-create-many-query-runner.service.ts (TypeScript)

Code before:
    flatObjectMetadata,
    flatObjectMetadataMaps,
    flatFieldMetadataMaps,
    repository,
    selectedFieldsResult,
  }: {
    objectRecords: InsertResult;
    flatObjectMetadata: FlatObjectMetadata;
    flatObjectMetadataMaps: FlatEntityMaps<FlatObjectMetadata>;
    flatFieldMetadataMaps: FlatEntityMaps<FlatFieldMetadata>;
    repository: WorkspaceRepository<ObjectLiteral>;
    selectedFieldsResult: CommonSelectedFieldsResult;
  }): Promise<ObjectRecord[]> {
    const queryBuilder = repository.createQueryBuilder(
      flatObjectMetadata.nameSingular,
    );

    const columnsToSelect = buildColumnsToSelect({
      select: selectedFieldsResult.select,
      relations: selectedFieldsResult.relations,
      flatObjectMetadata,
      flatObjectMetadataMaps,
      flatFieldMetadataMaps,
    });

    const orderedIds = objectRecords.generatedMaps.map((record) => record.id);

    const upsertedRecords = await queryBuilder
      .setFindOptions({
        select: columnsToSelect,
      })
      .where({
        id: In(orderedIds),
      })
      .withDeleted()
      .take(QUERY_MAX_RECORDS)
      .getMany();

    // Preserve original input order by sorting results to match orderedIds
    const recordsById = new Map<string, ObjectRecord>(
      upsertedRecords.map((record) => [record.id, record as ObjectRecord]),
    );

    return orderedIds.map((id) => {
      const record = recordsById.get(id);

      if (!record) {
        throw new Error(`Record with id ${id} not found after upsert`);
      }

      return record;

Review comment (suggestion):
i'm confused on how this will preserver the order in the input: objectRecords does not seem to be the input. I wonder if the sort method should be done in a parent method that stil has access to the input

Code after:
    flatObjectMetadata,
    flatObjectMetadataMaps,
    flatFieldMetadataMaps,
    repository,
    selectedFieldsResult,
  }: {
    objectRecords: InsertResult;
    flatObjectMetadata: FlatObjectMetadata;
    flatObjectMetadataMaps: FlatEntityMaps<FlatObjectMetadata>;
    flatFieldMetadataMaps: FlatEntityMaps<FlatFieldMetadata>;
    repository: WorkspaceRepository<ObjectLiteral>;
    selectedFieldsResult: CommonSelectedFieldsResult;
  }): Promise<ObjectRecord[]> {
    const queryBuilder = repository.createQueryBuilder(
      flatObjectMetadata.nameSingular,
    );

    const columnsToSelect = buildColumnsToSelect({
      select: selectedFieldsResult.select,
      relations: selectedFieldsResult.relations,
      flatObjectMetadata,
      flatObjectMetadataMaps,
      flatFieldMetadataMaps,
    });

    const orderedIds = objectRecords.generatedMaps.map((record) => record.id);

    const upsertedRecords = await queryBuilder
      .setFindOptions({
        select: columnsToSelect,
      })
      .where({
        id: In(orderedIds),
      })
      .withDeleted()
      .take(QUERY_MAX_RECORDS)
      .getMany();

    const orderIndex = new Map(orderedIds.map((id, index) => [id, index]));

    upsertedRecords.sort(
      (a, b) => (orderIndex.get(a.id) ?? 0) - (orderIndex.get(b.id) ?? 0),
    );

    return upsertedRecords as ObjectRecord[];
  }

  async processQueryResult(
    queryResult: ObjectRecord[],
    flatObjectMetadata: FlatObjectMetadata,
    flatObjectMetadataMaps: FlatEntityMaps<FlatObjectMetadata>,