Hello,
I have a question that probably is somewhat opinion-based.
Basically, when you have a schema for some persisted data in your database, do you use it directly in your code or do you convert it to another struct first?
Example
Say, I have a table to store financial data:
schema "candles" do
field :timestamp, :utc_datetime_usec
field :open, :decimal
field :close, :decimal
field :high, :decimal
field :low, :decimal
field :volume, :decimal
end
It’s typespec would be like this:
@type t :: %__MODULE__{
__meta__: Ecto.Schema.Metadata.t(),
id: integer | nil,
timestamp: DateTime.t() | nil,
open: Decimal.t() | nil,
close: Decimal.t() | nil,
high: Decimal.t() | nil,
low: Decimal.t() | nil,
volume: Decimal.t() | nil
}
Normally what I do is create a struct like this in another module:
defmodule Candle do
defstruct [:timestamp, :open, :close, :high, :low, :volume]
end
And whenever I read from the database, I always convert from the schema to the struct I created, and that struct is what is used in the rest of the program.
Do you think this is necessary or just a waste of CPU cycles? Can you see any advantages/disadvantages from each approach?