假设我有三张桌子
product (id, name)
customer (id, name)
product_customer (product_id, customer_id)
我已经为product
和customer
GET /products => get all products
GET /products/:id => get details on a specific product
POST /products => add a product
PUT /products/:id => update a product
DELETE /products/:id => delete a product
Same as above for /customers
问题
现在,连接表product_customer
需要URI AND REST约定来根据以下需求检索记录
a)/product_customer
(将通过customer_id
param以获取客户购买的所有产品)
b)/product_customer
(将通过product_id
param以获取购买此产品的所有客户)
我需要一个连接表的REST uri约定才能通过两个参数检索记录,那里有没有标准约定?
编辑示例JSON
{
"products": [
{
"id": 1,
"name": "product 1"
}
],
"customers": [
{
"id": 1,
"name": "john"
},
{
"id": 2,
"name": "jane"
},
]
}
编辑2 - 基于评论的推荐URI
多个(有s)
GET /products
- 列出所有产品
GET /products/1
- 产品ID 1(无客户)等名称等详细信息
奇异
GET /product/1
- 产品详情及其客户
GET /product/1/customers
仅限产品1的客户
答案 0 :(得分:2)
好吧,如果您在模型中创建了Many-to-many个关联,请执行以下操作:
API /模型/ Product.js
module.exports = {
attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true,
unique: true
},
name:'string',
// Add a reference to Customer model
customers: {
collection: 'customer',
via: 'products'
}
}
}
API /模型/ Customer.js
module.exports = {
attributes: {
id: {
type: 'integer',
primaryKey: true,
autoIncrement: true,
unique: true
},
name:'string',
// Add a reference to Product model
products: {
collection: 'product',
via: 'customers'
}
}
}
之后,您可以通过Blueprint API:
申请所有客户 产品 ID 1:
GET /product/1/customers
客户 ID 5的所有产品:
GET /customer/5/products
请注意,该型号名称是产品,客户是单一的 - 没有's',他们的联想是产品和客户!这就是我喜欢的,你可以像你想要的那样命名。