使用极其快速的数据库查询,轻松创建强大、标准化的 API 端点
Easily create robust, standardized API endpoints using lightning-fast database queries
FastAPI is a Rails library for querying interdependent datasets quickly and returning a human-readable, standard API output.
It works by constructing complex SQL queries that make efficient use of JOINs
and subqueries based upon model dependencies (namely belongs_to, has_one, and has_many).
In only a few lines of code you can decide which fields you wish to expose to your endpoint, any filters you wish to run the data through, and create your controller.
You can preview a live example of FastAPI at http://fastapi.herokuapp.com/
The repository is located at thestorefront/fastapi_example
This gem requires Oj >= 2.9.9 for JSONification, ActiveRecord >= 3.2.0, and ActiveSupport >= 3.2.0.
FastAPI currently supports PostegreSQL as a data layer.
FastAPI is available via RubyGems using:
$ gem install fastapiOtherwise, in any Gemfile in a rails project, use:
require 'fastapi'Let's say we have three models. Person, Bucket, and Marble.
Each Bucket belongs to a Person and can have many Marbles.
Your model for Bucket might look something like this:
class Bucket =)* 5?
```ruby
Model.fastapi.filter({
key1__gte: 5
})It's that easy. The double underscore indicates you're using a filter
comparator, and gte stands for greater than or equal to.
The available comparators are as follows: (Descriptions marked with * indicate scalar inputs will be converted to arrays)
Scalar Fields
'is' # Field == Value
'not' # Field != Value
'gt' # Field > Value
'gte' # Field >= Value
'lt' # Field < Value
'lte' # Field <= Value
'like' # Field contains Value (string)
'not_like' # Field does not contain Value (string)
'ilike' # Field contains Value (case ins.)
'not_ilike' # Field does not contain Value (case ins.)
'null' # Field is NULL
'not_null' # Field is not NULL
'in' # * Field is in Value
'not_in' # * Field is not in Value
Array Fields
'subset' # * Field is a subset of Value
'not_subset' # * Field is not a subset of Value
'contains' # * Value is a subset of Field
'not_contains' # * Value is not a subset of Field
'intersects' # * Field and Value have shared elements
'not_intersects' # * Field and Value have no shared elementsIf your key contains a double underscore, make sure to use the __is comparator
if you look for a specific value.
If you'd like to allow for client-side data filtration (highly recommended), simply use the following in your API endpoint controller:
filters = request.query_parameters
render json: Model.fastapi.filter(filters).response
…
ruby
render json: User.fastapi.filter({__order: [:age, ASC]}).responseOr perhaps via HTTP (hitting an endpoint with request.query_parameters as the
filter):
http://yourapp/api/v1/users/?__order=age,ASCIn FastAPI we opted for very robust, granular control of API responses. "Pages"
do not exist in a strict sense, but rather by :__offset and :__count, much
like you'd expect in a traditional database query.
For example,
render json: Model.fastapi.filter({__offset: 100, __count: 100}).responseWould return (up to) 100 results from Model, beginning at result number 100. (Page 2 at 100 results per page.)
FastAPI has a very strict, standard way of outputting data in the form of a response.
Responses will always look like the following:
{
'meta': {
'total': 0,
'count': 0,
'offset': 0,
'error': null,
},
'data': []
}Where meta.total is the total number of records in the entire dataset,
meta.count is the number of records in the response, meta.offset is the
offset of the first record of the response, and meta.error is null if there
was no error, or a string containing an error message if there was an error.
data will always be an Array of Objects. If there was an error with the
response, data will be empty. If the response was formed by a
FastAPIInstance.fetch call and a record was retrieved, data will be a
length-1 Array.
Thanks for reading! We welcome contributors with good ideas, and we're always looking for new talent.
FastAPI was created by Keith Horwood and Trevor Strieber of Storefront, Inc. in 2014 and is (happily!) MIT licensed.
Twitter: @keithwhor, @TrevorStrieber, @Storefront
Github: keithwhor, TrevorS, thestorefront
Most recent version of the gem is available at RubyGems.org: fastapi
暂无开放 Issues,或尚未同步最近议题。