Skip to content

Commit

Permalink
SDK Fix aggregation on multiple fields (directus#19965)
Browse files Browse the repository at this point in the history
  • Loading branch information
br41nslug authored and br-rafaelbarros committed Nov 7, 2023
1 parent a4ba42e commit 5277564
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/dry-ravens-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"docs": patch
"@directus/sdk": patch
---

Fixed aggregation on multiple fields in the SDK
49 changes: 44 additions & 5 deletions docs/reference/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,42 @@ The following aggregation functions are available in Directus:
| `max` | Return the highest value in the field |
| `countAll` | Equivalent to `?aggregate[count]=*` (GraphQL only) |

<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
<template #rest>

```
?aggregate[count]=*
```

</template>
<template #graphql>

```graphql
query {
articles_aggregated {
countAll
}
}
```

</template>
<template #sdk>

```js
import { createDirectus, rest, aggregate } from '@directus/sdk';

const client = createDirectus('https://directus.example.com').with(rest());

const result = await client.request(
aggregate('articles', {
aggregate: { count: '*' },
})
);
```

</template>
</SnippetToggler>

### Grouping

By default, the above aggregation functions run on the whole dataset. To allow for more flexible reporting, you can
Expand All @@ -499,7 +535,7 @@ this allows for aggregate reporting per year-month-date.
<template #rest>

```
?aggregate[avg]=cost
?aggregate[count]=views,comments
&groupBy[]=author
&groupBy[]=year(publish_date)
```
Expand All @@ -511,8 +547,9 @@ this allows for aggregate reporting per year-month-date.
query {
articles_aggregated(groupBy: ["author", "year(publish_date)"]) {
group
sum {
revenue
count {
views
comments
}
}
}
Expand All @@ -528,8 +565,10 @@ const client = createDirectus('https://directus.example.com').with(rest());

const result = await client.request(
aggregate('articles', {
aggregate: { count: '*' },
groupBy: 'authors',
aggregate: {
count: ['views', 'comments']
},
groupBy: ['authors', 'year(publish_date)'],
})
);
```
Expand Down
15 changes: 12 additions & 3 deletions sdk/src/types/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export type AggregationTypes = {
* Aggregation parameters
*/
export type AggregateRecord<Fields = string> = {
[Func in keyof AggregationTypes]?: Fields | (AggregationTypes[Func]['wildcard'] extends never ? never : '*');
[Func in keyof AggregationTypes]?:
| Fields
| Fields[]
| (AggregationTypes[Func]['wildcard'] extends never ? never : '*');
};

/**
Expand Down Expand Up @@ -96,10 +99,16 @@ export type AggregationOutput<
: never
: object) & {
[Func in keyof Options['aggregate']]: Func extends keyof AggregationTypes
? Options['aggregate'][Func] extends string
? Options['aggregate'][Func] extends string[]
? {
[Field in UnpackList<Options['aggregate'][Func]>]: Field extends '*'
? AggregationTypes[Func]['output']
: { [SubField in Field]: AggregationTypes[Func]['output'] }[Field];
}
: Options['aggregate'][Func] extends string
? Options['aggregate'][Func] extends '*'
? AggregationTypes[Func]['output']
: { [Field in Options['aggregate'][Func]]: AggregationTypes[Func]['output'] }
: { [SubField in Options['aggregate'][Func]]: AggregationTypes[Func]['output'] }[Options['aggregate'][Func]]
: never
: never;
})[];
Expand Down

0 comments on commit 5277564

Please sign in to comment.