Hi I am moving a web app that work with geospatial data, from express to cowboy.
I have a many sql selects that run without problems using node pg-promise,
for example:
sql= `select json_build_object(
'id', farms.id::text,
'name', farms.name,
'centroid', (SELECT st_AsGeoJSON(st_centroid(ST_Extent(boundary)))
FROM fields where farms.id = fields.farmid )::json
)
as farm FROM farms where userid = 5 ORDER BY name`;
In the result every row is an object:
rows:[{"farm":{"id":"6","name":"La Candelaria","centroid":{"type":"Point","coordinates":[-63.710060569,-31.327962513]}}},
{"farm":{"id":"8","name":"New farm 10","centroid":{"type":"Point","coordinates":[-63.844279209,-31.339159407]}}},
{"farm":{"id":"5","name":"San Teodoro","centroid":{"type":"Point","coordinates":[-63.700476101,-31.395135145]}}}]
But with Postgrex every row have inside a list, and also, lost the object name “farm”.
rows: [
[
%{"id" => "6", "name" => "La Candelaria", "centroid" => %{"coordinates" => [-63.710060569, -31.327962513],"type" => "Point" }}
],
[
%{"id" => "8", "name" => "New farm 10", "centroid" => %{ "coordinates" => [-63.844279209, -31.339159407], "type" => "Point"}}
],
[
%{"id" => "5", "name" => "San Teodor", "centroid" => %{"coordinates" => [-63.700476101, -31.395135145], "type" => "Point" }}
]
]
How to configure Posgrex to give me a list of maps
rows: [%{"farm" => %{....},%{"farm" => %{....},%{"farm" => %{....}]
The other alternative is reprocess the rows:
newRows = Enum.map(rows, fn [row] -> %{"farm" => row} end)
But, there are a lot of select and some are very complex.
The version from mix.exs are: {:postgrex, “~> 0.16”} and {:jason, “~> 1.3”},
Greetings