【问题标题】:REST uri naming conventions for junction tables连接表的 REST uri 命名约定
【发布时间】:2015-05-29 23:52:44
【问题描述】:

假设我有三张桌子

product (id, name)
customer (id, name)
product_customer (product_id, customer_id)

我已经关注 productcustomer 的 REST 服务 (URI)

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 参数以获取客户购买的所有产品)

b) /product_customer(将传递product_id 参数以获取所有购买此产品的客户)

我需要一个用于连接表的 REST uri 约定,以便能够通过两个参数检索记录,是否有任何标准约定?

编辑示例 JSON

{
  "products": [
    {
      "id": 1,
      "name": "product 1"
    }
  ],
  "customers": [
    {
      "id": 1,
      "name": "john"
    },
    {
      "id": 2,
      "name": "jane"
    },        
  ]
}

编辑 2 - 基于 cmets 的建议 URI

复数(带 s)

GET /products - 列出所有产品

GET /products/1 - 产品 ID 1(无客户)的名称等详细信息

单数

GET /product/1 - 产品及其客户的详细信息

GET /product/1/customers 仅限产品 1 的客户

【问题讨论】:

  • 单数或复数形式取决于模型的名称。如果型号名称是product,那么request是GET /product/1等等。

标签: node.js rest express sails.js


【解决方案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

customer ID 5 的所有产品

GET /customer/5/products

请注意,型号名称是产品,客户是单数 - 没有“s”,它们的关联是产品和客户!这是我喜欢的方式,你可以随意命名。

【讨论】:

  • 很好,我没有使用 Sails.js,但想在我的自定义应用程序中使用它的约定,所以GET /product/1/customers 表示获取有关产品 id 1 的“详细信息”和所有客户的列表?不仅仅是客户,产品的细节也是如此。请确认
  • 能否请您帮忙处理一下我的 JSON 输出,我的格式是 { "product": [ {"id": 1, "name": "prod1"} ], "customers": [ {"id": 1, "name": "john"}, {"id": 2, "name": "Doe"} ]] 产品和客户处于同一级别,或者我需要将客户嵌套在产品中吗?编辑我的问题以提高知名度
  • 它会将所有相关客户连同他们的所有文件一起返回给该产品。如果您想在一个请求中获得产品和客户,那么只需GET /product/1
  • 我实际上GET /product/1 仅用于产品详细信息(没有客户),我还有另一个要求是返回产品详细信息 + 客户的详细信息。我想执行标准,是否有解决方案,或者我应该要求他们打两个电话?
  • 那个 JSON 是干什么用的?如果您的问题是 SAILS 返回的内容 - 现在就是答案。正确返回的数据类似于:{ "customers": [ { "id": 1, "name": "John", "createdAt": "2015-05-28T09:50:51.000Z", "updatedAt": "2015-05-28T09:50:51.000Z" }, { "id": 2, "name": "Doe", "createdAt": "2015-05-29T09:57:51.000Z", "updatedAt": "2015-05-29T09:57:51.000Z" } ], "id": 1, "name": "prod1", "createdAt": "2015-05-15T10:03:01.000Z", "updatedAt": "2015-05-26T07:17:14.000Z" }
猜你喜欢
  • 2011-03-26
  • 1970-01-01
  • 2010-12-18
  • 2022-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多