agonzalezro
Hi folks ![]()
TLDR; I have an idea explained at the end, but I am not sure that’s how I should be doing it in Ash.
I am trying to understand how my domains & resources should look like.
I have a domain Portofolio where I want to store Assets and the price of those Assets for a given date.
A simplified version of what I have is:
defmodule Portfolio.Asset do
attributes do
...
attribute :name, :string, allow_nil?: false
...
end
relationships do
belongs_to :user, Accounts.User, allow_nil?: false
has_many :snapshots, AssetSnapshot
end
end
defmodule Portfolio.AssetSnapshot do
attributes do
...
attribute :amount, :decimal, allow_nil?: false
create_timestamp :created_at
...
end
relationships do
belongs_to :asset, Portfolio.Asset, allow_nil?: false
end
end
With these resources I could “easily” get user.assets and then for each asset a asset.last_snapshot or even have a calculation in the Portfolio for it. But at the moment that I need a bit more flexibility getting, for example, the last year snapshots I think it will get complicated.
I was thinking in adding an intermediary Snapshot for the whole Portfolio and not for the asset that would be simply:
attributes do
...
date :date
...
end
relationships do
has_many :assets, Portfolio.Asset, allow_nil?: false
has_many :asset_snapshots, Porfolio.AssetSnapshot, allow_nil?: false
end
This way the Snapshot just saves the date and I can easily query there and create some calculations.
In the past I did something similar with a JSONB and I think I could get a similar behaviour here with a :map but I am not sure that’s the Ash-way.
Any insight would be more than welcome! ![]()
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
agonzalezro
This is what I was thinking about when I talked about the JSONB:
But I will need to validate that the
asset_snapshotmaps includes real data as I can’t use the reference. Same when I delete et al. It seems more like a workaround than a real solution.WDYT?
zachdaniel
I would personally almost always prefer real tables over using maps/embedded to model types. Something that you could do, for example, is have actions on
Portfoliothat manage its assets, and after any asset is changed you could build and save a full snapshot. You could also snapshot the changing asset as well.agonzalezro
So do you think that the two resources I have defined make sense for the kind of queries I want to do later?
In case I want to query between two dates, for example one year, I might end up with few hundreds of AssetSnapshots vs few dozens in the case of the Snapshot I shared later. Maybe it’s simply an early optimization and nothing to worry about yet.