KSingh1980

KSingh1980

Need to run the query and get the result back as JSON format

How to run the random query and get the result in JSON format and send it as a response. The query will be in the following form
DB Name- POSTGRES
1- select COLUMNLIST from TABLENAME where FILTERS.

No validation required. I do not want to use multiple structs because the query is dynamic in nature and will change at RUNTIME and column list and table name all will change at runtime.

Most Liked

mbuhot

mbuhot

Ecto schema-less queries allow you to supply the table name as a string, interpolate simple where conditions from a keyword list, and select the result into a map with keys from a list.

The ecto ebook has a chapter on schemaless queries: http://pages.plataformatec.com.br/ebook-whats-new-in-ecto-2-0

sribe

sribe

It’s also super easy to manipulate the Postgrex return types if you want to drop down below ecto:

  def result_to_maps(%Postgrex.Result{columns: _, rows: nil}), do: []

  def result_to_maps(%Postgrex.Result{columns: col_nms, rows: rows}) do
    Enum.map(rows, fn(row) -> row_to_map(col_nms, row) end)
  end

  def row_to_map(col_nms, vals) do
    Stream.zip(col_nms, vals)
    |> Enum.into(Map.new(), &(&1))
  end

Where Next?

Popular in Chat/Questions Top

lgmfred
Hello, I want to get started with elixir today. Having learnt the basics of Erlang (my first ever language) and seen what it’s can do, I’...
New
Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
New
pillaiindu
I am a VSCode and Sublime user and I know VIM, though I don’t use VIM directly but whenever I code inside Sublime or VSCode, I use Vim em...
New
Lincxx
Hello, I’m sure this has been asked a bunch, but where to start. I do prefer vodeos over books, but recently I have found books to be mor...
New
AstonJ
It’s been a while since we asked this - I’m sure others (especially newcomers to the language) will be interested to hear how you’ve all ...
New
loganhelms
A while back, I read a great book by Luis Atencio titled, Functional Programming in JavaScript. In section 7.3, he discusses memoization ...
New
jsnoble
I come from a javascript background and I just starting learning elixir. I went through the basic tutorials but I want something deeper. ...
New
venomnert
Background I have been a backend elixir developer for about 3 years now. I have been mainly working on simple CRUD applications. Context...
New
Nopp
Hello there, i have a lot to read and to learn, but are there some books, that are focussing on how i “should” plan the architecture of ...
New
Besto
I’ve been trying to start learning Elixir for a couple of weeks while I develop a tiny project I have on node.js, but every time I try to...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement