Skip to content

Commit e5bfd9e

Browse files
committed
Tidy up examples
1 parent 6aaba32 commit e5bfd9e

File tree

10 files changed

+238
-115
lines changed

10 files changed

+238
-115
lines changed

examples/basic.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { D2 } from '../packages/d2ts/src/index.js'
2-
import { map, filter, debug } from '../packages/d2ts/src/operators/index.js'
1+
import { D2, map, filter, debug } from '@electric-sql/d2ts'
32

43
// Create a new D2 graph with an initial frontier of 0
54
// This is the lower bound of the version space
@@ -27,15 +26,15 @@ graph.finalize()
2726
// The graph will process the data and frontier updates in a single step
2827
for (let i = 1; i <= 10; i++) {
2928
// Sending a frontier is informing the graph what the new lower bound of the version
30-
// space is *on that input*. Each input essentially can have its own lower bound.
29+
// space is *on that input*. Each input essentially can have its own lower bound.
3130
// These are then passed through all the operators
3231
input.sendFrontier(i)
3332

3433
// Sending data to the graph
3534
// The first param is the version of the data
36-
// The second param is a MultiSetArray of *changes in the collection*, where the first
37-
// element is the record and the second is the multiplicity. A positive multiplicity
38-
// means that the record is added to the collection at that version. A negative
35+
// The second param is a MultiSetArray of *changes in the collection*, where the first
36+
// element is the record and the second is the multiplicity. A positive multiplicity
37+
// means that the record is added to the collection at that version. A negative
3938
// multiplicity means that the record is removed from the collection at that version.
4039
input.sendData(i, [
4140
[i + 1, 1],

examples/fruit-processed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { map, reduce, consolidate } from '../packages/d2ts/src/operators/index.js'
2-
import { Store } from '../packages/d2ts/src/store.js'
1+
import { map, reduce, consolidate } from '@electric-sql/d2ts'
2+
import { Store } from '@electric-sql/d2ts/store'
33

44
type FruitOrder = {
55
name: string

examples/includes.ts

Lines changed: 107 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { D2 } from '../packages/d2ts/src/index.js'
2-
import { map, filter, join, concat, distinct, debug } from '../packages/d2ts/src/operators/index.js'
3-
import { v } from '../packages/d2ts/src/order.js'
1+
import {
2+
D2,
3+
map,
4+
filter,
5+
join,
6+
concat,
7+
distinct,
8+
debug,
9+
v,
10+
} from '@electric-sql/d2ts'
411

512
type Issue = {
613
type: 'issue'
@@ -31,46 +38,44 @@ const inputComments = graph.newInput<[number, Comment]>()
3138

3239
// Transform comments into [issue_id, comment] pairs for joining
3340
const commentsByIssue = inputComments.pipe(
34-
map(([id, comment]) => [comment.issue_id, comment] as [number, Comment])
41+
map(([id, comment]) => [comment.issue_id, comment] as [number, Comment]),
3542
)
3643

3744
// Issues for our project
3845
const issuesForProject = inputIssues.pipe(
39-
filter(([id, issue]) => issue.project_id === 1)
46+
filter(([id, issue]) => issue.project_id === 1),
4047
)
4148

4249
// Issues ids for joining with comments
4350
const issueIds = issuesForProject.pipe(
44-
map(([id, issue]) => [issue.id, undefined] as [number, undefined])
51+
map(([id, issue]) => [issue.id, undefined] as [number, undefined]),
4552
)
4653

4754
// Join comments and map back to just the comment
4855
const commentsForProject = commentsByIssue.pipe(
4956
join(issueIds),
50-
map(([id, [comment, _]]) => [comment.id, comment] as [number, Comment])
57+
map(([id, [comment, _]]) => [comment.id, comment] as [number, Comment]),
5158
)
5259

5360
// Users
5461
const usersIdsForIssues = issuesForProject.pipe(
55-
map(([id, issue]) => [issue.owner_id, undefined] as [number, undefined])
62+
map(([id, issue]) => [issue.owner_id, undefined] as [number, undefined]),
5663
)
5764
const usersIdsForComments = commentsForProject.pipe(
58-
map(([id, comment]) => [comment.owner_id, undefined] as [number, undefined])
59-
)
60-
const usersIds = usersIdsForIssues.pipe(
61-
concat(usersIdsForComments)
65+
map(([id, comment]) => [comment.owner_id, undefined] as [number, undefined]),
6266
)
67+
const usersIds = usersIdsForIssues.pipe(concat(usersIdsForComments))
6368
const users = usersIds.pipe(
6469
join(inputUsers),
6570
map(([id, [_, user]]) => [id, user] as [number, User]),
66-
distinct()
71+
distinct(),
6772
)
6873

6974
// Concat comments and issues and output the result
7075
const output = commentsForProject.pipe(
7176
concat(issuesForProject),
7277
concat(users),
73-
debug('output', true)
78+
debug('output', true),
7479
)
7580

7681
graph.finalize()
@@ -84,19 +89,64 @@ inputUsers.sendData(v([1, 0]), [
8489

8590
// Add some issues
8691
inputIssues.sendData(v([1, 0]), [
87-
[[1, { type: 'issue', id: 1, project_id: 1, title: 'Issue 1', owner_id: 1 }], 1],
88-
[[2, { type: 'issue', id: 2, project_id: 2, title: 'Issue 2', owner_id: 2 }], 1],
89-
[[3, { type: 'issue', id: 3, project_id: 1, title: 'Issue 3', owner_id: 3 }], 1],
92+
[
93+
[1, { type: 'issue', id: 1, project_id: 1, title: 'Issue 1', owner_id: 1 }],
94+
1,
95+
],
96+
[
97+
[2, { type: 'issue', id: 2, project_id: 2, title: 'Issue 2', owner_id: 2 }],
98+
1,
99+
],
100+
[
101+
[3, { type: 'issue', id: 3, project_id: 1, title: 'Issue 3', owner_id: 3 }],
102+
1,
103+
],
90104
])
91105

92106
// Add some comments
93107
inputComments.sendData(v([1, 0]), [
94-
[[1, { type: 'comment', id: 1, issue_id: 1, text: 'Comment 1', owner_id: 1 }], 1],
95-
[[2, { type: 'comment', id: 2, issue_id: 1, text: 'Comment 2', owner_id: 3 }], 1],
96-
[[3, { type: 'comment', id: 3, issue_id: 2, text: 'Comment 3', owner_id: 1 }], 1],
97-
[[4, { type: 'comment', id: 4, issue_id: 2, text: 'Comment 4', owner_id: 3 }], 1],
98-
[[5, { type: 'comment', id: 5, issue_id: 3, text: 'Comment 5', owner_id: 1 }], 1],
99-
[[6, { type: 'comment', id: 6, issue_id: 3, text: 'Comment 6', owner_id: 3 }], 1],
108+
[
109+
[
110+
1,
111+
{ type: 'comment', id: 1, issue_id: 1, text: 'Comment 1', owner_id: 1 },
112+
],
113+
1,
114+
],
115+
[
116+
[
117+
2,
118+
{ type: 'comment', id: 2, issue_id: 1, text: 'Comment 2', owner_id: 3 },
119+
],
120+
1,
121+
],
122+
[
123+
[
124+
3,
125+
{ type: 'comment', id: 3, issue_id: 2, text: 'Comment 3', owner_id: 1 },
126+
],
127+
1,
128+
],
129+
[
130+
[
131+
4,
132+
{ type: 'comment', id: 4, issue_id: 2, text: 'Comment 4', owner_id: 3 },
133+
],
134+
1,
135+
],
136+
[
137+
[
138+
5,
139+
{ type: 'comment', id: 5, issue_id: 3, text: 'Comment 5', owner_id: 1 },
140+
],
141+
1,
142+
],
143+
[
144+
[
145+
6,
146+
{ type: 'comment', id: 6, issue_id: 3, text: 'Comment 6', owner_id: 3 },
147+
],
148+
1,
149+
],
100150
])
101151

102152
// Send frontiers
@@ -107,7 +157,13 @@ graph.run()
107157

108158
// Add a new Comment to an issue in project 1
109159
inputComments.sendData(v([2, 0]), [
110-
[[7, { type: 'comment', id: 7, issue_id: 1, text: 'Comment 7', owner_id: 1 }], 1],
160+
[
161+
[
162+
7,
163+
{ type: 'comment', id: 7, issue_id: 1, text: 'Comment 7', owner_id: 1 },
164+
],
165+
1,
166+
],
111167
])
112168
inputUsers.sendFrontier(v([3, 0]))
113169
inputIssues.sendFrontier(v([3, 0]))
@@ -116,7 +172,13 @@ graph.run()
116172

117173
// Add a new Comment to an issue in project 2
118174
inputComments.sendData(v([3, 0]), [
119-
[[8, { type: 'comment', id: 8, issue_id: 2, text: 'Comment 8', owner_id: 1 }], 1],
175+
[
176+
[
177+
8,
178+
{ type: 'comment', id: 8, issue_id: 2, text: 'Comment 8', owner_id: 1 },
179+
],
180+
1,
181+
],
120182
])
121183
inputUsers.sendFrontier(v([4, 0]))
122184
inputIssues.sendFrontier(v([4, 0]))
@@ -126,8 +188,14 @@ console.log('> Comment 8 should not be included in the output above')
126188

127189
// Move issue 2 to project 1
128190
inputIssues.sendData(v([4, 0]), [
129-
[[2, { type: 'issue', id: 2, project_id: 2, title: 'Issue 2', owner_id: 2 }], -1],
130-
[[2, { type: 'issue', id: 2, project_id: 1, title: 'Issue 2', owner_id: 2 }], 1],
191+
[
192+
[2, { type: 'issue', id: 2, project_id: 2, title: 'Issue 2', owner_id: 2 }],
193+
-1,
194+
],
195+
[
196+
[2, { type: 'issue', id: 2, project_id: 1, title: 'Issue 2', owner_id: 2 }],
197+
1,
198+
],
131199
])
132200
inputUsers.sendFrontier(v([5, 0]))
133201
inputIssues.sendFrontier(v([5, 0]))
@@ -137,14 +205,22 @@ console.log('> Issue 2 and its comments should be included in the output above')
137205

138206
// Move issue 2 back to project 2
139207
inputIssues.sendData(v([5, 0]), [
140-
[[2, { type: 'issue', id: 2, project_id: 1, title: 'Issue 2', owner_id: 2 }], -1],
141-
[[2, { type: 'issue', id: 2, project_id: 2, title: 'Issue 2', owner_id: 2 }], 1],
208+
[
209+
[2, { type: 'issue', id: 2, project_id: 1, title: 'Issue 2', owner_id: 2 }],
210+
-1,
211+
],
212+
[
213+
[2, { type: 'issue', id: 2, project_id: 2, title: 'Issue 2', owner_id: 2 }],
214+
1,
215+
],
142216
])
143217
inputUsers.sendFrontier(v([6, 0]))
144218
inputIssues.sendFrontier(v([6, 0]))
145219
inputComments.sendFrontier(v([6, 0]))
146220
graph.run()
147-
console.log('> Issue 2 and its comments should have a multiplicity of -1 in the output above')
221+
console.log(
222+
'> Issue 2 and its comments should have a multiplicity of -1 in the output above',
223+
)
148224

149225
/*
150226
Output looks like this:
@@ -413,4 +489,4 @@ debug output data: version: Version([5,0]) collection: MultiSet([
413489
debug output notification: frontier Antichain([[6,0]])
414490
> Issue 2 and its comments should have a multiplicity of -1 in the output above
415491
416-
*/
492+
*/

examples/iterate.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { D2 } from '../packages/d2ts/src/index.js'
2-
import { MultiSet } from '../packages/d2ts/src/multiset.js'
3-
import { Antichain, v } from '../packages/d2ts/src/order.js'
41
import {
5-
iterate,
6-
debug,
7-
map,
2+
D2,
3+
Antichain,
4+
concat,
85
consolidate,
6+
debug,
97
distinct,
108
filter,
11-
concat,
12-
} from '../packages/d2ts/src/operators/index.js'
9+
iterate,
10+
map,
11+
MultiSet,
12+
v,
13+
} from '@electric-sql/d2ts'
1314

1415
const graph = new D2({ initialFrontier: new Antichain([v(0)]) })
1516

0 commit comments

Comments
 (0)