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
For things like that you would use Aggregates https://hexdocs.pm/ash/aggregates.html
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"?
I'm trying to figure it out reading https://github.com/ash-project/ash_postgres/blob/main/test/support/resources/post.ex
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.
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.
There's no parent relationship. It's a simple SELECT COUNT(*) FROM table.
can you give me a real world example of what you are trying to do?
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.
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 argumentsThe 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.
👋 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
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.
My bad, I probably made it more complicated than it is, I also didn't think of the
Api.count()
functionSo
in your case
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?
The resource also defines an ecto schema
so you can use it with ecto just like before
Perfect. I'll give it a try.
https://hexdocs.pm/ash/Ash.Api.html#callbacks
The callbacks section there are the functions that are defined on each API