Using Ecto for Database Interactions

From Elixir Wiki
Jump to navigation Jump to search

Using Ecto for Database Interactions

Introduction[edit]

Ecto is a powerful database wrapper and query generator for Elixir. It provides a clean, functional, and intuitive API for interacting with databases. This article will guide you through the process of using Ecto for database interactions in your Elixir applications.

Prerequisites[edit]

Before we dive into using Ecto, make sure you have the following prerequisites:

  • Elixir installed on your machine
  • Understanding of basic Elixir syntax and concepts
  • Familiarity with databases and SQL

Getting Started[edit]

To start using Ecto in your Elixir application, you need to add it as a dependency in your `mix.exs` file. Open the file and add the following line to the `deps` function:

```elixir {:ecto, "~> 3.0"} ```

Configuring Ecto[edit]

Ecto relies on a configuration file to connect to your database. Create a `config` directory if it doesn't exist and then create a `config.exs` file inside it. Add the following configuration options:

```elixir use Mix.Config

config :my_app, MyApp.Repo,

 adapter: Ecto.Adapters.Postgres,
 database: "my_app",
 username: "postgres",
 password: "password",
 hostname: "localhost",
 pool_size: 10

```

Replace `MyApp.Repo` with the name of your own repository module, and modify other options according to your database setup.

Defining a Schema[edit]

Next, we need to define a schema module that represents our database table. Create a file called `user.ex` under the `lib/my_app` directory with the following code:

```elixir defmodule MyApp.User do

 use Ecto.Schema
 schema "users" do
   field :name, :string
   field :age, :integer
   timestamps()
 end

end ```

This defines a schema for the `users` table with `name` and `age` columns. The `timestamps()` function automatically adds `created_at` and `updated_at` fields to the schema.

Creating a Repository[edit]

Now, let's create a repository module that will handle the database interactions. Create a file called `repo.ex` under the `lib/my_app` directory with the following code:

```elixir defmodule MyApp.Repo do

 use Ecto.Repo,
   otp_app: :my_app,
   adapter: Ecto.Adapters.Postgres

end ```

Replace `:my_app` with the name of your application module.

Querying the Database[edit]

With Ecto and the necessary modules set up, we can now perform database operations. Let's see some examples:

  • Fetch all users:

```elixir users = MyApp.Repo.all(MyApp.User) ```

  • Find a user by ID:

```elixir user = MyApp.Repo.get(MyApp.User, 1) ```

  • Create a new user:

```elixir changeset = MyApp.User.changeset(%MyApp.User{}, %{name: "John", age: 25}) MyApp.Repo.insert(changeset) ```

  • Update a user:

```elixir user = MyApp.Repo.get(MyApp.User, 1) changeset = MyApp.User.changeset(user, %{age: 26}) MyApp.Repo.update(changeset) ```

  • Delete a user:

```elixir user = MyApp.Repo.get(MyApp.User, 1) MyApp.Repo.delete(user) ```

These are just a few examples of what you can do with Ecto. Refer to the official Ecto documentation for more advanced queries and features.

Conclusion[edit]

By now, you should have a good understanding of how to use Ecto for database interactions in your Elixir applications. Ecto provides a simple and powerful way to work with databases, making it an excellent choice for building robust and scalable applications.

See Also[edit]