如何使用REST API实现复杂的查询? [英] How to implement complex queries with a REST api?
问题描述
我正在使用ember-data构建EmberJS应用.
I'm building an EmberJS app using ember-data.
我的应用程序中的某些功能需要非常复杂的查询.
Some of the functionality in my app requires quite complex queries.
作为一个例子,假设我有三个实体-学生,老师和班级.如果我想获得所有1993年以前出生的,由X老师上课的学生的清单,我该如何使用RESTful api做到这一点?在普通的SQL中,这很容易,但是我不确定将其实现到我的API中的最佳实践.
As an example, let's say I have three entities - students, teachers and classes. If I wanted to get a list of all the students born before 1993 that are taking classes taught by teacher X, how can I do that with a RESTful api? In plain SQL it's easy enough, but I'm unsure of the best practice for implementing this into my API.
我需要在基本的REST API旁边构建自定义终结点吗?
Do I need to build a custom endpoint alongside my basic REST api?
所以我仍然有:
GET /students (which returns all the students)
GET /students/{id} (which returns a specific student)
etc
但是对于我的自定义"查询,请执行以下操作:
But then implement the following for my 'custom' query:
GET /students/custom/born_before/{date}/taught_by/{teacher_id}
还是有更标准化的方法?
Or is there a more standardized way of doing this?
推荐答案
您可以转换您的
GET /students/custom/born_before/{date}/taught_by/{teacher_id}
进入
GET /students/?born_before={date}&taught_by={teacher_id}
仅仅是通过示例查询" 选项:您可以填充模型实例使用提供的字段并使用它们进行查询.字段越少,搜索范围越广,显示的结果越多.
which is just a "query by example" option: you can populate a model instance with the provided fields and make a query using them. The less fields, the broader is the search and more results to show.
这是 JIRA API的工作方式,例子.
这篇关于如何使用REST API实现复杂的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!