{@thread.name}

zachdaniel
2023-02-04

zachdaniel:

Its not really documented, and there will lots of rough edges, but its there if people want to play with it.

The general usage looks like this:

MyApi.count(Resource) # count everything
{:ok, 100}

Resource
|> Ash.Query.filter(name == "fred")
|> MyApi.count!() # count with filter, raising on any errors
{:ok, 5}

MyApi.avg(Resource, :score)

## Then, to get the results of multiple aggregates in one go, or to add custom aggregates, you can do the following:

custom = Aggregate.new(Resource, :some_name, :custom, implementation: CustomImplementation) # only postgres supports custom aggregates, and its relatively limited currently
sum = Aggregate.new(Resource, :some_other_name, :sum, field: :points)

query
|> Ash.Query.filter(foo == "bar")
|> MyApi.aggregate!([custom, sum])
%{some_name: 10, sum: 10}

zachdaniel:

As always the logic for porting this to non-postgres data layers is essentially already done. Need to copy it over to mnesia, but ets supports this currently (of course its very inefficient, just fetching all related rows and counting them for example, but we can optimize it some day)