https://github.com/tfwright/agar
Dynamic query aggregation for Ecto
https://github.com/tfwright/agar
Last synced: 4 months ago
JSON representation
Dynamic query aggregation for Ecto
- Host: GitHub
- URL: https://github.com/tfwright/agar
- Owner: tfwright
- Created: 2021-04-07T00:09:33.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-10-10T23:20:52.000Z (over 3 years ago)
- Last Synced: 2024-05-02T00:22:35.508Z (about 2 years ago)
- Language: Elixir
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Agar
Dynamically build aggregate queries in Ecto from simple configs that can easily be encoded as URL params.
## Usage
```
MySchema.aggregate(
fields: [:name, :email, some_assoc: :id],
assocs: [other_assocs: [numeric_field: [:sum, :avg]]],
)
=> [%{"name" => "hi", "email" => "email@example.com", "other_assoc_numeric_field_avg" => 3, "other_assoc_numeric_field_sum" => 10}]
```
or: `MySchema.aggregate(["name", "email", "some_assoc_id", "sum_other_assocs_numeric_field", "avg_other_assocs_numeric_field"])`
In the former version, you can use any association and any field defined on the schemas. In the latter, only associations that themselves use Agar and fields specified in the whitelist option will be permitted, as shown below. Other values will cause an error.
```
defmodule MySchema do
use Agar, whitelist: [:name, :email]
#...
belongs_to :some_assoc, AssocSchema
has_many :other_assocs, OtherAssocSchema
end
defmodule AssocSschema do
use Agar, whitelist: [:id]
end
defmodule OtherAssocSchema do
use Agar, whitelist: [:numeric_field]
end
```
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `agar` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:agar, "~> 0.1.0"}
]
end
```
## Development
* Prepare test database: `MIX_ENV=test mix do ecto.create, ecto.migrate`