


Despite the fact that we may have a single table in our database to hold the data for a particular model, the ways in which we work with that data can be many and varied. The key observation that led the Ecto team to these lightweight schemas is that when it comes to our complex business domains, one size does not fit all. We can have another one for taking data input by a user on a web form, and mapping that to an Elixir struct with a slightly different shape, for eventual insertion back into the database. For instance, we can have one schema for pulling posts out of our database into an Elixir struct, ready for manipulation by our application. For any particular domain model, we can have multiple schemas for different purposes. What's nice about schemas is that they are lightweight. Through a tour of its four main modules. is a popular database wrapper and integrated query language for Elixir. We want data management tools that support our intentions to design our applications well.
#ECTO CHANGESET CODE#
Similarly, code that's easy to change affords us the joy of fluid expression. When our code is easy to change, we can respond to changing requirements quickly, thereby providing more value to our clients. Since "well-designed" is a fairly nebulous term, I'll lean on to provide a definition.įrom a practical point of view, changeability is the only design metric that matters code that’s easy to change is well-designed. In trying to meet both of them, it helps to focus on building well-designed applications. Thankfully, these goals are not mutually exclusive.

#ECTO CHANGESET SOFTWARE#
As engineers, our desire is to build software that is a joy to work on. Ideally, the specifications defined in the migration should align with your Ecto Schema and generic changeset: defmodule Core.How Elixir's Ecto Promotes Well-Designed ApplicationsĪs consultants, our job is to provide the most value possible to our clients over the course of our engagement. In the migration above we are specifying:ĭepending on your datastore and column type you can apply a variety of data constraints to fulfill your needs. In Elixir, this is commonly achievable through Ecto, the most common component to interact with application data stores, through Ecto Migration DSL: defmodule doĪdd :company_id, references(:companies, type: :binary_id), null: falseĬreate index(:users,, unique: true)Ĭreate constraint(:users, :age_must_be_positive, check: "version > 0") Database Data IntegrityĪs mentioned above, having good database specifications will facilitate data integrity.

The Changeset API Pattern is not the sole responsible for achieving this goal, however, once used in conjunction with some data modeling best practices such as column types and constraints, default values and so on, the pattern will become an important application layer on top of an already established data layer, aiming for an overall better data integrity. Overall Goalįacilitate the data integrity main goal, ensure that data is recorded exactly as intended. Data integrity is not to be confused with data security, the discipline of protecting data from unauthorized parties. In short, data integrity aims to prevent unintentional changes to information. The overall intent of any data integrity technique is the same: ensure data is recorded exactly as intended. Data integrity is the maintenance of, and the assurance of the accuracy and consistency of, data over its entire life-cycle, and is a critical aspect to the design, implementation and usage of any system which stores, processes, or retrieves data. One of these patterns that I am having great success while working on web applications in Elixir, is what I am calling, for the lack of a better name, the Changeset API Pattern.īefore I start with the pattern itself, I'd like to outline some information that I consider as the motivation behind the usage, and it is called Data Integrity. Those are called design patterns, formalized best practices that can be used to solve common problems when implementing a system. Over time, as you gain overall experience with software development, you start noticing some paths that can lead to much more smooth sailing.
