Elixir restapi convert database record: MySQL + PostgreSQL + MongoDB to File Excel

Example 1: Elixir convert MySQL record to Excel file

To convert a MySQL database record to an Excel file in an Elixir REST API, you can use a library like Ecto to query the database and retrieve the record, and then use a library like ElixirExcel to generate the Excel file.

Here’s an example of how you might do this:

Add the necessary dependencies to your Elixir project:

defp deps do
  [
    {:ecto, "~> 3.4"},
    {:elixir_excel, "~> 1.3"}
  ]
end

In your controller, use Ecto to query the database and retrieve the record:

import Ecto

def export_to_excel(conn, params) do
  # Connect to the database
  {:ok, repo} = Ecto.Repo.start_link(:my_app)

  # Retrieve the record from the database
  record = Repo.get(MyApp.Object, params["id"])

  # Generate the Excel file from the record using ElixirExcel
  excel = ElixirExcel.generate(record)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

In your view, you can use the ElixirExcel library to generate an Excel file from the record and send it to the client:

import ElixirExcel

def export_to_excel(record) do
  # Generate the Excel file from the record using ElixirExcel
  excel = ElixirExcel.generate(record)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

That’s a basic example of how you can convert a MySQL database record to an Excel file in an Elixir REST API. You can customize the Excel layout and formatting using the options provided by the ElixirExcel library.

Example 2: Exlix Restapi convert PostgreSQL to Excel file

To convert a PostgreSQL database record to an Excel file in an Elixir REST API, you can use a library like Ecto to query the database and retrieve the record, and then use a library like ElixirExcel to generate the Excel file.

Here’s an example of how you might do this:

Add the necessary dependencies to your Elixir project:

defp deps do
  [
    {:ecto, "~> 3.4"},
    {:elixir_excel, "~> 1.3"}
  ]
end

In your controller, use Ecto to query the database and retrieve the record:

import Ecto

def export_to_excel(conn, params) do
  # Connect to the database
  {:ok, repo} = Ecto.Repo.start_link(:my_app)

  # Retrieve the record from the database
  record = Repo.get(MyApp.Object, params["id"])

  # Generate the Excel file from the record using ElixirExcel
  excel = ElixirExcel.generate(record)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

In your view, you can use the ElixirExcel library to generate an Excel file from the record and send it to the client:

import ElixirExcel

def export_to_excel(record) do
  # Generate the Excel file from the record using ElixirExcel
  excel = ElixirExcel.generate(record)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

Example 3: Elixir restapi convert Mongodb database document to file excel

o convert a MongoDB database document to an Excel file in an Elixir REST API, you can use a library like MongoDB Elixir to query the database and retrieve the document, and then use a library like ElixirExcel to generate the Excel file.

Here’s an example of how you might do this:

Add the necessary dependencies to your Elixir project:

defp deps do
  [
    {:mongodb_elixir, "~> 0.4"},
    {:elixir_excel, "~> 1.3"}
  ]
end

In your controller, use MongoDB Elixir to query the database and retrieve the document:

import MongoDB

def export_to_excel(conn, params) do
  # Connect to the database
  {:ok, client} = MongoDB.Client.start_link(["mongodb://localhost:27017"])

  # Retrieve the document from the database
  document = MongoDB.Collection.find_one(client, "my_app", "objects", {"_id" => params["id"]})

  # Generate the Excel file from the document using ElixirExcel
  excel = ElixirExcel.generate(document)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

In your view, you can use the ElixirExcel library to generate an Excel file from the document and send it to the client:

import ElixirExcel

def export_to_excel(document) do
  # Generate the Excel file from the document using ElixirExcel
  excel = ElixirExcel.generate(document)

  # Send the Excel file to the client
  send_file(conn, excel, type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
end

Elixir create RestApi Model: One to one example: PostgreSQL + MySQL + MongoDB

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

Elixir create restapi example: Plug + Postgresql + MySQL + MongoDB

Here’s an example of how you might create a REST API in Elixir that uses a PostgreSQL database, using the Plug and Ecto libraries:

Example 1 with Elixir + Plug + PostgreSQL

defmodule MyApi do
  use Plug.Router
  use Ecto.Repo, otp_app: :my_app

  plug :match
  plug :dispatch

  get "/users" do
    users = Repo.all(from u in User, select: u)
    conn
    |> put_resp_content_type("application/json")
    |> send_resp(200, Poison.encode!(users))
  end
end

This defines a single endpoint, GET /users, which retrieves all users from the users table in the database and returns them as a JSON array.

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 with Elixir + Pug + MySQL

Here’s an example of how you might create a REST API in Elixir that uses a MySQL database, 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" do
    users = Repo.all(from u in User, select: u)
    conn
    |> put_resp_content_type("application/json")
    |> send_resp(200, Poison.encode!(users))
  end
end

This defines a single endpoint, GET /users, which retrieves all users from the users table in the database and returns them as a JSON array.

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, []

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 3 RestAPI: Elixir MongoDB

Here’s an example of how you might create a REST API in Elixir that uses a MongoDB database, using the Plug and Mongo libraries:

defmodule MyApi do
  use Plug.Router
  use Mongo.Connection

  plug :match
  plug :dispatch

  get "/users" do
    users = Mongo.find("users")
    conn
    |> put_resp_content_type("application/json")
    |> send_resp(200, Poison.encode!(users))
  end
end

This defines a single endpoint, GET /users, which retrieves all users from the users collection in the database and returns them as a JSON array.

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 mongodb to your list of dependencies in your mix.exs file:

defp deps do
  [
    {:mongodb, "~> 0.5"}
  ]
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, []

This is just a very simple example, but you can use the Plug and Mongo 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 Mongo libraries:

Plug: https://hexdocs.pm/plug/readme.html
Mongo: https://hexdocs.pm/mongo/readme.html

Angular 13 Spring WebFlux MongoDB CRUD Operations

In this tutorial, we’re gonna build a full Reactive Application in which, Spring WebFlux, Spring Data Reactive MongoDB are used for backend, and Angular, RxJS, EventSource are on client side.

Related Posts:
How to use Angular Http Client to fetch Data from SpringBoot RestAPI – Angular 13
How to use Angular HttpClient to POST, PUT, DELETE data on SpringBoot Rest APIs – Angular 13
How to build SpringBoot MongoDb RestfulApi
How to use SpringData MongoRepository to interact with MongoDB
Angular 13 + Spring Boot + MongoDB CRUD example
Introduction to RxJS – Extensions for JavaScript Reactive Streams

Continue reading “Angular 13 Spring WebFlux MongoDB CRUD Operations”

Angular 8 Spring WebFlux MongoDB CRUD RestAPI

In this tutorial, we’re gonna build a full Reactive Application in which, Spring WebFlux, Spring Data Reactive MongoDB are used for backend, and Angular, RxJS, EventSource are on client side.

Related Posts:
How to use Angular Http Client to fetch Data from SpringBoot RestAPI – Angular 8
How to use Angular HttpClient to POST, PUT, DELETE data on SpringBoot Rest APIs – Angular 8
How to build SpringBoot MongoDb RestfulApi
How to use SpringData MongoRepository to interact with MongoDB
Angular 8 + Spring Boot + MongoDB CRUD example
Introduction to RxJS – Extensions for JavaScript Reactive Streams

Continue reading “Angular 8 Spring WebFlux MongoDB CRUD RestAPI”