airtable教程_适用于开发人员的Airtable API
airtable教程
Airtable is an amazing tool.
Airtable是一个了不起的工具。
It’s a mix between a spreadsheet and a database.
它是电子表格和数据库之间的混合体。
As a developer you get to create a database with a very nice to use interface, with the ease of use and editing of a spreadsheet, and you can easily update your records even from a mobile app.
作为开发人员,您可以使用一个非常易于使用的界面来创建数据库,并且易于使用和编辑电子表格,甚至可以从移动应用程序轻松地更新记录。
适用于原型 (Perfect for prototypes)
Airtable is much more than a glorified spreadsheet, however. It is a perfect tool for a developer looking to prototype or create an MVP of an application.
但是,Airtable不仅仅是一个荣耀的电子表格。 对于寻求原型或创建应用程序MVP的开发人员来说,这是一个完美的工具。
An MVP, or Minimum Viable Product, is one initial version of an application or product.
MVP或最低可行产品是应用程序或产品的一个初始版本。
Most products fail not because of technical limitations or because “the stack did not scale”. They fail because either there is no need for them, or the maker does not have a clear way to market the product.
大多数产品之所以失败,并不是因为技术限制或“堆栈无法扩展”。 他们之所以失败,是因为要么没有必要,要么制造商没有明确的产品营销方式。
Creating an MVP minimizes the risks of spending months trying to build the perfect app, and then realizing no one wants it.
创建MVP可以最大程度地减少花数月时间尝试构建完美应用程序,然后没人意识到的风险。
很棒的API (A great API)
Airtable has an absolutely nice API to work with, which makes it easy to interface with your Airtable database programmatically.
Airtable具有绝对不错的API,可以轻松地以编程方式与Airtable数据库进行接口。
This is what makes it 10x superior to a standard spreadsheet, when it comes to data handling AND making it easy to authenticate.
这使得它在数据处理和易于认证方面比标准电子表格高出十倍。
The API has a limit of 5 requests per second, which is not high, but still reasonable to work with for most scenarios.
该API每秒有5个请求的限制,这个请求虽然不高,但是对于大多数情况来说仍然可以使用。
API的出色文档 (A great documentation for the API)
Here is the Airtable API documentation: https://airtable.com/api.
这是Airtable API文档: https ://airtable.com/api。
As developers we spend a lot of time reading through docs and trying to figure out how things work.
作为开发人员,我们花费大量时间阅读文档并试图弄清事情的运作方式。
An API is tricky because you need to interact with a service, and you want to both learn what the service exposes, and how can you use the API to do what you need.
API之所以棘手,是因为您需要与服务进行交互,并且既要了解服务公开的内容,又要如何使用API来完成所需的工作。
Airtable raises the bar for any API documentation out there. It puts your API keys, base IDs and table names directly in the examples, so you just need to copy and paste them into your codebase and you’re ready to go.
Airtable提升了所有API文档的标准。 它将API**,基本ID和表名直接放在示例中,因此您只需要将它们复制并粘贴到代码库中就可以了。
Not just that, the examples in the API docs use the actual data in your table. In this image, notice how the fields example values are actual values I put in my table:
不仅如此,API文档中的示例还使用表中的实际数据。 在此图像中,请注意字段示例值如何是我放入表中的实际值:
The API documentation offers examples using curl
:
API文档提供了使用curl
示例:
and their Node.js official client:
和他们的Node.js官方客户端:
官方Node.js客户端 (The official Node.js client)
Airtable maintains the official Airtable.js Node.js client library, a very easy to use way to access the Airtable data.
Airtable维护官方的Airtable.js Node.js客户端库,这是一种非常易于使用的访问Airtable数据的方式。
It’s convenient because it offers a built-in logic to handle rate limits and retrying the requests when you exceed them.
之所以方便,是因为它提供了内置的逻辑来处理速率限制并在超出限制时重试请求。
Let’s see a few common operations you can perform with the API, but first let’s define a couple values we’ll reference in the code:
让我们看一下您可以使用API进行的一些常见操作,但首先让我们定义一些我们将在代码中引用的值:
-
API_KEY
: the Airtable API keyAPI_KEY
:Airtable API** -
BASE_NAME
: the name of the base you’ll work withBASE_NAME
:您将使用的基地名称 -
TABLE_NAME
: the name of the table in that base.TABLE_NAME
:该库中表的名称。 -
VIEW_NAME
: the name of the table view.VIEW_NAME
:表视图的名称。
A base is a short term for database, and it can contain many tables.
基是数据库的短期术语,它可以包含许多表。
A table has one or more views that organize the same data in a different way. There’s always at least one view (see more on views)
一个表具有一个或多个视图,这些视图以不同的方式组织相同的数据。 始终至少有一个视图( 有关视图的更多信息 )
认证 (Authenticate)
You can set up the AIRTABLE_API_KEY
environment variable, and Airbase.js will automatically use that, or explicitly add it into your code:
您可以设置AIRTABLE_API_KEY
环境变量,Airbase.js将自动使用该变量,或将其显式添加到您的代码中:
const Airtable = require('airtable')
Airtable.configure({
apiKey: API_KEY
})
初始化基地 (Initialize a base)
const base = require('airtable').base(BASE_NAME)
or, if you already initialized the Airtable variable, use
或者,如果您已经初始化了Airtable变量,请使用
const base = Airtable.base(BASE_NAME)
引用表格 (Reference a table)
With a base
object, you can now reference a table using
使用base
对象,您现在可以使用
const table = base(TABLE_NAME)
检索表记录 (Retrieve the table records)
Any row inside a table is called a record.
表中的任何行都称为记录 。
Airtable returns a maximum of 100 records in each page of results. If you know you will never go over 100 items in a table, just use the firstPage
method:
Airtable在每页结果中最多返回100条记录。 如果您知道您永远不会超过一个表中的100个项目,则只需使用firstPage
方法:
table.select({
view: VIEW_NAME
}).firstPage((err, records) => {
if (err) {
console.error(err)
return
}
//all records are in the `records` array, do something with it
})
If you have (or expect) more than 100 records, you need to paginate through them, using the eachPage
method:
如果您拥有(或预期)超过100条记录,则需要使用eachPage
方法对它们进行分页:
let records = []
// called for every page of records
const processPage = (partialRecords, fetchNextPage) => {
records = [...records, ...partialRecords]
fetchNextPage()
}
// called when all the records have been retrieved
const processRecords = (err) => {
if (err) {
console.error(err)
return
}
//process the `records` array and do something with it
}
table.select({
view: VIEW_NAME
}).eachPage(processPage, processRecords)
检查记录内容 (Inspecting the record content)
Any record has a number of properties which you can inspect.
任何记录都有许多可以检查的属性。
First, you can get its ID:
首先,您可以获取其ID:
record.id
//or
record.getId()
and the time of creation:
和创建时间:
record.createdTime
and you can get any of its properties, which you access through the column name:
您可以获取其任何属性,可通过列名称进行访问:
record.get('Title')
record.get('Description')
record.get('Date')
获取特定记录 (Get a specific record)
You can get a specific record by ID:
您可以通过ID获取特定记录:
const record_id = //...
table.find(record_id, (err, record) => {
if (err) {
console.error(err)
return
}
console.log(record)
})
Or by a specific column value:
或按特定的列值:
const getData = url => {
table.select({
filterByFormula: `{url} = "${url}"`
}).eachPage(function page(records) {
records.forEach(function(record) {
console.dir(record.get('json'))
})
})
}
It’s also handy to use .all()
and async/await if you want to return the data from the function:
如果要从该函数返回数据,也可以使用.all()
和async / await:
const getData = async url => {
const records = await table.select({
filterByFormula: `{url} = "${url}"`
}).all()
return records[0].get('json')
}
Note that all()
fetches all pages of results synchronously, and I’d use it only when you have a few pages of results (or 1 result, like in this case)
请注意, all()
同步获取所有页面的结果,并且仅当您有几个页面的结果(或本例中为1个结果)时才使用它。
建立新纪录 (Create a new record)
You can add a new record
您可以添加新记录
table.create({
"Title": "Tutorial: create a Spreadsheet using React",
"Link": "https://flaviocopes.com/react-spreadsheet/",
}, (err, record) => {
if (err) {
console.error(err)
return
}
console.log(record.getId())
})
更新记录 (Update a record)
You can update a single field of a record, and leave the other fields untouched, using update
:
您可以更新纪录的单场,并留下其他字段不变,使用update
:
const record_id = //...
table.update(record_id, {
"Title": "The modified title"
}, (err, record) => {
if (err) {
console.error(err)
return
}
console.log(record.get('Title'))
})
Or, you can update some fields in a record and clear out the ones you did not touch, with replace
:
或者,您可以使用replace
更新记录中的某些字段并清除您未触摸的字段:
const record_id = //...
table.replace(record_id, {
"Title": "The modified title",
"Description": "Another description"
}, (err, record) => {
if (err) {
console.error(err)
return
}
console.log(record)
})
删除记录 (Delete a record)
A record can be deleted using
可以使用删除记录
const record_id = //...
table.destroy(record_id, (err, deletedRecord) => {
if (err) {
console.error(err)
return
}
console.log('Deleted record', deletedRecord.id)
})
airtable教程
上一篇: RSA
下一篇: iText使用小技巧