connection the whole universe of things we need to know about the user's request (a struct, map with known set of fields)
connection
|> endpoint
|> router
|> browser_pipeline
|> UsersController
struct Elixir's main abstraction for working with structured data
- built on top of maps
- provide protection for bad keys at compile time (instead of runtime like maps)
- struct is a map with a
__struct__
key
repository an API for holding things
view module containing rendering functions that convert data into a format the end data will consume (HTML/JSON)
template function on that module compiled from a file containing a raw makrup language and embedded Elixir code
- helpers exist
- after compilation, templates are functions
- Phoenix builds templates using linked lists (no copies of strings)
- cache templates at the hardware level
- similar to partials, can render another template from a template
Phoenix.View.render("template_name", _assigns, {locals: locals})
- Controller renders layout view, which renders actual template
- each template receives
@view_module
and@view_template
- Can we talk more about caching at the hardware level?
- the Elixir framework for persistence
- changesets hold all changes you want to perform on the database
- migrations used to make the database reflect structure of app
- "Just as a controller is a layer to transform requests and responses according to a communication protocol, the model is nothing more than a group of functions to transform data according to our business requirements" (57)
- Models can manipulate other structures such as changesets and queries
- changesets let Ecto manage record changes, cast parameters, and perform validations
- cast on changeset checks for required fields and casts required and optional values to schema types and rejects everything else
- Ecto.Changeset defines
cast
andvalidate_length
- Why changesets? One size does not fit all for update strategies.
- Changesets let you decouple update policy from schema
- changeset had validation errors because Ecto changeset carries validations and stores info for later use. Changesets track changes.
- changeset as bucket to hold everything related to db change, before and after persistence
- "If no parameters are specified, we can’t just default to an empty map, because that would be indistinguishable from a blank form submission. Instead, we default params to the :empty atom. By convention, Ecto will produce an invalid changeset, with empty parameters."
- pass a changeset into the form helper rather than a model.
- able to update data via
Repo.insert
e.g.Repo.insert(%User{ ...> name: "José", username: "josevalim", password_hash: "<3<3elixir" ...> })
- but should prefer to use a changeset e.g.
changeset = User.changeset(%User{}, user_params) {:ok, user} = Repo.insert(changeset)
- validation errors stored in
@changeset.action
- example of multiple changesets?