简体   繁体   English

如何使用获取数据所需的多种方式编写Rest API

[英]How to write a rest api with multiple ways needed to fetch data

I am creating a REST API for a point of sale database at my workplace. 我正在工作场所为销售点数据库创建REST API。

The thing is, depending on the situation I'm attempting to get the same result (an item from the 'Stock' table) with different inputs. 问题是,根据情况,我尝试使用不同的输入获得相同的结果(“库存”表中的一项)。

For instance we have a barcode table that points to a StockID, StockID being the primary key for Stock table rows, we also have a 'part code' for ordering reference, a part code is the code our suppliers use to reference items we hold, and we have a table very similar to the barcode table where a partcode points towards a StockID. 例如,我们有一个指向StockID的条形码表,StockID是库存表行的主键,我们还有一个“零件代码”用于订购参考,零件代码是我们的供应商用来参考我们持有的商品的代码,我们有一个与条形码表非常相似的表,其中零件代码指向StockID。

Problem with that is sometimes there are 2 rows in the PartCode table that have the same part code key but point to two different stock items. 问题有时是PartCode表中有两行具有相同的零件代号键,但指向两个不同的库存物料。 Meaning I cant just have more obvious REST API calls. 这意味着我不能拥有更明显的REST API调用。

Currently I have my URLS like so: 目前,我的网址如下:

example.com/stockid/{123} (Probably the only logical REST call) example.com/stockid/ {123}(可能是唯一的逻辑REST调用)

example.com/barcode/{123} (This takes in a barcode, does one SQL query, gets the actual stock row its correlated and responds with that example.com/barcode/ {123}(这将获取条形码,执行一次SQL查询,获取与其相关的实际库存行,并以此进行响应

example.com/partcode/{123} (This does similar as above, although there are instances where the partcode is related to multiple row results example.com/partcode/ {123}(这与上面类似,尽管在某些情况下,该部分代码与多行结果相关

Now this is the second in depth REST API I've had to create and the first was alot easier because I was building a web app and could follow good conventions. 现在,这是我必须创建的第二个深度REST API,第一个更容易,因为我正在构建Web应用程序,并且可以遵循良好的约定。 But I am at a loss on how to start writing this REST API properly, all information I've found seems like it would work effectively or be clean and efficient for a code base and I would like to do this properly and keep it as maintainable as possible. 但是我对如何正确地开始编写此REST API感到迷茫,我发现的所有信息似乎对于代码库来说都是有效的,或者是干净高效的,我想适当地做到这一点并保持可维护性。尽可能。

Things to note 注意事项

  • I have no capacity to change the structure of the database itself 我没有能力更改数据库本身的结构
  • I am using NodeJS + ExpressJS as a backend server for writing this REST API 我正在使用NodeJS + ExpressJS作为编写此REST API的后端服务器
  • I am happy to start fresh with whatever would seem to be the most practical and logical way to write this 我很高兴从似乎是最实际,最合逻辑的方式开始撰写本文
  • I am stuck because I don't want to have a huge amount of ambiguous and otherwise useless API URLs, I just want a proper way to reference the Stock table given the different ways it needs to be searched 我被困住了,因为我不想有大量的模棱两可或无用的API URL,我只是想鉴于需要使用不同的搜索方式,以正确的方式引用Stock表
  • The database is a Microsoft SQL Server 2014 database 该数据库是Microsoft SQL Server 2014数据库

My suggestion would try to read a bit about request , response , router of expressjs from expressjs official. 我的建议是尝试从expressjs官方那里阅读一下有关expressjs的requestresponserouter的信息。

I have a CLI to start REST api named Spready with good practice means proper way how should start. 我有一个CLI,可以通过良好的实践来启动名为Spready的 REST api,这意味着应该如何正确启动。 I can recommend that and Database, you can find nodeJS ORM for MSSQL. 我可以建议您在数据库中找到MSSQL的nodeJS ORM。

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

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