Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/clear-impalas-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/d2mini': patch
---

make aggregates explicitly optional on a groupBy
2 changes: 1 addition & 1 deletion packages/d2mini/src/operators/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function groupBy<
T,
K extends GroupKey,
A extends Record<string, AggregateFunction<T, any, any>>,
>(keyExtractor: (data: T) => K, aggregates: A) {
>(keyExtractor: (data: T) => K, aggregates: A = {} as A) {
type ResultType = K & AggregatesReturnType<T, A>

const basicAggregates = Object.fromEntries(
Expand Down
56 changes: 56 additions & 0 deletions packages/d2mini/tests/operators/groupBy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,62 @@ import { output } from '../../src/operators/index.js'

describe('Operators', () => {
describe('GroupBy operation', () => {
test('with no aggregate', () => {
const graph = new D2()
const input = graph.newInput<{
category: string
amount: number
}>()
let latestMessage: any = null

input.pipe(
groupBy((data) => ({ category: data.category })),
output((message) => {
latestMessage = message
}),
)

graph.finalize()

// Initial data
input.sendData(
new MultiSet([
[{ category: 'A', amount: 10 }, 1],
[{ category: 'A', amount: 20 }, 1],
[{ category: 'B', amount: 30 }, 1],
]),
)
graph.run()

// Verify we have the latest message
expect(latestMessage).not.toBeNull()

const result = latestMessage.getInner()

const expectedResult = [
[
[
`{"category":"A"}`,
{
category: 'A',
},
],
1,
],
[
[
`{"category":"B"}`,
{
category: 'B',
},
],
1,
],
]

expect(result).toEqual(expectedResult)
})

test('with single sum aggregate', () => {
const graph = new D2()
const input = graph.newInput<{
Expand Down