Let’s create a migration script for our first table: mix create_invoices Migrations really help keep the application in sync with the database. Ecto.Migration gives you a set of methods to create tables, add indexes, create constraints, and other schema-related stuff. Migrations are files that are used to modify the database schema. This diagram represents how our models will be related to each other: For changesets (models) we will have Invoice, Item and InvoiceItem. Building an Invoice with Inline Itemsįor our demo application, we will build a simple invoicing tool. We are now ready to start coding, but let’s define the scope of our app first. This command creates the database and, with that, we have essentially finished the configuration. Having defined all configuration for our database we can now generate it by running: mix ecto.create Now let’s edit the configuration file config/config.exs: use Mix.Config If you look at lib/cart/repo.ex you will see that this file has been already created, meaning we have a Repo for our application. You can learn more about supervisors here. We define the children supervised with the strategy strategy: :one_for_one which means that, if one of the supervised processes fails, the supervisor will restart only that process into its default state. In this file, we are defining the supervisor supervisor(Cart.Repo, ) and adding it to the children list (in Elixir, lists are similar to arrays). We will edit lib/cart.ex: defmodule Cart do If you read the output, it tells you to add a supervisor and repo in your app. Now run the following command: mix ĭon't forget to add your new repo to your supervision treeĪnd to add it to the list of ecto repositories in your configuration files (so Ecto tasks work as expected): This is a cool feature since it allows us to have many repos, i.e. With the line :cart, cart_repos: we are telling Ecto which repos we are using. ![]() We are just setting the minimum, so we can run the next command. We need to configure the database in the file config/config.exs: use Mix.Config We can have more than one repo, meaning we can connect to more than one database. We will now look at how to define a repo in our application. This will install all dependencies and create a file mix.lock that stores all dependencies and sub-dependencies of the installed packages (similar to Gemfile.lock in bundler). Once you have edited the file, run in the console: mix deps.get We also have to add those as dependencies by adding in defp deps do postgrex (which is the database adapter) and ecto. In def application do we have to add as applications :postgrex, :ecto so these can be used inside our application. # Type "mix help deps" for more examples and options Next, we go to the cart directory with cd cart and open the file mix.exs and replace its contents: defmodule Cart.Mixfile do ![]() We are using the -sup option since we need a supervisor tree that will keep the connection to the database. ![]() This will create a directory cart with the initial project files: * creating README.md Mix is a build tool that ships with Elixir that provides tasks for creating, compiling, testing your application, managing its dependencies and much more.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |