简体   繁体   English

我应该使用 PUT 还是 POST 向现有项目添加新的子项目?

[英]Should I use PUT or POST to add a new subitem to an existing item?

Suppose I have an item (model) which has fields that are arrays, for example:假设我有一个项目(模型),其字段为 arrays,例如:

const item = {
  name: "",
  prices: [{somePriceSpecificObject}],
}

When I want to add a new price to that item prices array, which method is better to be used (in terms of quality I guess), PUT or POST?当我想向该商品prices数组添加新价格时,使用哪种方法更好(我猜就质量而言),PUT 还是 POST?

Right now I am using PUT because by creating a new price for prices array I am editing the item , but I am now making functionality that will let me edit the existing entries, and it naturally goes into PUT aswell, which got me thinking about this issue.现在我正在使用PUT ,因为通过为prices数组创建新价格我正在编辑item ,但我现在正在制作可以让我编辑现有条目的功能,它自然也会进入PUT ,这让我想到了这个问题。

Because, it also kinda makes sense to use POST , because price relative to the item is a new thing, but the item relative to me with the new price is still the item, just with an edited field (prices).因为,使用POST也有点意义,因为相对于项目的价格是一个新事物,但相对于我的新价格的项目仍然是项目,只是带有一个已编辑的字段(价格)。

Now one solution to my dilemma might be to make a separate model for prices ?现在解决我的困境的一种方法可能是为prices制作一个单独的 model ? But I never did that, because, a given item in my specific conditions, will never have more than a handful of prices, and it will make me make more queries to the database (I am not concerned about performance, but still, just looking for best practices).但是我从来没有这样做过,因为在我的特定条件下,给定的项目永远不会有超过少数的价格,而且它会让我对数据库进行更多的查询(我不关心性能,但仍然,只是看最佳实践)。

So which route do you think I should go?那么你认为我应该走哪条路线 go?

EDIT: My api endpoints look like this at the moment:编辑:我的 api 端点目前看起来像这样:

post("/one", POST.oneItem);
put("/addprice", PUT.addPriceToItem);

put("/editone/:id", PUT.EDIT.item);
put("/editone/:id/price/:priceId", PUT.EDIT.price);

Notice I had to make a nested EDIT object in my PUT object (that provides the handling functions), to be able to easily distinguish between adding a price and editing a price, so that got me into thinking about all of that.请注意,我必须在我的 PUT object(提供处理功能)中创建一个嵌套的 EDIT object,以便能够轻松区分添加价格和编辑价格,这让我开始思考所有这些。

I have seen in most cases of production code there is never a delete query being run as the schema has a soft delete key which gets turned when wanting to delete a particular row/document.我已经看到在大多数生产代码情况下,从来没有运行过删除查询,因为架构有一个软删除键,当想要删除特定的行/文档时,它会被转动。 Hence it is always using PUT as a best practice even for delete.因此,即使是删除,它也始终使用 PUT 作为最佳实践。 Any changes to an existing document need to be a PUT because it will help you in improving performance unless the document is completely different from the one before.对现有文档的任何更改都必须是 PUT,因为它将帮助您提高性能,除非该文档与之前的文档完全不同。 It even becomes easier while caching as one needs to update the cache and not add another one in and remove some other one (in case the cache becomes full at that point).在缓存时它甚至变得更容易,因为需要更新缓存而不是添加另一个缓存并删除另一个缓存(以防缓存在此时已满)。 In the end, if you intend to keep prices as an array then I guess PUT is a better choice.最后,如果您打算将价格保持为数组,那么我认为 PUT 是一个更好的选择。

"PUT /uri" creates/updates the thing at "/uri". “PUT /uri”在“/uri”创建/更新事物。 Your example where the URI contains "/addprice" is IMHO a misuse of PUT.您的 URI 包含“/addprice”的示例是恕我直言,是对 PUT 的滥用。

If you have a collection X and want to add an item Y, use "PUT /X/Y".如果您有一个集合 X 并想添加一个项目 Y,请使用“PUT /X/Y”。 If you want the server to name the item, use "POST /x".如果您希望服务器为项目命名,请使用“POST /x”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM