Ash FrameworkAF
Ash Framework3y ago
12 replies
miguels

Persist embedded resources as :text instead of :map

In my app I'm using Cocktail (https://hexdocs.pm/cocktail) to manage scheduled events which I'd like to save to a text field using Cocktail.Schedule.to_i_calendar/1.

What I have so far:

I use an embedded schema to generate the API used in LV forms and GraphQL

defmodule Schedule do
  use Ash.Resource,
    data_layer: :embedded,
    extensions: [AshGraphql.Resource]

  graphql do
    type :schedule
  end

  attributes do
    attribute :count, :integer
    // ...
  end

  validations do
    validate numericality(:count, greater_than: 0)
  end
end


Which I use in another resource

attributes do
  integer_primary_key :id

  // ...

  attribute :schedule, Schedule

  // ...
end


And then realized that embedded schemas are persisted as maps and I haven't found a way to change this behaviour

def up do
  alter table(:table) do
    add :schedule, :map
  end
end


I've also briefly tried setting up a custom type instead without much success.
Was this page helpful?