44
55import { Client } from '@dolittle/sdk' ;
66import { TenantId } from '@dolittle/sdk.execution' ;
7- import { projection , ProjectionResult } from '@dolittle/sdk.projections' ;
7+ import { ProjectionResult } from '@dolittle/sdk.projections' ;
88import { Menu } from './Menu' ;
99import { Chef } from './Chef' ;
1010import { ChefFired } from './ChefFired' ;
1111import { DishPrepared } from './DishPrepared' ;
12+ import { ScopeId } from '@dolittle/sdk.events' ;
1213
1314const client = Client
1415 . forMicroservice ( 'f39b1f61-d360-4675-b859-53c05c87c0e6' )
@@ -17,39 +18,57 @@ const client = Client
1718 eventTypes . register ( ChefFired ) ;
1819 } )
1920 . withProjections ( projections => {
20- // projections.createProjection('4a4c5b13-d4dd-4665-a9df-27b8e9b2054c')
21- // .forReadModel(Chef)
22- // .on(DishPrepared, _ => _.keyFromProperty('Chef'), (chef, event, ctx) => {
23- // console.log(`Handling event ${JSON.stringify(event)} and read model ${JSON.stringify(chef)}`);
24- // chef.name = event.Chef;
25- // chef.dishes.push(event.Dish);
26- // return chef;
27- // });
28- // .on(ChefFired, _ => _.keyFromProperty('Chef'), (chef, event, ctx) => {
29- // console.log(`Firing ${chef.name}`);
30- // return ProjectionResult.delete;
31- // });
21+ projections . createProjection ( '4a4c5b13-d4dd-4665-a9df-27b8e9b2054c' )
22+ . forReadModel ( Chef )
23+ . on ( DishPrepared , _ => _ . keyFromProperty ( 'Chef' ) , ( chef , event , ctx ) => {
24+ console . log ( `Handling event ${ JSON . stringify ( event ) } and read model ${ JSON . stringify ( chef ) } ` ) ;
25+ chef . name = event . Chef ;
26+ chef . dishes . push ( event . Dish ) ;
27+ return chef ;
28+ } )
29+ . on ( ChefFired , _ => _ . keyFromProperty ( 'Chef' ) , ( chef , event , ctx ) => {
30+ console . log ( `Firing ${ chef . name } ` ) ;
31+ return ProjectionResult . delete ;
32+ } ) ;
3233 projections . register ( Menu ) ;
33- } )
34+ } )
3435 . build ( ) ;
3536
3637const beanBlaster = new DishPrepared ( 'Bean Blaster Taco' , 'Mr. Taco' ) ;
3738const avocadoArtillery = new DishPrepared ( 'Avocado Artillery Tortilla' , 'Mr. Taco' ) ;
3839const chiliCannon = new DishPrepared ( 'Chili Cannon Wrap' , 'Ms. TexMex' ) ;
3940const mrTacoFired = new ChefFired ( 'Mr. Taco' ) ;
4041
41- console . log ( 'starting comit' ) ;
42- client . eventStore
43- . forTenant ( TenantId . development )
44- . commit ( beanBlaster , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
45- client . eventStore
46- . forTenant ( TenantId . development )
47- . commit ( avocadoArtillery , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
48- client . eventStore
49- . forTenant ( TenantId . development )
50- . commit ( chiliCannon , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
51-
52- client . eventStore
53- . forTenant ( TenantId . development )
54- . commit ( mrTacoFired , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
55- console . log ( 'done comitng' ) ;
42+ ( async ( ) => {
43+ client . eventStore
44+ . forTenant ( TenantId . development )
45+ . commit ( beanBlaster , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
46+ client . eventStore
47+ . forTenant ( TenantId . development )
48+ . commit ( avocadoArtillery , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
49+ client . eventStore
50+ . forTenant ( TenantId . development )
51+ . commit ( chiliCannon , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
52+
53+ client . eventStore
54+ . forTenant ( TenantId . development )
55+ . commit ( mrTacoFired , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
56+
57+ const { state : menu } = await client . projections . forTenant ( TenantId . development ) . get ( Menu , 'bfe6f6e4-ada2-4344-8a3b-65a3e1fe16e9' ) ;
58+ console . log ( 'Got menu' , menu ) ;
59+
60+ console . log ( 'Getting all chefs' ) ;
61+ for ( const [ , { key, state : chef } ] of await client . projections . forTenant ( TenantId . development ) . getAll ( Chef ) ) {
62+ console . log ( `Found chef ${ key } ` , chef ) ;
63+ }
64+
65+ console . log ( 'Getting all chefs but specify ids' ) ;
66+ for ( const [ , { key, state : chef } ] of await client . projections . forTenant ( TenantId . development ) . getAll ( Chef , '4a4c5b13-d4dd-4665-a9df-27b8e9b2054d' ) ) {
67+ console . log ( `Found chef ${ key } ` , chef ) ;
68+ }
69+
70+ console . log ( 'Getting all chefs with no type' ) ;
71+ for ( const [ , { key, state : chef } ] of await client . projections . forTenant ( TenantId . development ) . getAll ( '4a4c5b13-d4dd-4665-a9df-27b8e9b2054c' , ScopeId . default ) ) {
72+ console . log ( `Found chef ${ key } ` , chef ) ;
73+ }
74+ } ) ( ) ;
0 commit comments