11/29/2023 0 Comments Elixir ecto to sql![]() We can use the Repo.get/3 function to fetch a record from the database given its ID. We can perform simple queries directly against our Friends.Repo with the help of a handful of functions. Recall that a “repository” in Ecto maps to a datastore such as our Postgres database.Īll communication to the database will be done using this repository. Let’s query the database again to check if we still have a single record.In this lesson, we’ll continue building off the Friends app and the movie-cataloguing domain we set up in the previous lesson. Let’s update our migration to look as follows: topic - The current topic of the room can be changed from time to time, to reflect current state in the room.īefore we apply the migration itself we need to update it for our needs.description - The text which describes the intention of the room.We have created it with the following columns. Now since we are clear about contexts let’s get back to our table. So from now on, Phoenix requires us to group models into contexts. The talk is quite interesting by the way. You can also find more details what that decision has been made in the ElixirConf 2017 Keynote talk given by Chris McCord. The main idea comes from the Domain-Driven Design Book and also described as a Bounded Contexts by Martin Fowler. It can help to draw the borders between different modules. Contexts help to group similar functionality under the same module. Phoenix 1.3 introduces the contexts functionality. Here we also need to stop a little bit and talk about contexts. Why do we use Conversation.Room instead of just Room? The Conversation module here would be a context. → mix Conversation.Room rooms name:string description:string topic:string Using Phoenix generators we can create our model as follows: The last one will provide diffrent results in some cases, it is deprecated, and it would be removed in Phoenix 1.4. If you are using version 1.3 of the Phoenix framework then make sure you use mix phx. Let’s try to create a new table in our database to keep the information about rooms.Įcto provides us migration generators, which we can use to describe the structure of the table and apply those changes in a database. Query - Wrapper in Elixir around SQL queries.Changeset - Provides a way to cast attributes value into correct types, add validation rules and constraints.It converts table’s columns into Struct items. Schema - Think of that as a mapping between a database table and its columns with the Elixir struct.It can provide access to different data storages such as databases or in-memory storages. The Repo hides implementation details from models. Models hide implementation details from the rest of components. Repo - the repository of data storage.Or does it?Īnyway, Ecto is a library and domain-specific language to interact with databases. Probably what is why Ecto cannot be considered as ORM. We know, Elixir is not an Object-Oriented Programming language. ![]() Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. If we take a look at ORM’s description from Wikipedia page: If you are familiar with other frameworks you probably know about ORM (Object-relational Mapping and might think: “Ah. We are communicating with it through an additional layer called Ecto. In the Phoenix framework, we are not working database directly. As you may know from the first article of these series, We will use PostgreSQL for that application. Nowadays almost every application has a persistent storage. Also, there is no way to manage rooms by users of our application. Every time we want to update the list of the rooms we need to update the code. We have the rooms list in our application. We will get to that question soon as well. ![]() You may also notice why does that model is called Conversation instead of Room. Yes, that data is hardcoded at the moment, but we are going to change it soon. Our controller and view use that data to render a list of the rooms on the page. We have created a model which provides us data about rooms. Let’s quickly sum up what we did till now. We have used a comprehension here to iterate through a list of the rooms and render the element for each room.Īnd now our page looks as it was at the beginning. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |