Ash.Query getting started docs

I need some help to figure out how to translate Ecto queries into Ash ones. I've already read the docs, but I'm still stucked on simple "SELECT COUNT(*) FROM table" and "SELECT MIN(field) FROM table". Where do I find such explanation? Thanks in advance.
16 Replies
barnabasj
barnabasj•2y ago
For things like that you would use Aggregates https://hexdocs.pm/ash/aggregates.html
tellesleandro
tellesleandroOP•2y ago
Thanks. I tried, but I'm still stucked. Maybe I missed something. Could you give an example how to write these queries: "SELECT COUNT(*) FROM table" and "SELECT MIN(field) FROM table"?
tellesleandro
tellesleandroOP•2y ago
GitHub
ash_postgres/test/support/resources/post.ex at main · ash-project/a...
A postgresql datalayer for the Ash Framework. Contribute to ash-project/ash_postgres development by creating an account on GitHub.
barnabasj
barnabasj•2y ago
What might be important is that you set up the aggregates on the "parent" resource. You add the relationships to the resource and then you can run aggregates on them.
tellesleandro
tellesleandroOP•2y ago
There's no parent relationship. It's a simple SELECT COUNT(*) FROM table.
barnabasj
barnabasj•2y ago
can you give me a real world example of what you are trying to do?
tellesleandro
tellesleandroOP•2y ago
There is a dashboard that shows the amount of cars parked in a parking lot in a day. The table is parked_cars and it has a timestamp field (the time the car arrived at the parking lot). I have to count how many cars is parked during a timespan. Something like SELECT COUNT(*) FROM parked_cars WHERE timestamp_field BETWEEN start_time AND end_time. start_time AND end_time are set by the user. The timestamp field could be the inserted_at field. Sorry for disturbing your meeting with Zach, but I need this initial guidance to move on.
barnabasj
barnabasj•2y ago
the easiest way would probably be to just add a calulation and use the fragment. I would probably model it a bit differently https://hexdocs.pm/ash/calculations.html https://hexdocs.pm/ash_postgres/postgres-expressions.html#fragments but to me it feels a bit wrong, I would probably create some kind of parking_space resource and that would have_many cars and add the aggregate on the parking_space but you would probably need to call the aggregate through a calculation because aggregates themself do not support arguments
tellesleandro
tellesleandroOP•2y ago
The lack of this functionality in Ash sounds like I'm doing something wrong, but what I need is just as simple as a SELECT COUNT. Why this is not possible? Thanks for your support.
ZachDaniel
ZachDaniel•2y ago
👋 Ash is not meant to replace anything you may have used ecto for. Its not a database driver like Ecto is however, we have the functionality you are looking for
Resource
|> Ash.Query.for_read(...)
|> YourApi.count()
Resource
|> Ash.Query.for_read(...)
|> YourApi.count()
To rephrase: Ash is not meant to replace everything you may have used ecto for sometimes you'd still need to use it in some cases, like running complex sql queries.
barnabasj
barnabasj•2y ago
My bad, I probably made it more complicated than it is, I also didn't think of the Api.count() function
ZachDaniel
ZachDaniel•2y ago
So
Resource
|> Ash.Query.filter(timestamp_field > ^users_start_time and timestamp_field < ^users_end_time)
|> YourAPi.count!()
Resource
|> Ash.Query.filter(timestamp_field > ^users_start_time and timestamp_field < ^users_end_time)
|> YourAPi.count!()
in your case
tellesleandro
tellesleandroOP•2y ago
Wow. Trying right now. Thanks. Bingo. Simple like that. Worked like a charm. Thank you all. BTW, is there a documentation for this? How do I use Ecto with Ash.Resource and w/o the Ecto.Schema?
barnabasj
barnabasj•2y ago
The resource also defines an ecto schema so you can use it with ecto just like before
tellesleandro
tellesleandroOP•2y ago
Perfect. I'll give it a try.
ZachDaniel
ZachDaniel•2y ago
https://hexdocs.pm/ash/Ash.Api.html#callbacks The callbacks section there are the functions that are defined on each API

Did you find this page helpful?