联结表的 REST uri 命名约定
REST uri naming conventions for junction tables
假设我有三个 tables
product (id, name)
customer (id, name)
product_customer (product_id, customer_id)
我已经关注 product
和 customer
的 REST 服务 (URI'S)
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
问题
现在加入 table product_customer
需要一个 URI 和 REST 约定来根据以下需要检索记录
a) /product_customer
(将传递customer_id
参数获取客户购买的所有产品)
b) /product_customer
(将通过 product_id
参数获取所有购买此产品的客户)
我需要一个用于连接 tables 的 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 的客户
好吧,如果您在模型中创建了 Many-to-many 个关联,例如:
api/models/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/models/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',它们的关联是产品和客户!我喜欢这样,你想怎么命名就怎么命名。
假设我有三个 tables
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
问题
现在加入 table product_customer
需要一个 URI 和 REST 约定来根据以下需要检索记录
a) /product_customer
(将传递customer_id
参数获取客户购买的所有产品)
b) /product_customer
(将通过 product_id
参数获取所有购买此产品的客户)
我需要一个用于连接 tables 的 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 的客户
好吧,如果您在模型中创建了 Many-to-many 个关联,例如:
api/models/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/models/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',它们的关联是产品和客户!我喜欢这样,你想怎么命名就怎么命名。