Mix Elixir project with Ecto

Taken from unsplash

In this post I am going to share how to make a Mix project with Elixir and Ecto. I was using Elixir 1.7.3 while making this project.

For this section, I started with an empty template of the mix project but its a template. More about this can be found over here.

After installing it I pulled a template form hex,

mix template.install hex gen_template_ecto_service

and after this run command to have a mix project with Ecto,

mix gen ecto_service project_name --module_name=ModuleName
cd project_name
mix deps.get

Now I have to open the project and update the credentials over the config/dev.exs

after we can run the command over terminal,

mix ecto.create

It will create a database with the project name. And will initialize the Repo.

After this, I tried to create Migration and generate it.

mix ecto.gen.migration create_tasks_table

And write the migration, into /repo/migration/migration_file_name

And run, mix ecto.migrate

After this to spin up a CRUD app to check the power of the Ecto I created,

todo_list/task.ex over the Module directory.

Now, we can boot up iex -S mix and run,

> TodoList.Task.create_task("Work", "Work for elixir", "incomplete")
> TodoList.Task.tasks

>> [
__meta__: #Ecto.Schema.Metadata<:loaded, "tasks">,
id: 1,
inserted_at: ~N[2018-08-27 18:11:46.807061],
task_description: "Work for elixir",
task_name: "Work",
task_status: "incomplete",
updated_at: ~N[2018-08-27 18:11:46.811099]

> TodoList.Task.show_task(1) #id
>> "{\"updated_at\":\"2018-08-27T18:11:46.811099\",\"task_status\":\"incomplete\",\"task_name\":\"Work\",\"task_description\":\"Work for elixir\",\"inserted_at\":\"2018-08-27T18:11:46.807061\",\"id\":1}"
> TodoList.Task.update_task(1, %{task_name: "New Name"}) 
#id, changeset
>> "Updated"
> TodoList.Task.delete_task(1) #id
>> "Deleted 1 numbered task"

And this is a small CRUD based todolist made with mix project.

The github link of the full code is here: Link Of the Project.

Like what you read? Give Mahabub Islam Prio a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.