![]() ![]() Do you feel like you learn best with a book? This awesome book (co-written by three people – the creator of Elixir, the creator of Phoenix, and another accomplished author) will guide you through implementing Rumbl, a real-time video app with comments. This is one of the best talks I’ve seen on the subject: concise and informative. Phoenix: an Intro to Elixir’s Web Framework.Phoenix documentation is great, and it’s a good way to find answers to the questions you might have. If you want to learn more about Phoenix, here are a few resources you can use: Iex( 3)> Repo.insert(% Item) do lete_item(id) Now we can interact with our repository in IEx. The above steps can be done faster by using mix. ![]() Is there a generator for making a migration and a schema? Right now, we do not have a great use for them, but they might come handy when expanding the app. You can write custom changesets for each occurrence of data insertion. Ecto changesets help validate data before inserting it into the database. The only difference is that it has a default value for :completed – false. Changesetįield :completed, :boolean, default: false Then, create a subfolder called items, and create a file called item.ex. It will consist of an Ecto schema and a context.įirst, create a new module named items.ex in the lib/todo folder. To migrate the database, type mix ecto.migrate in the console.Īfterwards, we need to create the part of our app that will interact with the database. Here, we are creating a table of items, each of which have a string description and a boolean value, which marks if they have been completed or not. migration add_itemsĪfterwards, you need to find the generated migration file in priv/repo/migrations and edit it to look like this: defmodule do use Ecto. To add a table to our database with Ecto, we need to create a migration. You can find a more detailed introduction to macros in our article on metaprogramming in Elixir. It might take another article to explain how each of these macros work, but they shouldn’t cause you a lot of problems right off the bat, hopefully. Macros are also used for evading boilerplate when calling plugs, working with the routing table, etc. Quick tip: if you use ElixirLS in VSCode, you can use the expand macro command (in the command palette) to look at the final code that will be generated with use. When you see something like use TodoWeb at the top of a module, it means that before compiling, Elixir will go to the module with that name, find a macro called _using_, and put the code that it generates inside the caller module. To pull other code into our modules (quickest import statements + boilerplate setup in the West).Macros are functions that generate code at compile-time. Let’s talk a bit about the magic incantations you might have seen in Phoenix code ( router is the biggest offender). As I said before: at each step, we are just moving the request further and further through composable functions. The endpoint, the router, and the controllers are plugs. In other words, Phoenix is itself a pipeline for transforming conn. They get compiled into the view module during the compilation process with macros. Views get most of their content from templates, which are HTML or JSON files with a dash of Elixir inside them. The controller launches a view that uses conn data to render the page for the user. Then conn gets forwarded to the router, which moves it through its pipeline (developers can define different ones to handle different requests) and passes it to its controller. ![]() It defines a common pipeline that all of the requests pass through. ![]() The endpoint is where all the requests land after being converted to conn by the server. ![]()
0 Comments
Leave a Reply. |