postgresql - 如何使用 typeorm 在 NestJS 框架上执行原始 SQL 查询
问题描述
我将使用 Typeform 在 NestJS 框架上执行这么长的查询。请让我知道如何执行此查询。
select user.id, user.fullName, (select count(*) sendCnt from chat where senderId = user.id), (select count(*) recvCnt from chat where receiverId = user.id) from users user where user.role = 'Admin'
解决方案
如果您正在使用,您可以使用和使用TypeORM
注入连接来运行原始查询,如TypeORM 文档中所示@InjectConnection()
query
const rawData = await connection.query(`SELECT * FROM USERS`);
假设你使用@nestjs/typeorm
,this.connection
可以通过@InjectConnection()
构造函数中的装饰器获取
@Injectable()
export class FooService {
constructor(@InjectConnection() private readonly connection: Connection) {}
async doSomeQuery() {
return this.connection.query('SELECT * FROM USERS;');
}
}
推荐阅读
- php - Cakephp如何从管理文件夹设置根页面?
- javascript - 为什么我的 formdata POST 没有被 Cors 政策阻止?
- amazon-web-services - AWS - 这可能吗?- 通过 ip 重定向到不同的 index.html 页面-
- javascript - 我怎样才能使这个触摸滑块原子地移动?
- javascript - 单击按钮后如何更新文本区域中的值?
- c++ - 如何以 sublime 文本的特定顺序编译多个 c++ 文件?
- pygame - pygame.sprite 给出错误 RecursionError: 调用 Python 对象时超出最大递归深度
- javascript - 难以让函数在我的 Javascript 类中工作
- swift - Alamofire,用于发布请求的 HTTPheaders [string:any]
- angular - 除非悬停输入框,否则星云聊天 UI 不显示消息