Learn how to persist and query data using Ecto's Repo module in Phoenix applications
Learn how to persist and query data using Ecto’s Repo module in Phoenix applications.
The main gateway for database operations in Phoenix/Ecto. Use functions like Repo.all
, Repo.insert
, Repo.get
, and Repo.get_by
to work with data.
Repo.all(Product)
returns all productsRepo.insert(%Product{...})
(bypasses changeset)Product.changeset(%Product{}, params)
) then insert:
Repo.insert(changeset)
.iex.exs
file to save aliases/imports for easier command-line workflowfrom(p in Product, select: p.slug, where: p.console == "pc")
Repo.all(query)
Repo
callsRepo.all(Product)
for index actions, Repo.get_by(Product, slug: slug)
for show actionsAccessing DB directly in controllers works for demos, but proper Phoenix projects should use contexts (separation of business logic from web/UI code). This comes in the next video.
This episode explains how to actually persist (insert/query) data in Phoenix using Ecto’s Repo
module, best practices for using changesets, basic querying/saving patterns, and connecting the UI to the DB for real dynamic pages.