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
512type 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
3340const 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
3845const 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
4350const 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
4855const 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
5461const 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)
5764const 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 ) )
6368const 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
7075const output = commentsForProject . pipe (
7176 concat ( issuesForProject ) ,
7277 concat ( users ) ,
73- debug ( 'output' , true )
78+ debug ( 'output' , true ) ,
7479)
7580
7681graph . finalize ( )
@@ -84,19 +89,64 @@ inputUsers.sendData(v([1, 0]), [
8489
8590// Add some issues
8691inputIssues . 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
93107inputComments . 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
109159inputComments . 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] )
112168inputUsers . sendFrontier ( v ( [ 3 , 0 ] ) )
113169inputIssues . sendFrontier ( v ( [ 3 , 0 ] ) )
@@ -116,7 +172,13 @@ graph.run()
116172
117173// Add a new Comment to an issue in project 2
118174inputComments . 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] )
121183inputUsers . sendFrontier ( v ( [ 4 , 0 ] ) )
122184inputIssues . 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
128190inputIssues . 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] )
132200inputUsers . sendFrontier ( v ( [ 5 , 0 ] ) )
133201inputIssues . 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
139207inputIssues . 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] )
143217inputUsers . sendFrontier ( v ( [ 6 , 0 ] ) )
144218inputIssues . sendFrontier ( v ( [ 6 , 0 ] ) )
145219inputComments . sendFrontier ( v ( [ 6 , 0 ] ) )
146220graph . 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/*
150226Output looks like this:
@@ -413,4 +489,4 @@ debug output data: version: Version([5,0]) collection: MultiSet([
413489debug 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+ */
0 commit comments