Skip to content

Commit 7777928

Browse files
Copilothuangyiirene
andcommitted
Split ObjectQL Protocol Specification into focused documents
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent 6e752f6 commit 7777928

12 files changed

Lines changed: 1582 additions & 1288 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
title: Aggregation Operations
3+
description: Complete reference for grouping and aggregating data in ObjectQL
4+
---
5+
6+
# Aggregation Operations
7+
8+
This document provides the complete specification for ObjectQL's Aggregation operations.
9+
10+
## GroupBy
11+
12+
```typescript
13+
db.aggregate(objectName: string, options: AggregateOptions)
14+
15+
interface AggregateOptions {
16+
group_by: string[]
17+
having?: Filter[]
18+
fields?: AggregateField[]
19+
sort?: string | Sort[]
20+
limit?: number
21+
}
22+
```
23+
24+
### Basic GroupBy
25+
26+
```typescript
27+
// Count by category
28+
await db.aggregate('product', {
29+
group_by: ['category'],
30+
fields: [
31+
{ field: '_id', function: 'count', alias: 'total' }
32+
]
33+
})
34+
35+
// Result: [
36+
// { category: 'Electronics', total: 45 },
37+
// { category: 'Books', total: 123 }
38+
// ]
39+
```
40+
41+
### Multiple Groupings
42+
43+
```typescript
44+
// Sales by year and quarter
45+
await db.aggregate('order', {
46+
group_by: ['YEAR(created)', 'QUARTER(created)'],
47+
fields: [
48+
{ field: 'amount', function: 'sum', alias: 'total_sales' },
49+
{ field: '_id', function: 'count', alias: 'order_count' }
50+
],
51+
sort: 'YEAR(created) desc, QUARTER(created) desc'
52+
})
53+
```
54+
55+
## Aggregate Functions
56+
57+
```typescript
58+
// Count
59+
{ field: '_id', function: 'count' }
60+
61+
// Sum
62+
{ field: 'amount', function: 'sum' }
63+
64+
// Average
65+
{ field: 'price', function: 'avg' }
66+
67+
// Min
68+
{ field: 'price', function: 'min' }
69+
70+
// Max
71+
{ field: 'price', function: 'max' }
72+
73+
// Distinct count
74+
{ field: 'customer_id', function: 'count_distinct' }
75+
```
76+
77+
## Having Clause
78+
79+
Filter aggregated results:
80+
81+
```typescript
82+
await db.aggregate('order', {
83+
group_by: ['customer_id'],
84+
fields: [
85+
{ field: 'amount', function: 'sum', alias: 'total_spent' },
86+
{ field: '_id', function: 'count', alias: 'order_count' }
87+
],
88+
having: [
89+
['total_spent', '>', 1000] // Only customers who spent > 1000
90+
]
91+
})
92+
```
93+
94+
## Complete Aggregation Example
95+
96+
```typescript
97+
// Monthly sales report with filters
98+
const monthlySales = await db.aggregate('invoice', {
99+
// Pre-aggregation filters (WHERE clause)
100+
filters: [
101+
['status', '=', 'paid'],
102+
['created', '>=', '2024-01-01']
103+
],
104+
105+
// Group by
106+
group_by: ['YEAR(created)', 'MONTH(created)'],
107+
108+
// Aggregate calculations
109+
fields: [
110+
{ field: 'amount', function: 'sum', alias: 'revenue' },
111+
{ field: '_id', function: 'count', alias: 'invoice_count' },
112+
{ field: 'amount', function: 'avg', alias: 'avg_invoice' },
113+
{ field: 'customer_id', function: 'count_distinct', alias: 'unique_customers' }
114+
],
115+
116+
// Post-aggregation filters (HAVING clause)
117+
having: [
118+
['revenue', '>', 50000] // Only months with revenue > 50k
119+
],
120+
121+
// Sort results
122+
sort: 'YEAR(created) desc, MONTH(created) desc',
123+
124+
// Limit results
125+
limit: 12 // Last 12 months
126+
})
127+
128+
// Result: [
129+
// {
130+
// year: 2024,
131+
// month: 3,
132+
// revenue: 125000,
133+
// invoice_count: 45,
134+
// avg_invoice: 2777.78,
135+
// unique_customers: 32
136+
// },
137+
// ...
138+
// ]
139+
```
140+
141+
## Next Steps
142+
143+
- Learn [Query DSL](/docs/02-objectql/query-dsl) for basic queries
144+
- Master [Mutation Operations](/docs/02-objectql/mutation) for data modification
145+
- Review [Schema Definition](/docs/02-objectql/schema-definition) for data modeling

content/docs/02-objectql/meta.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"index",
66
"core-concepts",
77
"protocol-spec",
8+
"schema-definition",
9+
"query-dsl",
10+
"aggregation",
11+
"mutation",
812
"core-features",
913
"server-sdk"
1014
]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Mutation Operations
3+
description: Complete reference for inserting, updating, and deleting data in ObjectQL
4+
---
5+
6+
# Mutation Operations
7+
8+
This document provides the complete specification for ObjectQL's Mutation operations.
9+
10+
## Insert
11+
12+
```typescript
13+
await db.mutation('customer', {
14+
action: 'insert',
15+
data: {
16+
name: 'Acme Corp',
17+
email: 'contact@acme.com',
18+
status: 'active'
19+
}
20+
})
21+
```
22+
23+
## Update
24+
25+
```typescript
26+
await db.mutation('customer', {
27+
action: 'update',
28+
filters: [['_id', '=', customerId]],
29+
data: {
30+
status: 'inactive',
31+
modified: new Date()
32+
}
33+
})
34+
```
35+
36+
## Delete
37+
38+
```typescript
39+
await db.mutation('customer', {
40+
action: 'delete',
41+
filters: [['status', '=', 'archived']]
42+
})
43+
```
44+
45+
## Batch Operations
46+
47+
### Batch Insert
48+
49+
```typescript
50+
// Batch insert
51+
await db.mutation('product', {
52+
action: 'insert',
53+
data: [
54+
{ name: 'Product 1', price: 10 },
55+
{ name: 'Product 2', price: 20 },
56+
{ name: 'Product 3', price: 30 }
57+
]
58+
})
59+
```
60+
61+
### Batch Update
62+
63+
```typescript
64+
// Batch update
65+
await db.mutation('task', {
66+
action: 'update',
67+
filters: [['project_id', '=', projectId]],
68+
data: { status: 'cancelled' }
69+
})
70+
```
71+
72+
## Next Steps
73+
74+
- Learn [Query DSL](/docs/02-objectql/query-dsl) for querying data
75+
- Explore [Aggregation](/docs/02-objectql/aggregation) for data analysis
76+
- Review [Schema Definition](/docs/02-objectql/schema-definition) for data modeling
77+
- Explore [Core Features](/docs/02-objectql/core-features) for performance optimizations
78+
- Master the [Server SDK](/docs/02-objectql/server-sdk) API reference

0 commit comments

Comments
 (0)