Intro to Elixir (Ecto Schemas and Phoenix Contexts)

in #utopian-io3 years ago (edited)

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

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you for reading my content/watching my videos as always.

Hey @tensor I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Enjoy the vote and reward!

Thanks for telling us what is phoenix

Coin Marketplace

STEEM 0.16
TRX 0.03
JST 0.026
BTC 13724.94
ETH 405.11
USDT 1.00
SBD 0.98