node.js - 需要将 psql 查询转换为 typeorm
问题描述
我需要每天/每周/每月获取数据。所以我使用 date_trunc() 函数来获取这种类型的记录。我进行了 psql 查询,但我需要将其转换为 typeorm 代码,因为我是 typeorm 堆栈的新手。下面是查询
select date_trunc('day', e."createdAt") as production_to_month, count(id) as count from events e
where e."createdAt" between '2021-05-10' and '2021-05-17' and e."type" = 'LOGIN'
group by date_trunc('day', e."createdAt")
order by date_trunc('day', e."createdAt") asc
需要转换这个
解决方案
您需要使用查询生成器来获得所需的结果。
查询将是 -
return await getRepository(Events)
.createQueryBuilder("e")
.select("date_trunc('day',`e`.`createdAt`)","production_to_month")
.addSelect("count(`id`)","count")
.where("`e`.`createdAt` between '2021-05-10' and '2021-05-17'")
.andWhere("`e`.`type` = 'LOGIN'")
.groupBy("date_trunc('day',`e`.`createdAt`)").
.orderBy("date_trunc('day',`e`.`createdAt`)","ASC")
.printSql()
.getRawMany()
推荐阅读
- git - 从我的本地更改中删除文件 (GIT)
- google-api - SSML Speak 标签 begin 属性的绝对值
- julia - 为什么我没有得到与 Complex(::Sym) 匹配的方法?
- spring - Mockito 测试归零
- sql-server - SQL Server 2017 中的 OUTPUT 参数的存储过程返回 null
- java - 如何在 JAVA 中以二维数组功能方式交换元素
- java - 带有 GraphQL 的 Spring Boot - 架构问题
- c - 用数组中的另一个数据序列替换一个数据序列
- javascript - 选项标签 - 在同一选项卡中打开页面
- postman - 如何从邮递员的响应标头中提取特定的字符串值?