In short I’m trying to store a subscription object
Which also looks something like.
{
"subscription":{
"endpoint":"https://fcm.googleapis.com/fcm/send/...",
"expirationTime":null,
"keys":{
"p256dh":"...",
"auth":"..."
}
}
}
Ideally since I don’t really see much value in trying to normalize the json into a flat structor I’d like to just shovel this into a field called a subscription.
I was looking at embedded_schema but I’m not sure if I how I would deal with nodes ie:
"keys":{
"p256dh":"...",
"auth":"..."
}
I tried doing something like.
defmodule PolymorphicProductions.Messages.Subscription do
use Ecto.Schema
import Ecto.Changeset
alias PolymorphicProductions.Messages.Subscription.Keys
embedded_schema do
field(:endpoint)
field(:expirationTime)
embeds_one(:keys, Keys)
end
def changeset(schema, params) do
schema
|> cast(params, [:endpoint, :expirationTime])
|> put_keys(params)
end
def put_keys(cs, %{keys: keys} \\ %{keys: %{}}) do
keys =
%Keys{}
|> Keys.changeset(keys)
cs
|> cast_embed(:keys, keys)
end
def put_keys(cs, _), do: cs
end
defmodule PolymorphicProductions.Messages.Subscription.Keys do
use Ecto.Schema
import Ecto.Changeset
embedded_schema do
field(:p256dh)
field(:auth)
end
def changeset(schema, params) do
schema
|> cast(params, [:p256dh, :auth])
end
end
then in my main schema
schema "push_subscribers" do
embeds_one(:subscription, Subscription)
timestamps()
end
I don’t have this working well for me at the moment and it feels really wrong already. So I figured before I keep going down this path I’d thought I would check in with you all to see if I’m way off base.
PS: Also I’m using a real jsonb column not Array::Jsonb
ie:
def up do
alter table("push_subscribers") do
add(:subscription, :jsonb, default: "[]")
end
execute("CREATE INDEX subscriptions_gin ON push_subscribers USING GIN (subscription);")
end
def down do
alter table("push_subscribers") do
remove(:subscription, :jsonb)
end
execute("DROP INDEX subscriptions_gin;")
end
note the default: "[]"