我有3个Mysql表,一个带供应商列表的供应商表,一个带这些供应商在其中存储的类别列表的categorys表,category表通过辅助键链接到该供应商表(vendor_id ),我还有一个产品表,其中包含这些类别中的所有产品,并且使用外键(category_id)将其链接到类别表。我如何编写一个NodeJs / Express路由,该路由返回一个如下所示的json对象,
{
"id": 1,
"vendor": "Food place",
"category": "Chicken"[
{
"id": "1,
"name": "Fried chicken",
"price": "100"
},
{
"id": "2,
"name": "Soup Chicken",
"price": "150"
}
}
因此json对象将向供应商返回他们在那里存储的所有类别,并在类别内返回所有属于这些类别的产品
表看起来与此类似 供应商表:
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| category | varchar(255) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
类别表:
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| vendor_id | int(11) | NO | MUL | NULL | |
+-----------+-------------+------+-----+---------+----------------+
产品表:
+-----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| price | decimal(10,0) | NO | | NULL | |
| name | varchar(255) | YES | | NULL | |
| category_id | int(11) | YES | MUL | NULL | |
+-----------------+---------------+------+-----+---------+----------------+
答案 0 :(得分:0)
路线
export const route = (router: any) => {
return router.get('/your_route_here', (req: any, res: any) => {
// do your logic here
});
}
export default route;