简体   繁体   English

API(REST)过滤表名称,复杂查询

[英]API (REST) Filtering table names, complex queries

I am creating API for consuming (for this time) from mobile clients Android and iOS. 我正在创建API(以供这次使用)从移动客户端Android和iOS使用。 Main application is a kind of e-store. 主要应用是一种电子商店。

I have news endpoint, lets consider for example main url is http://consumer.com/api/v0/ 我有news端点,例如考虑主网址为http://consumer.com/api/v0/

Currently I have such endpoints to filter news /news?actual=1&moderation=1 .... 目前,我有这样的端点来过滤新闻/news?actual=1&moderation=1 ....

Simple array of GET arguments, but I have some questions about this. GET参数的简单数组,但是对此我有一些疑问。 The bad thing ( I thinks so) is that filter parameters ( actual,moderation ) are the same as column names in database. 不好的事情(我认为是)是过滤器参数( actual,moderation )与数据库中的列名相同。

  1. Is it bad idea to expose real column names ? 公开真实的列名是个坏主意吗?
  2. How can I solve the problem ? 我该如何解决这个问题? I have one idea is to introduce some middleware (it is possible anyway, but I am using lumen it great possibility) that will translate some fake names for example filter_actual and filter_mooderation into real table names, this will not brake my app now,just add one more layer (power of middlewares). 我有一个主意是要介绍一些中间件(无论如何是有可能的,但是我使用lumen的可能性很大),它将把一些假名(例如filter_actualfilter_mooderation转换为真实的表名,这现在不会filter_mooderation我的应用程序,只需添加一层(中间件的功能)。 Or there is another way to do this. 或有另一种方法可以做到这一点。

But the main problem is that if I need to perform complex query , consider next example. 但是主要的问题是,如果我需要执行复杂的查询,请考虑下一个示例。

Table news has column company_id , company table has column blocked , this column describes if company is blocked or not. 表格newscompany_idcompany表格的列blocked ,此列描述了公司是否被阻止。

I need to get all news by companies that are not blocked, I have perform something like this pseudo query SELECT * FROM news WHERE company_id IN (SELECT id FROM companies WHERE blocked=0) ( I haven't checked but I hope you got an idea) 我需要获取未被阻止的公司的所有新闻,我已经执行了类似以下的伪查询: SELECT * FROM news WHERE company_id IN (SELECT id FROM companies WHERE blocked=0) (我尚未检查,但希望您能理念)

How to craft endpoint filter for this type of query, now I have introduced additional options like non_blocked_companies and pass then in with other filters /news?actual=1&moderation=1&non_blocked_companies=1 and than check if this parameter was passed with others so in code it can soon look like a lot of if statements` specific for each complex query. 如何为这种类型的查询设计端点过滤器,现在我介绍了其他选项,例如non_blocked_companies ,然后与其他过滤器/news?actual=1&moderation=1&non_blocked_companies=1一起传递,然后检查此参数是否与其他参数一起传递,因此在代码中很快就会看起来像很多针对每个复杂查询的if语句。

I have thought about queries like this /news?actual=1&moderation=1&blocked[companies:company_id]=0 I think it is clear. 我曾考虑过类似/news?actual=1&moderation=1&blocked[companies:company_id]=0我认为这很清楚。 I use field from other table ( companies ) an column in news table ( company_id ) which stores id of company. 我使用其他表( companies )中的字段来存储公司ID的news表( company_id )中的一列。

But it looks ugly for me firstly because it is quite complex query in get request, but the major bad side of this for me is that I need to know exactly table name, table columns, it is not the problem now, because this API is used only for mobile clients communication and won't be opened in public, but I want to make reliable APIs anyway 但是首先对我来说这很丑陋,因为它在get请求中是一个非常复杂的查询,但是对我而言,主要的不利方面是我需要确切地知道表名,表列,现在这不是问题,因为此API是仅用于移动客户端通信,不会在公共场合开放,但我还是想制作可靠的API

  1. Is it bad idea as I described formerly ? 如我之前描述的,这是个坏主意吗?
  2. How can I organize my API to be flexible (changes in API should affect frontend as little as possible) 如何组织API以保持灵活性(API的更改应尽可能少地影响前端)

Please, more advanced API developers, suggest what it the best choice in my case. 请更高级的API开发人员,就我的情况提出最佳选择。

Thanks. 谢谢。

Is it bad idea to expose real column names ? 公开真实的列名是个坏主意吗?

Is is not bad Idea. 是不错的主意。 It will be good for developer to understand what exactly happening. 对于开发人员来说,了解确切发生的事情将是一件好事。 I would give proper name rather direct column name. 我会给专有名称而不是直接的列名称。 There are many API from Amazon and google are doing same thing. 来自亚马逊和谷歌的许多API都在做同样的事情。

How can I solve the problem ? 我该如何解决这个问题? I have one idea is to introduce some middleware (it is possible anyway, but I am using lumen it great possibility) that will translate some fake names for example filter_actual and filter_mooderation into real table names, this will not brake my app now,just add one more layer (power of middlewares). 我有一个主意是要介绍一些中间件(无论如何是有可能的,但是我使用lumen的可能性很大),它将把一些假名(例如filter_actual和filter_mooderation)转换为真实的表名,这现在不会停止我的应用程序,只需添加一层(中间件的功能)。 Or there is another way to do this. 或有另一种方法可以做到这一点。

Many times developer don't like to send body in Get response but if you feel it will be more complex and break application. 很多时候,开发人员不喜欢在Get响应中发送正文,但是如果您觉得它会更复杂并破坏应用程序。 I would suggest do that. 我建议这样做。 Here is detail discussion before you make your call HTTP GET with request body 这是在使用请求正文进行调用HTTP GET之前的详细讨论

You can add many validation to make API more strong and handle different request scenario. 您可以添加许多验证以增强API的强度并处理不同的请求方案。

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

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