Skip to content

Commit f27d73b

Browse files
authored
Merge pull request #597 from devforth/feature/AdminForth/1563/create-docs-for-resource.aggre
feat: add aggregation methods examples to Data API documentation
2 parents 5287fac + 2b97636 commit f27d73b

1 file changed

Lines changed: 86 additions & 1 deletion

File tree

  • adminforth/documentation/docs/tutorial/03-Customization

adminforth/documentation/docs/tutorial/03-Customization/11-dataApi.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,89 @@ Create INDEX is just for example, you have to use your migrator / ORM to create
280280

281281
First one covers performance for the first query, second one for the second query.
282282
If you did not understand how indexes are created: **get sorted tuple of all fields in filters + all fields in sort,
283-
in order they appear in filters and sort**.
283+
in order they appear in filters and sort**.
284+
285+
## Get aggregated data from database
286+
The aggregate method allows you to compute statistical summaries over database records instead of returning raw rows. It is useful for building analytics, dashboards, charts, and reporting endpoints.
287+
288+
You can combine:
289+
- filters (to narrow down dataset)
290+
- aggregates (to compute metrics like count, average, sum, median)
291+
- grouping (to split results by field or time periods)
292+
This lets you answer questions like:
293+
- How many apartments are listed per day?
294+
- What is the average price per country?
295+
- What is the total revenue per category?
296+
297+
### Available aggregates
298+
- Aggregates.count()
299+
- Aggregates.avg(field)
300+
- Aggregates.sum(field)
301+
- Aggregates.median(field)
302+
303+
### Available grouping
304+
- GroupBy.Field(field)
305+
- GroupBy.DateTrunc(field, unit, timezone)
306+
307+
Example:
308+
```ts
309+
GroupBy.DateTrunc('created_at', 'month', 'Europe/Kyiv')
310+
```
311+
312+
### Response format
313+
```ts
314+
[
315+
{
316+
group: string,
317+
count?: number | string,
318+
avgPrice?: number | null,
319+
sum?: number | null,
320+
medianPrice?: number | null,
321+
}
322+
]
323+
```
324+
325+
### Get daily apartment stats (count, avg, sum, median) for listed apartments
326+
```ts
327+
const rows = await admin.resource('apartments').aggregate(
328+
Filters.EQ('listed', true),
329+
{
330+
count: Aggregates.count(),
331+
avgPrice: Aggregates.avg('price'),
332+
sum: Aggregates.sum('price'),
333+
medianPrice: Aggregates.median('price'),
334+
},
335+
GroupBy.DateTrunc('created_at', 'day', 'Europe/Kyiv'),
336+
);
337+
```
338+
339+
What’s happening here:
340+
- Filters.EQ('listed', true)
341+
→ only apartments that are listed (listed = true)
342+
- aggregates:
343+
count() → number of records in each group
344+
avg('price') → average price
345+
sum('price') → total price
346+
median('price') → median price
347+
- GroupBy.DateTrunc('created_at', 'day', 'Europe/Kyiv')
348+
→ groups data by day (with timezone applied)
349+
350+
### Get apartment stats grouped by country
351+
```ts
352+
const rows = await admin.resource('apartments').aggregate(
353+
[],
354+
{
355+
count: Aggregates.count(),
356+
avgPrice: Aggregates.avg('price'),
357+
sum: Aggregates.sum('price'),
358+
medianPrice: Aggregates.median('price'),
359+
},
360+
GroupBy.Field('country'),
361+
);
362+
```
363+
364+
What is happening here:
365+
- [] → no filters (all records)
366+
- GroupBy.Field('country')
367+
→ grouping by country
368+
- same aggregates (count, avg, sum, median)

0 commit comments

Comments
 (0)