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
]

Last Post!

thenrio

thenrio

Ideed, for user input fields=ean,date,brand the query from p in Product, select: [p.ean, fragment("extract(epoch from ?)*1000", p.date), p.brand] yields fields in desired order.

But I need to map user fields to selected expressions. When fields=ean,price,date then I should case to from p in Product, select: [p.ean, p.price, fragment("extract(epoch from ?)*1000", p.date)].

I would like to start with select: [] and reduce user inputs to expressions on query to build the equivalent queries above.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement