thenrio

thenrio

Dynamic and ordered list of expressions in ecto query from user input, each input maps to a field or fragment

I have a products api, database is postgresql, I use ecto.

curl "localhost:4000/products?brand=AZN&limit=1&fields=ean,date,brand"
{"data":[{"brand":"AZN","date":"2019-09-01T02:00:00Z","ean":"9783423214254"}]}

Accept can be text/csv, and then

  • format of date is milliseconds from epoch.
  • fields MUST be in order
curl "localhost:4000/products?brand=AZN&limit=1&fields=ean,date,brand" -H accept:text/csv
9783423214254,1567303200000,AZN

I do not know how to achieve that using select and select_merge.

When I add expression to select, one at a time using select_merge:

q = from p in Product, select: %{}
q = from p in q, select_merge: map(p, [:ean])
q = from p in q, select_merge: %{date: fragment("extract(epoch from ?)*1000", p.date)}
q = from p in q, select_merge: map(p, [:brand])
Repo.to_sql(:all, q)
{"SELECT p0.\"ean\", p0.\"brand\", extract(epoch from p0.\"date\")*1000 FROM \"products\" AS p0",
 []}

Then order is not preserved: ean, brand, date. I need ean, date, brand.
How would you do that?

Cheers.

Most Liked

LostKobrakai

LostKobrakai

Maps in elixir are not ordered, therefore you probably shouldn’t expect an ecto query producing a map to do so. Any ordering you might observe is an implementation detail, you shouldn’t rely on.

q = from p in Product, select: [
  p.ean,
  fragment("extract(epoch from ?)*1000", p.date),
  p.brand
]

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

We're in Beta

About us Mission Statement