Ecto.Schema to_map and queries results as map

374 views
Skip to first unread message

Aeliton Silva

unread,
Sep 14, 2017, 9:39:14 AM9/14/17
to elixir-ecto
Hi all,

I have to implement a functionality that sends schema data to another application instance and I'm not interested in serialize __struct__ and __meta__ fields existing in my schemas. To do so I have implemented a recursive function that removes them, but I think would be nice to have an option to either remove them from the Ecto.Schema API and from the Ecto Queries select (something similar to map() but without explicit which fields will compose the resulting map).

To remove from Ecto.Schema api we would have to call MySchema.to_map.

To remove from a query result we would have something like: from x in MySchema, select: map(x).

Using repo we could have: Repo.all(MySchema) |> Repo.to_map

There's a problem for not loaded associations, since would not be possible to get all the data in this scenario. In this scenario the map function would return that it couldn't perform the operation.

I'm not sure this is something sufficiently general or important to be part of Ecto, but I'm interested in knowing what you guys think.

Thanks,
Aeliton

OvermindDL1

unread,
Sep 14, 2017, 3:50:09 PM9/14/17
to elixir-ecto
> To remove from a query result we would have something like: from x in MySchema, select: map(x).

I actually needed precisely this recently, ended up having to duplicate all the field names (over 40).

It would be nice to add a `map/1` to compliment the currently existing `map/2`.
Reply all
Reply to author
Forward
0 new messages