ppetzold / nestjs-paginate

Pagination and filtering helper method for TypeORM repositories or query builders using Nest.js framework :book::paperclip:
MIT License
429 stars 93 forks source link

Nested select when using nested relations #502

Closed allanvobraun closed 1 year ago

allanvobraun commented 1 year ago

The pr #405, implements nested relations. But when using nested relations together with select in a nested entity it not works. example:

export const customerRealtyPaginateConfig: PaginateConfig<RealtyAd> = {
  select: [
    'city.name',
    'city.state.name',
  ],
  relations: {
    city: {
      state: true,
    },
  },
}; 

The generated query fail because of the generated alias: expected alias: entity_city_state.name actual alias: entity_city.state.name

generated query:

SELECT DISTINCT `distinctAlias`.`entity_id` AS `ids_entity_id`, `distinctAlias`.`entity_data_alteracao`
FROM (SELECT `entity`.`id`                                   AS `entity_id`,
             `entity`.`titulo`                               AS `entity_titulo`,
             `entity`.`referencia`                           AS `entity_referencia`,
             `entity`.`ativo`                                AS `entity_ativo`,
             `entity`.`data_cadastro`                        AS `entity_data_cadastro`,
             `entity`.`data_alteracao`                       AS `entity_data_alteracao`,
             `entity`.`area_util`                            AS `entity_area_util`,
             `entity`.`finalidade1`                          AS `entity_finalidade1`,
             `entity`.`finalidade2`                          AS `entity_finalidade2`,
             `entity`.`qtd_quartos`                          AS `entity_qtd_quartos`,
             `entity`.`qtd_vagas_garagem`                    AS `entity_qtd_vagas_garagem`,
             `entity`.`valor_imovel`                         AS `entity_valor_imovel`,
             `entity`.`valor_condominio`                     AS `entity_valor_condominio`,
             `entity_pictures`.`foto`                        AS `entity_pictures_foto`,
             `entity_publications`.`destaque`                AS `entity_publications_destaque`,
             `entity_city`.`nome_cidade`                     AS `entity_city_nome_cidade`,
             `entity_statistics`.`qtd_visualizacao`          AS `entity_statistics_qtd_visualizacao`,
             `entity_statistics`.`qtd_click`                 AS `entity_statistics_qtd_click`,
             `entity_statistics`.`qtd_visualizacao_telefone` AS `entity_statistics_qtd_visualizacao_telefone`,
             `entity_statistics`.`qtd_propostas`             AS `entity_statistics_qtd_propostas`,
             `entity_statistics`.`qtd_whatsapp`              AS `entity_statistics_qtd_whatsapp`,
             `entity_statistics`.`qtd_agendar_visita`        AS `entity_statistics_qtd_agendar_visita`,
             `entity_statistics`.`qtd_atendimento_online`    AS `entity_statistics_qtd_atendimento_online`,
             entity_city_state.name # <-- error here
      FROM `tb_imovel` `entity`
               LEFT JOIN `tb_imovel_foto` `entity_pictures`
                         ON `entity_pictures`.`id_imovel` = `entity`.`id` AND (`entity_pictures`.`ordem` = 1)
               LEFT JOIN `tb_imovel_plano_assinatura_cliente_imovel` `entity_publications`
                         ON `entity_publications`.`id_imovel` = `entity`.`id`
               LEFT JOIN `tb_bairro` `entity_neighborhood` ON `entity_neighborhood`.`id` = `entity`.`id_bairro`
               LEFT JOIN `tb_cidade` `entity_city` ON `entity_city`.`id` = `entity`.`id_cidade`
               LEFT JOIN `tb_uf` `entity_city_state` ON `entity_city_state`.`id` = `entity_city`.`id_uf`
               LEFT JOIN `tb_estatisticas_imovel_geral` `entity_statistics`
                         ON `entity_statistics`.`id_imovel` = `entity`.`id`
               LEFT JOIN `tb_imovel_tipo` `entity_type` ON `entity_type`.`id` = `entity`.`id_imovel_tipo`
      WHERE `entity`.`id_cliente` = 149289
        AND (`entity`.`deleted` = false)
        AND 1 = 1) `distinctAlias`
ORDER BY `distinctAlias`.`entity_data_alteracao` DESC, `entity_id` ASC
LIMIT 20
ppetzold commented 1 year ago

Oh no, thought about asking for coverage on all features with the new option but then.. 🙈

Can you tell if this only affects select?

ppetzold commented 1 year ago

is this still an issue with v6+ ?

allanvobraun commented 1 year ago

I'll test it tomorrow when I get to work.

allanvobraun commented 1 year ago

Doesn't seem to be working, test cases added in a pr

ppetzold commented 1 year ago

alright. thanks for the feedback :) will look into it over the next days

ppetzold commented 1 year ago

fixed with v7.1.1