POST /aggregate
Spesifikasi endpoint aggregate untuk operasi agregasi data
Endpoint /aggregate menyediakan fungsi agregasi data seperti COUNT, SUM, AVG, MIN, dan MAX dengan dukungan GROUP BY, HAVING, dan JOIN.
Referensi Cepat (Quick Reference)
| Properti | Nilai |
|---|---|
| Method | POST |
| URL | /api/{project}/{endpoint}/aggregate |
| Content-Type | application/json |
| Status Sukses | 200 OK |
| Database | PostgreSQL, MySQL, Oracle |
| Fungsi | count, sum, avg, min, max |
| Fitur | GROUP BY, HAVING, JOIN, WHERE, sort_columns |
Ikhtisar (Overview)
Endpoint /aggregate menjalankan fungsi agregasi pada data endpoint. Tanpa GROUP BY, response berisi satu object hasil agregasi. Dengan GROUP BY, response berisi array data yang dikelompokkan berdasarkan kolom tertentu.
Endpoint ini memerlukan konfigurasi aggregate: true di property action pada payload.
Format Request (Request Format)
Parameter (Parameters)
| Parameter | Tipe | Wajib | Default | Keterangan |
|---|---|---|---|---|
operations | array | Tidak | [{function: "count", field: "*"}] | Array operasi agregasi |
group_by | array | Tidak | — | Kolom untuk pengelompokan |
having | object | Tidak | — | Filter pada hasil agregasi (hanya jika GROUP BY ada) |
joins | array | Tidak | — | Konfigurasi JOIN ke tabel lain |
where | object/array | Tidak | — | Kondisi filter sebelum agregasi |
sort_columns | array | Tidak | — | Pengurutan hasil |
limit | number | Tidak | — | Batas jumlah baris hasil (maksimal 10000) |
Format Operasi (Operation Format)
Setiap item di array operations:
| Field | Tipe | Wajib | Keterangan |
|---|---|---|---|
function | string | Ya | "count", "sum", "avg", "min", atau "max" |
field | string | Ya | Nama kolom target. "*" hanya valid untuk count |
alias | string | Tidak | Nama alias untuk hasil (default: {function}_{field}) |
Contoh Request (Request Examples)
Count sederhana:
{
"operations": [
{ "function": "count", "field": "*", "alias": "total_supplier" }
]
}Beberapa fungsi agregasi sekaligus:
{
"operations": [
{ "function": "count", "field": "*", "alias": "total_items" },
{ "function": "sum", "field": "stock", "alias": "total_stock" },
{ "function": "avg", "field": "selling_price", "alias": "avg_price" }
]
}GROUP BY dengan HAVING:
{
"operations": [
{ "function": "count", "field": "*", "alias": "item_count" },
{ "function": "sum", "field": "stock", "alias": "total_stock" }
],
"group_by": ["category_id"],
"having": {
"conditions": [
{ "key": "total_stock", "operator": ">=", "value": 100 }
]
},
"sort_columns": [
{ "column": "total_stock", "direction": "DESC" }
]
}Agregasi dengan JOIN:
{
"operations": [
{ "function": "count", "field": "*", "alias": "item_count" },
{ "function": "sum", "field": "stock", "alias": "total_stock" }
],
"joins": [
{
"tableName": "category",
"joinType": "LEFT",
"sourceField": "category_id",
"targetField": "category_id",
"fields": ["category_name"]
}
],
"group_by": ["category_name"]
}Format Response (Response Format)
Tanpa GROUP BY (Without GROUP BY)
Response berisi satu object hasil agregasi:
{
"success": true,
"data": {
"total_supplier": 25
},
"timestamp": "2026-04-16T10:30:00.000Z"
}Dengan GROUP BY (With GROUP BY)
Response berisi array data yang dikelompokkan:
{
"success": true,
"data": [
{ "category_name": "Electronics", "item_count": 15, "total_stock": 450 },
{ "category_name": "Furniture", "item_count": 8, "total_stock": 120 }
],
"timestamp": "2026-04-16T10:30:00.000Z"
}Response Error (Error Responses)
400 — Fungsi agregasi tidak valid:
{
"success": false,
"error": "Validation error",
"message": "Invalid aggregate function \"median\"",
"timestamp": "2026-04-16T10:30:00.000Z"
}400 — Wildcard pada fungsi selain COUNT:
{
"success": false,
"error": "Validation error",
"message": "Wildcard (*) can only be used with count function",
"timestamp": "2026-04-16T10:30:00.000Z"
}400 — HAVING tanpa GROUP BY:
{
"success": false,
"error": "Validation error",
"message": "HAVING clause requires GROUP BY",
"timestamp": "2026-04-16T10:30:00.000Z"
}Konfigurasi JOIN (JOIN Configuration)
Setiap item di array joins:
| Field | Tipe | Wajib | Keterangan |
|---|---|---|---|
tableName | string | Ya | Nama tabel yang akan di-JOIN |
joinType | string | Tidak | "LEFT", "INNER", atau "RIGHT" (default: "LEFT") |
sourceField | string | Ya | Kolom foreign key di tabel utama |
targetField | string | Ya | Kolom primary key di tabel target |
fields | array | Ya | Kolom dari tabel target yang diambil |
Langkah Selanjutnya (Next Steps)
- POST /read untuk pengambilan data dengan filter dan pagination
- POST /datatables untuk server-side processing DataTables
- Kode Error untuk referensi lengkap HTTP status code