带子查询和联接的TypeORM查询

时间:2020-08-05 18:07:21

标签: typeorm

我研究过使用QueryBuilder,但发现在以下情况下很难将SQL查询映射到它。

架构

合作伙伴

  • id
  • 名称

销售

  • row_id
  • 合作伙伴-> partner.id
  • 其他销售数据

sale_event-此表正在跟踪N个工作流事件和销售状态数据。

  • uuid
  • sale_id-> sale.row_id
  • event_workflow
  • event_action
  • 创建

给出合作伙伴ID,我需要获取处于 pending 状态或 sale没有事件的特定工作流程 sale-confirm 的所有订单-确认工作流程。

我有下面的SQL应该可以满足我的需要,但是将其映射到TypeORM的查询生成器有点困难。我遇到的大多数使用子查询的示例都更简单一些,看起来它们也在使用旧版本的TypeORM。

select s.*,
       p.*,
       ev.*
    from sale s
             inner join partner p on s.partner = p.id
             inner join (
        # Get the most recent event in the 'sale-confirm' workflow
        select e.*
            from sale_event e
            where e.sale_id = -1
              and event_workflow = 'sale-confirm'
            order by e.created
            limit 1
    ) ev on ev.sale_id = s.row_id
where ev.event_action = 'pending';

0 个答案:

没有答案