Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/add-string-agg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tanstack/db-ivm': patch
'@tanstack/db': patch
---

Add `stringAgg` aggregate function for concatenating string values within groups. Supports configurable separators and ordering with efficient incremental maintenance via binary search and fast-path text splicing for head/tail changes.
12 changes: 11 additions & 1 deletion docs/guides/live-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ const userStats = createCollection(liveQueryCollectionOptions({
Use various aggregate functions to summarize your data:

```ts
import { count, sum, avg, min, max } from '@tanstack/db'
import { count, sum, avg, min, max, stringAgg } from '@tanstack/db'

const orderStats = createCollection(liveQueryCollectionOptions({
query: (q) =>
Expand All @@ -1136,6 +1136,7 @@ const orderStats = createCollection(liveQueryCollectionOptions({
avgOrderValue: avg(order.amount),
minOrder: min(order.amount),
maxOrder: max(order.amount),
statusTimeline: stringAgg(order.status, ' -> ', order.createdAt),
}))
}))
```
Expand Down Expand Up @@ -2254,6 +2255,15 @@ min(user.salary)
max(order.amount)
```

#### `stringAgg(value)`, `stringAgg(value, orderBy)`, `stringAgg(value, separator)`, `stringAgg(value, separator, orderBy)`
Concatenate string values within each group. When `orderBy` is omitted, TanStack DB falls back to the source row key for deterministic ordering:
```ts
stringAgg(delta.text) // Deterministic fallback order by row key
stringAgg(delta.text, delta.createdAt) // Ordered by createdAt
stringAgg(delta.text, ' ') // Custom separator with fallback order by row key
stringAgg(delta.text, ' ', delta.seq) // Ordered by seq with separator
```

### Function Composition

Functions can be composed and chained:
Expand Down
Loading
Loading