ExAirtable.Phoenix.Repo (ex_airtable_phoenix v0.1.0) View Source

Functions to deal with finding and converting ExAirtable structs into Ecto Schema-fied models for your app.

Also contains helper functions for finding, filtering, and injecting fields and relationships from any model that implements the ExAirtable.Phoenix.Model behaviour.

These methods roughly correspond to similar methods in the Ecto.Repo library, to ensure a smooth upgrade path for apps that want to move from Airtable to a hosted Postgres or other database back-end.

Link to this section Summary

Functions

Return all valid model records from the Airtable cache.

Filter a list of model records to just the ones where key matches value.

Filter all records from a given model to just the ones where one of the values at key matches value.

Replace Airtable "list of IDS" related field references with actual model objects.

Same as get_by/3 but only returns the first match.

Link to this section Functions

Return all valid model records from the Airtable cache.

Pass in the name of the module that implements the ExAirtable.Phoenix.Model behaviour.

Returns records from the model that pass validation. This might be a subset of the records that you find in Airtable, depending on how strict your validation is.

Link to this function

get_by(model, key, value)

View Source

Filter a list of model records to just the ones where key matches value.

You can pass a pre-found list of records, or just the name of a model.

Returns an empty list [] if nothing matches.

Examples

iex> get_by(MyApp.Posts.Comment, :airtable_id, "rec1234")
[%MyApp.Posts.Comment{}, ...]

iex> get_by(list_of_posts, :airtable_id, "rec1234")
[%MyApp.Posts.Posts{}, ...]
Link to this function

get_relationship(records, key, value)

View Source

Filter all records from a given model to just the ones where one of the values at key matches value.

Because Airtable deals with "many-to-many" fields and "multiple choice" fields by returning JSON arrays, it's helpful to have a way to find records where something in that array matches a particular value.

You can pass a pre-found list of records, or just the name of a model.

Examples

iex> get_relationship(MyApp.Post.Comment, :user, my_user.airtable_id)
[%MyApp.Posts.Comment{}, ...]

iex> get_relationship(MyApp.Post.Post, :tags, "Tech")
[%MyApp.Posts.Post{}, ...]
Link to this function

inject_relationship(record, key, records)

View Source

Replace Airtable "list of IDS" related field references with actual model objects.

You can pass a pre-found list of records, or just the name of a model.

Examples

iex> inject_relationship(post, :comments, MyApp.Post.Comment)
%MyApp.Posts.Post{comments: [%MyApp.Posts.Comment{}, ...]}

Same as get_by/3 but only returns the first match.