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

Code before:

    return this.userRoleService.getRoleIdForUserWorkspace({
      userWorkspaceId: authContext.userWorkspaceId,
      workspaceId,
    });
  }

  private async prepareExtendedQueryRunnerContextWithGlobalDatasource(
    authContext: WorkspaceAuthContext,
    queryRunnerContext: CommonBaseQueryRunnerContext,
  ): Promise<Omit<CommonExtendedQueryRunnerContext, 'commonQueryParser'>> {
    const workspaceId = authContext.workspace.id;

    const roleId = await this.getRoleIdOrThrow(authContext, workspaceId);

    const rolePermissionConfig: RolePermissionConfig = {
      intersectionOf: [roleId],
    };

    const isReadOnReplicaEnabled =
      await this.featureFlagService.isFeatureEnabled(
        FeatureFlagKey.IS_READ_ON_REPLICA_ENABLED,
        workspaceId,
      );

    const repository =
      this.isReadOnly && isReadOnReplicaEnabled
        ? await this.globalWorkspaceOrmManager.getReadOnlyRepository(
            workspaceId,
            queryRunnerContext.flatObjectMetadata.nameSingular,
            rolePermissionConfig,
          )
        : await this.globalWorkspaceOrmManager.getRepository(
            workspaceId,
            queryRunnerContext.flatObjectMetadata.nameSingular,
            rolePermissionConfig,
          );

    const globalWorkspaceDataSource =
      this.isReadOnly && isReadOnReplicaEnabled
        ? await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSourceReplica()
        : await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();

    return {
      ...queryRunnerContext,
      authContext,
      workspaceDataSource: globalWorkspaceDataSource,
      rolePermissionConfig,
      repository,
    };
  }

Review comment (suggestion):
Not a blocker and not on you but I feel like preparing the repository early here does not bring much value, especially if you pass your datasource as a parameter as well.
I feel getReadRepository method from the orm manager is not very useful, it's just a shortcut to `this.getGlobalDatasource().getRepository()`.
Maybe something to think about later!

Code after:

    return this.userRoleService.getRoleIdForUserWorkspace({
      userWorkspaceId: authContext.userWorkspaceId,
      workspaceId,
    });
  }

  private async prepareExtendedQueryRunnerContextWithGlobalDatasource(
    authContext: WorkspaceAuthContext,
    queryRunnerContext: CommonBaseQueryRunnerContext,
  ): Promise<Omit<CommonExtendedQueryRunnerContext, 'commonQueryParser'>> {
    const workspaceId = authContext.workspace.id;

    const roleId = await this.getRoleIdOrThrow(authContext, workspaceId);

    const rolePermissionConfig: RolePermissionConfig = {
      intersectionOf: [roleId],
    };

    const isReadOnReplicaEnabled =
      await this.featureFlagService.isFeatureEnabled(
        FeatureFlagKey.IS_READ_ON_REPLICA_ENABLED,
        workspaceId,
      );

    const globalWorkspaceDataSource =
      this.isReadOnly && isReadOnReplicaEnabled
        ? await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSourceReplica()
        : await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();

    const repository = globalWorkspaceDataSource.getRepository(
      queryRunnerContext.flatObjectMetadata.nameSingular,
      rolePermissionConfig,
    );

    return {
      ...queryRunnerContext,
      authContext,
      workspaceDataSource: globalWorkspaceDataSource,
      rolePermissionConfig,
      repository,
    };
  }

  private async throttleQueryExecution(authContext: WorkspaceAuthContext) {
    try {
      if (!isDefined(authContext.apiKey)) return;

      const workspaceId = authContext.workspace.id;

      const shortConfig = {