Repository: twentyhq/twenty
File: packages/twenty-server/src/engine/twenty-orm/entity-manager/workspace-entity-manager.ts (TypeScript)

Code before:
      )
        .execute()
        .then(() => formattedEntityOrEntities as Entity[])
        .finally(() => queryRunnerForEntityPersistExecutor.release());

      const resultArray = Array.isArray(result) ? result : [result];

      let formattedResult = formatResult<Entity[]>(
        resultArray,
        objectMetadataItem,
        this.internalContext.flatObjectMetadataMaps,
        this.internalContext.flatFieldMetadataMaps,
      );

      const updatedEntities = formattedResult.filter(
        (entity) => beforeUpdateMapById[entity.id],
      );
      const createdEntities = formattedResult.filter(
        (entity) => !beforeUpdateMapById[entity.id],
      );

      this.internalContext.eventEmitterService.emitDatabaseBatchEvent(
        formatTwentyOrmEventToDatabaseBatchEvent({
          action: DatabaseEventAction.UPDATED,
          objectMetadataItem,
          flatFieldMetadataMaps: this.internalContext.flatFieldMetadataMaps,
          workspaceId: this.internalContext.workspaceId,
          entities: updatedEntities,
          beforeEntities: updatedEntities.map(
            (entity) => beforeUpdateMapById[entity.id],
          ),
        }),
      );

      this.internalContext.eventEmitterService.emitDatabaseBatchEvent(
        formatTwentyOrmEventToDatabaseBatchEvent({
          action: DatabaseEventAction.CREATED,
          objectMetadataItem,
          flatFieldMetadataMaps: this.internalContext.flatFieldMetadataMaps,
          workspaceId: this.internalContext.workspaceId,
          entities: createdEntities,
        }),
      );

      const permissionCheckApplies =
        permissionOptionsFromArgs?.shouldBypassPermissionChecks !== true &&
        objectMetadataItem.isSystem !== true;

      if (permissionCheckApplies) {
        formattedResult = this.getFormattedResultWithoutNonReadableFields({
          formattedResult,

Review comment (suggestion):
I'm surprised we are still calling those methods. As discussed already, imho we should have a nice layer above which will leverage query builders. Here we are duplicating things due to typeorm accepting different paths but we've already implemented heavy logic in query builders that's probably not there. Imho we should only modify query builders in your PR and use query builders in the different places where we are using the entity manager directly (which is probably not a lot). I can do that in a followup PR and then we can simply throw if those methods are called

Code after:
        options as SaveOptions | (SaveOptions & { reload: false }),
      )
        .execute()
        .then(() => formattedEntityOrEntities as Entity[])
        .finally(() => queryRunnerForEntityPersistExecutor.release());

      const resultArray = Array.isArray(result) ? result : [result];

      let formattedResult = formatResult<Entity[]>(
        resultArray,
        objectMetadataItem,
        this.internalContext.flatObjectMetadataMaps,
        this.internalContext.flatFieldMetadataMaps,
      );

      const updatedEntities = formattedResult.filter(
        (entity) => beforeUpdateMapById[entity.id],
      );
      const createdEntities = formattedResult.filter(
        (entity) => !beforeUpdateMapById[entity.id],
      );

      this.internalContext.eventEmitterService.emitDatabaseBatchEvent(
        formatTwentyOrmEventToDatabaseBatchEvent({
          action: DatabaseEventAction.UPDATED,
          objectMetadataItem,
          flatFieldMetadataMaps: this.internalContext.flatFieldMetadataMaps,
          workspaceId: this.internalContext.workspaceId,
          recordsAfter: updatedEntities,
          recordsBefore: updatedEntities.map(
            (entity) => beforeUpdateMapById[entity.id],
          ),
        }),
      );

      this.internalContext.eventEmitterService.emitDatabaseBatchEvent(
        formatTwentyOrmEventToDatabaseBatchEvent({
          action: DatabaseEventAction.CREATED,
          objectMetadataItem,
          flatFieldMetadataMaps: this.internalContext.flatFieldMetadataMaps,
          workspaceId: this.internalContext.workspaceId,
          recordsAfter: createdEntities,
        }),
      );

      const permissionCheckApplies =
        permissionOptionsFromArgs?.shouldBypassPermissionChecks !== true &&
        objectMetadataItem.isSystem !== true;

      if (permissionCheckApplies) {
        formattedResult = this.getFormattedResultWithoutNonReadableFields({