百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

FastAPI

> 编程语言
开源

使用极其快速的数据库查询,轻松创建强大、标准化的 API 端点

557 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

使用极其快速的数据库查询,轻松创建强大、标准化的 API 端点

FastAPI

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.

Preview

You can preview a live example of FastAPI at http://fastapi.herokuapp.com/

The repository is located at thestorefront/fastapi_example

Requirements

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.

Installation

FastAPI is available via RubyGems using:

bash
$ gem install fastapi

Otherwise, in any Gemfile in a rails project, use:

ruby
require 'fastapi'

Examples

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:

ruby
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 elements

If your key contains a double underscore, make sure to use the __is comparator if you look for a specific value.


Filters in HTTP Requests

If you'd like to allow for client-side data filtration (highly recommended), simply use the following in your API endpoint controller:

ruby
filters = request.query_parameters
render json: Model.fastapi.filter(filters).response

…

ruby
render json: User.fastapi.filter({__order: [:age, ASC]}).response

Or perhaps via HTTP (hitting an endpoint with request.query_parameters as the filter):

http://yourapp/api/v1/users/?__order=age,ASC

Pagination

In 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,

ruby
render json: Model.fastapi.filter({__offset: 100, __count: 100}).response

Would return (up to) 100 results from Model, beginning at result number 100. (Page 2 at 100 results per page.)

Standard Output

FastAPI has a very strict, standard way of outputting data in the form of a response.

Responses will always look like the following:

javascript
{
  '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.

Credits

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· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言