Intro to Elixir (Ecto Schemas and Phoenix Contexts)
Phoenix is mainly just a web interface on top of your basic elixir application but it still gives us some great abstractions and it ties very well into Ecto (the database back end library). Phoenix 1.3 brings many really cool features with it. One of the biggest changes is its bound context data model. Contexts are modules that expose and group related functionality which allows the user to build their application in a data driven way.
At the center of these new encapsulation patterns are Ecto Schemas. These Schemas let us map our data into any source using an Elixir struct (in this case we are using PostgreSQL). These schemas also expose functionality that might not be found in the database or data layer that you are working on. For instance, if you are using a database that does not support the autoincrementing of primary keys you can configure and use various modules to have elixir handle these tasks.
These Ecto Schemas also give us access to Ecto Changeset which allow us to filter, cast, define and validate our datasets in a functional pipeline style. Data provided by the user can be type converted and properly validated before it is inserted into your database/datastore. Because Elixir is a dynamic programming language, it does not make use of a strong type system and as such, libraries like Ecto are powerful allies to keeping our functions and programs pure and functional.
In this tutorial, we build a small scheduler app that mainly serves to show the various different ways that we can use Phoenix Contexts and Ecto to our advantage. We build a user module, a shift module, a companies module and a scheduling module. These different data sets all tie into one another in various different ways with the scheduling and shift module sharing a context.
Full Github Source Code is here
If you enjoy the video, feel free to follow me on YouTube to get updates on the channel as well as more content for Rust, Elm, Elixir and Go.
Posted on Utopian.io - Rewarding Open Source Contributors