How to Fetch data from MySQL to JSON Format using Phoenix-Elixir App

Hi, Im new to phoenix and elixir. Someone give me task to develop an App using Phoenix framework to retrieve data from existing MySQL DB into JSON file as output. Consider using Elixir and Ecto as middleware. Sorry, just newbie here. Any idea? Thank you

Hey @JeanWin25 welcome. To accomplish something like this you need to break down the problem. If you’re new to the language, then the first step is just getting familiar with basic Elixir. I like learning from books, so I used https://pragprog.com/book/elixir16/programming-elixir-1-6 when I started. There are lots of resources out there though to fit various learning styles, and if you search the forums there are a variety of resource compilation threads.

From there you’ll want to learn the basics of Phoenix and Ecto. Phoenix will let you handle web requests, an Ecto will let you make queries into MySQL.

2 Likes

Start off by defining a basic Phoenix app. From there define your MySQL DB model using Ecto.Schema. The key point is above your schema you indicate that it should be JSON encoded.
E.g:

@derive {Poison.Encoder, except: [:__meta__]}
schema "authors" do
   field :name, :string
   has_many :books, Book
end

In the above I’m using the Poison JSON encoder.

I usually use the console to test things out as well.

1 Like