Ciboulette

Ciboulette

Insert String, Integer, Map, List in a jsonb PostgreSQL column

Hi everybody,

I would like to know how can I insert data which can be a Map, String, List, Integer, Float in a jsonb PostgresSQL column ?

PostgreSQL is capable of doing such insertion in a jsonb field. I would like to find a way where Ecto (as an ORM) let me do this. Because currently my field is:

field(:data, :map) and it does not work when I try to insert: "toto" or 12 or [1,2]. And I have no other options, As I do not decide which type of data I’m receiving. Thanks :slight_smile:

Marked As Solved

Eiji

Eiji

@mischov: I think that this should work, but I did not fully tested it.

defmodule Type.Json do
  alias Ecto.Type

  @behaviour Ecto.Type
  @moduledoc """
  To store values that might be any json value.

  Not intended as a general replacement for :map.
  """

  def type, do: :json

  def cast(value) when is_nil(value), do: {:ok, value}
  def cast(value) when is_boolean(value), do: {:ok, value}
  def cast(value) when is_binary(value), do: {:ok, value}
  def cast(value) when is_number(value), do: {:ok, value}
  def cast(value) when is_list(value), do: Type.cast({:array, __MODULE__}, value)
  def cast(value) when is_map(value), do: Type.cast({:map, __MODULE__}, value)
  def cast(_), do: :error

  def load(data), do: {:ok, data}

  def dump(value) when is_nil(value), do: {:ok, value}
  def dump(value) when is_boolean(value), do: {:ok, value}
  def dump(value) when is_binary(value), do: {:ok, value}
  def dump(value) when is_number(value), do: {:ok, value}
  def dump(value) when is_list(value), do: Type.dump({:array, __MODULE__}, value)
  def dump(value) when is_map(value), do: Type.dump({:map, __MODULE__}, value)
  def dump(_), do: :error
end

Also Liked

LostKobrakai

LostKobrakai

You can always create your own. The ecto type is a map type not a json one. In the end ecto is not bound to any db at all and it makes way more sense to have types, which align well to elixir than ones that align well with a certain database.

Last Post!

Eiji

Eiji

Look, I’m not senior developer which would describe it properly. I believe this resource says enough:

Generally we should think about it especially when others (like in above linked article) have do it and explained why. However I agree that it should not be a longer discussion - I just wanted to suggest better alternative solution.

Where Next?

Popular in Questions Top

New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

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
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
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement