如何通过在nest.js中抛出查询来获取

时间:2020-10-06 09:09:24

标签: typescript nestjs typeorm

在我的controller.ts

  @Get()
  getFuga(@Query() query: { place: string }): string {
    return query.place
  }

当我通过query投掷GET

http://localhost:3000/event?place=Tokyo

响应为Tokyo

我想通过类似的查询来获得结果

select * from events where events.place = 'Tokyo'

有什么好的方法可以实现连接DB并引发查询吗?

我是初学者,如果存在一些有用的材料,我可以伸出援手吗?

mysql> select * from events;
+----+--------+------------+---------+--------+-------------+------------+----------+----------+----------------------------+----------------------------+------------+---------+
| id | title  | date       | place   | detail | is_lastdate | begin_time | end_time | label_cd | created_at                 | updated_at                 | deleted_at | user_id |
+----+--------+------------+---------+--------+-------------+------------+----------+----------+----------------------------+----------------------------+------------+---------+
|  1 | mytest | 2000-12-31 | Tottori | test   |           1 | 11:59:00   | 23:40:00 |        4 | 2020-10-04 02:46:05.834987 | 2020-10-04 13:08:29.000000 | NULL       |    NULL |
|  2 | mytest | 2000-12-31 | Tokyo   | test   |           1 | 11:59:00   | 23:40:00 |        4 | 2020-10-04 02:46:05.834987 | 2020-10-04 13:08:19.000000 | NULL       |    NULL |
|  3 | mytest | 2020-07-06 | Tokyo   | test   |           0 | 00:00:00   | 00:00:00 |        0 | 2020-10-04 02:46:05.834987 | 2020-10-04 02:46:05.901008 | NULL       |    NULL |
|  4 | mytest | 2020-07-06 | Tokyo   | test   |           0 | 00:00:00   | 00:00:00 |        0 | 2020-10-04 02:46:05.834987 | 2020-10-04 02:46:05.901008 | NULL       |    NULL |
|  5 | mytest | 2000-12-31 | Tokyo   | NULL   |           0 | 00:00:00   | 00:00:00 |        0 | 2020-10-04 02:46:05.834987 | 2020-10-04 02:46:05.901008 | NULL       |    NULL |
|  6 | mytest | 2000-12-31 | Tokyo   | NULL   |           0 | 00:00:00   | 00:00:00 |        0 | 2020-10-04 02:46:05.834987 | 2020-10-04 02:46:05.901008 | NULL       |    NULL |
|  7 | mytest | 2000-12-31 | Tokyo   | NULL   |           0 | 00:00:00   | 00:00:00 |        0 | 
+----+--------+------------+---------+--------+-------------+------------+----------+----------+----------------------------+----------------------------+------------+---------+

如果有人有意见,请告诉我。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用Typeorm,这是使用最广泛的ORM 在nestjs中连接到各种数据库。 将其集成到项目中后,可以使用Typeorm查询样式的查询生成器或活动记录,以便根据需要获取结果。