Example 1: Exilir + PostgreSQL: One-to-One
Here’s an example of how you might create a REST API in Elixir that uses a PostgreSQL database and models a one-to-one relationship between two entities, using the Plug and Ecto libraries:
defmodule MyApi do
use Plug.Router
use Ecto.Repo, otp_app: :my_app
plug :match
plug :dispatch
get "/users/:user_id/profile" do
user_id = Integer.parse(conn.params["user_id"])
user = Repo.get(User, user_id)
profile = Repo.get(Profile, user.profile_id)
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Poison.encode!(%{user: user, profile: profile}))
end
end
This defines a single endpoint, GET /users/:user_id/profile, which retrieves a user and their associated profile from the users and profiles tables in the database, respectively, and returns them as a JSON object. The user_id is passed as a URL parameter.
To run this API, you’ll need to set up a database connection using the Ecto library. Here’s an example of how you might do this in your config.exs file:
config :my_app, MyApi.Repo,
adapter: Ecto.Adapters.Postgres,
database: "my_database",
username: "postgres",
password: "postgres",
hostname: "localhost"
You can then use the Plug.Adapters.Cowboy adapter to run the API as a standalone web server:
Plug.Adapters.Cowboy.http MyApi, []
This is just a very simple example, but you can use the Plug and Ecto libraries to build more complex APIs with multiple endpoints, various HTTP methods, and more advanced database queries.
For more information, you can check out the documentation for the Plug and Ecto libraries:
Plug: https://hexdocs.pm/plug/readme.html
Ecto: https://hexdocs.pm/ecto/readme.html
Example 2: Elixir MySQL One-to-One RestAPI
Here’s an example of how you might create a REST API in Elixir that uses a MySQL database and models a one-to-one relationship between two entities, using the Plug and Ecto libraries:
defmodule MyApi do
use Plug.Router
use Ecto.Repo, otp_app: :my_app
plug :match
plug :dispatch
get "/users/:user_id/profile" do
user_id = Integer.parse(conn.params["user_id"])
user = Repo.get(User, user_id)
profile = Repo.get(Profile, user.profile_id)
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Poison.encode!(%{user: user, profile: profile}))
end
end
This defines a single endpoint, GET /users/:user_id/profile, which retrieves a user and their associated profile from the users and profiles tables in the database, respectively, and returns them as a JSON object. The user_id is passed as a URL parameter.
To run this API, you’ll need to set up a database connection using the Ecto library. Here’s an example of how you might do this in your config.exs file:
config :my_app, MyApi.Repo,
adapter: Ecto.Adapters.MySQL,
database: "my_database",
username: "root",
password: "root",
hostname: "localhost"
You’ll also need to install the mysql_ecto library, which provides the MySQL adapter for Ecto. To do this, add mysql_ecto to your list of dependencies in your mix.exs file:
defp deps do
[
{:mysql_ecto, "~> 2.0"}
]
end
Then run mix deps.get to install the dependencies.
You can then use the Plug.Adapters.Cowboy adapter to run the API as a standalone web server:
Plug.Adapters.Cowboy.http MyApi, []
Example 3: Elixir + MongoDB + One-to-One
Here’s an example of how you might create a REST API in Elixir that uses a MongoDB database and models a one-to-one relationship between two entities, using the Plug and Mongo libraries:
defmodule MyApi do
use Plug.Router
use Mongo.Connection
plug :match
plug :dispatch
get "/users/:user_id/profile" do
user_id = conn.params["user_id"]
user = Mongo.find_one("users", %{"_id" => user_id})
profile = Mongo.find_one("profiles", %{"user_id" => user_id})
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Poison.encode!(%{user: user, profile: profile}))
end
end
This defines a single endpoint, GET /users/:user_id/profile, which retrieves a user and their associated profile from the users and profiles collections in the database, respectively, and returns them as a JSON object. The user_id is passed as a URL parameter.
To run this API, you’ll need to set up a database connection using the Mongo library. Here’s an example of how you might do this in your config.exs file:
config :my_app, MyApi.Mongo,
hostname: "localhost",
database: "my_database"
You’ll also need to install the mongodb library, which provides the MongoDB driver for Elixir. To do this, add `mongod