ExAirtable.Table behaviour (ExAirtable v0.2.9) View Source
The Table
behaviour allows you to define your own modules that use Airtables.
It is a thin wrapper around Service
, but often more convenient to use.
Examples
defmodule MyTable do
use ExAirtable.Table
def base, do: %ExAirtable.Config.Base{
id: "your base ID",
api_key: "your api key"
}
def name, do: "My Airtable Table Name"
end
iex> MyTable.list()
%ExAirtable.Airtable.List{}
iex> MyTable.retrieve("rec123")
%ExAirtable.Airtable.Record{}
Link to this section Summary
Callbacks
A valid %ExAirtable.Config.Base{} config for your table.
(Optional) A map of parameters that you wish to send to Airtable when either list/1
or list_async/1
is called.
The name of your table within Airtable
(Optional) A map converting Airtable field names to local schema field names.
Link to this section Callbacks
Specs
base() :: ExAirtable.Config.Base.t()
A valid %ExAirtable.Config.Base{} config for your table.
Often this will end up being in the application configuration somewhere, for example:
# ... in your mix.config
config :my_app, Airtable.Base, %{
id: "base id",
api_key: "api key"
}
# ... in your table module
def base do
struct(ExAirtable.Config.Base, Application.get_env(:my_all, Airtable.Base))
end
Specs
list_params() :: Keyword.t()
(Optional) A map of parameters that you wish to send to Airtable when either list/1
or list_async/1
is called.
You would define this if you want your local MyTable.list
and ExAirtable.list(MyTable)
functions to always return a filtered query from Airtable.
Examples
# To filter records by a boolean "Approved" field, and only return the "Name" and "Picture", your params might look like this:
def list_params do
[
filterByFormula: "{Approved}",
fields: "Name",
fields: "Picture"
]
end
See here for more details about the available Airtable List API options.
Specs
name() :: String.t()
The name of your table within Airtable
Specs
schema() :: map()
(Optional) A map converting Airtable field names to local schema field names.
This is handy for situations (passing attributes to ecto schemas, for example) where you may want different in-app field names than the fields you get from Airtable.
If you don't define this method, the default is to simply use Airtable field names as the schema.
Examples
# If you want atom field names for your schema map...
def schema do
%{
"Airtable Field Name" => :local_field_name,
"Other Airtable Field" => :other_local_field
}
end
iex> ExAirtable.Airtable.Record.to_schema(record, MyTable.schema)
%{airtable_id: "rec1234", local_field_name: "value", other_local_field: "other value"}
# If you want string field names for your schema map...
def schema do
%{
"Airtable Field Name" => "local_field_name",
"Other Airtable Field" => "other_local_field"
}
end
iex> ExAirtable.Airtable.Record.to_schema(record, MyTable.schema)
%{"airtable_id" => "rec1234", "local_field_name" => "value", "other_local_field" => "other value"}
See also to_schema/1
and from_schema/1
.