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

Support for VirtualColumn with createQueryBuilder and paginate #352

Open
jy836979 opened this issue Aug 8, 2024 · 0 comments
Open

Support for VirtualColumn with createQueryBuilder and paginate #352

jy836979 opened this issue Aug 8, 2024 · 0 comments

Comments

@jy836979
Copy link

jy836979 commented Aug 8, 2024

I’m using TypeORM with NestJS and have encountered an issue when trying to use VirtualColumn with createQueryBuilder and paginate. The VirtualColumn fields are not recognized or included in the query results when using createQueryBuilder. Here is an example of my setup and the issue I’m facing:

Entity Definitions

@Entity('MonthlyBill')
@UseDto(MonthlyBillDto)
export class MonthlyBillEntity extends AbstractEntity<MonthlyBillDto> {

@Entity('Users')
@UseDto(UsersDto)
export class Users extends AbstractEntity<UsersDto> {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  group: string;

  @VirtualColumn({
    query: (alias) => `(SELECT COUNT(id) FROM Users WHERE group = ${alias}.group)`,
  })
  userCount: number;
}

Query Builder and Pagination

const pageOptionsDto = { q: 'GROUP_A' };

const query = this.usersRepository.createQueryBuilder('users');
const [items, pageMetaDto] = await query.paginate(pageOptionsDto);

Issue

When using the above setup, the VirtualColumn fields userCount are not included in the query results. This issue occurs because createQueryBuilder does not recognize or include VirtualColumn fields in the results.

Request

Is there a recommended approach to use VirtualColumn with createQueryBuilder and paginate in TypeORM? If not, could support for this feature be added? It would be very helpful to be able to use VirtualColumn seamlessly with createQueryBuilder and pagination.

Thank you for your assistance.

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