Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial loading of * with LoadStrategy.JOINED #3868

Closed
skapoor8 opened this issue Dec 23, 2022 · 0 comments
Closed

Partial loading of * with LoadStrategy.JOINED #3868

skapoor8 opened this issue Dec 23, 2022 · 0 comments

Comments

@skapoor8
Copy link

skapoor8 commented Dec 23, 2022

Describe the bug
I'm running the same function (findAll) with different strategies and getting different results.

My entities:

/** 
* Elist Entity
*/
import {
  Collection,
  Entity,
  ManyToOne,
  PrimaryKey,
  Property,
  ref,
  Ref,
  Unique,
  wrap,
} from '@mikro-orm/core';
import { UuidType } from '../custom-types';

@Entity({ tableName: 'elists' })
@Unique({ properties: ['elistName', 'owner'] })
export class ElistEntity {
  @PrimaryKey({ type: UuidType, autoincrement: false })
  public id!: string;

  @Property({ fieldName: 'elist_name', length: 255 })
  public elistName!: string;

  @Property({ columnType: 'JSON', nullable: true })
  public settings?: object;

  @Property({
    fieldName: 'default_settings',
    columnType: 'JSON',
    nullable: true,
  })
  public defaultSettings?: object;

  @ManyToOne({
    entity: () => UserEntity,
    ref: true,
    fieldName: 'owner_id',
    type: UuidType,
  })
  owner: Ref<UserEntity>;

  constructor(elist: Partial<IElist>) {
    const ownerId = elist.ownerId;

    if (ownerId) {
      delete elist.ownerId;
      this.owner = ref(UserEntity, ownerId);
    }

    Object.assign(this, elist);
  }

  toJSON(...args: any[]): Record<string, any> {
    const obj: Partial<IElist> & { owner?: UserEntity } = {};
    Object.assign(obj, this);

    if (!this.owner.isInitialized()) {
      obj.ownerId = this.owner.id;
      delete obj.owner;
    }

    return obj;
  }
}

/**
* UserEntity
*/
import { IUser } from '@azure-function-api-mysql/interfaces';

@Entity({ tableName: 'users' })
export class UserEntity {
  @PrimaryKey({ type: UuidType, autoincrement: false })
  public id: string = v4(); // auto-generate uuid

  @Property({ fieldName: 'first_name', length: 100, nullable: true })
  public firstName?: string;

  @Property({ fieldName: 'last_name', length: 100, nullable: true })
  public lastName?: string;

  @Property({ fieldName: 'phone_number', length: 20, nullable: true })
  public phoneNumber?: string;

  @Property({ fieldName: 'phone_country_code', length: 10, nullable: true })
  public countryCode?: string;

  @Property({ length: 255 })
  @Unique()
  @Index()
  public email: string;

  constructor(user: Partial<IUser>) {
    Object.assign(this, user);
  }
}

The query:

/** from nest.js service */
import { LoadStrategy, MikroORM, wrap } from '@mikro-orm/core';
import { EntityRepository, SqlEntityManager } from '@mikro-orm/mysql';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@mikro-orm/nestjs';
import { ElistEntity, UserEntity } from '../entities';
import {
  IElist,
  IElistWithOwnerInfoDTO,
  IUser,
} from '@azure-function-api-mysql/interfaces';

@Injectable()
export class ElistService {
  constructor(
    // private readonly mikro: MikroORM,
    // private readonly em: SqlEntityManager,
    @InjectRepository(ElistEntity)
    private readonly er: EntityRepository<ElistEntity>
  ) {}

  public async findAll(): Promise<IElistWithOwnerInfoDTO[]> {
    return await this.er.findAll({
      populate: ['owner'],
      fields: ['*', 'owner.id', 'owner.firstName', 'owner.lastName'],
      strategy: LoadStrategy.JOINED,
    });
  }
}

If I run use findAll as shown above, the following query is fired off:

select `e0`.`id`, `o1`.`id` as `o1__id`, `o1`.`first_name` as `o1__first_name`, `o1`.`last_name` as `o1__last_name` from `elists` as `e0` left join `users` as `o1` on `e0`.`owner_id` = `o1`.`id` [took 1 ms]

As you can see in the query, only id is selected from elists table, even though I've specified '*' in fields.

If I change strategy to LoadStrategy.SELECT, then two select queries are fired as expected. However, unexpectedly, a different set of fields is selected (query shown below):

select `e0`.`id`, `e0`.*, `e0`.`owner_id` from `elists` as `e0` [took 2 ms]
[query] select `u0`.`id`, `u0`.`first_name`, `u0`.`last_name` from `users` as `u0` where `u0`.`id` in (X'66c41ebc7bf811ed88765b0e34d8a2a2', X'66c425927bf811ed88765b0e34d8a2a2', X'66c4289e7bf811ed88765b0e34d8a2a2', X'66c42a067bf811ed88765b0e34d8a2a2') order by `u0`.`id` asc 

Stack trace
None

To Reproduce
Steps to reproduce the behavior:

  1. Create the above entities, create db and seed with some data (one row in each table should do)
  2. Run EntityRepository.findAll or EntityManager.findAll as shown above with the 2 different strategies

Expected behavior
I expected the only difference between the two strategies to be whether multiple selects run or if a join statement is used. Not the fields that are selected. I.e. I was expecting the LoadStrategy.JOINED query to have the same selected fields as LoadStrategy.SELECT.

Additional context
I'm using nest-js.

Versions

  1. mikro-orm: ^5.6.0
  2. nest-js: ^8.0.0
  3. ts: ~4.8.2
  4. mysql2: ^2.3.3
@B4nan B4nan changed the title EntityRepository.findAll has inconsistent behavior with LoadStrategy.JOINED Partial loading of * with LoadStrategy.JOINED Dec 25, 2022
@B4nan B4nan closed this as completed in 7781f84 Dec 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant