|
| 1 | +// Copyright (c) Dolittle. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | +// Sample code for the tutorial at https://dolittle.io/tutorials/getting-started/projections/ |
| 4 | + |
| 5 | +import { Client } from '@dolittle/sdk'; |
| 6 | +import { TenantId } from '@dolittle/sdk.execution'; |
| 7 | +import { projection, ProjectionResult } from '@dolittle/sdk.projections'; |
| 8 | +import { Menu } from './Menu'; |
| 9 | +import { Chef } from './Chef'; |
| 10 | +import { ChefFired } from './ChefFired'; |
| 11 | +import { DishPrepared } from './DishPrepared'; |
| 12 | + |
| 13 | +const client = Client |
| 14 | + .forMicroservice('f39b1f61-d360-4675-b859-53c05c87c0e6') |
| 15 | + .withEventTypes(eventTypes => { |
| 16 | + eventTypes.register(DishPrepared); |
| 17 | + eventTypes.register(ChefFired); |
| 18 | + }) |
| 19 | + .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 | + // }); |
| 32 | + projections.register(Menu); |
| 33 | + }) |
| 34 | + .build(); |
| 35 | + |
| 36 | +const beanBlaster = new DishPrepared('Bean Blaster Taco', 'Mr. Taco'); |
| 37 | +const avocadoArtillery = new DishPrepared('Avocado Artillery Tortilla', 'Mr. Taco'); |
| 38 | +const chiliCannon = new DishPrepared('Chili Cannon Wrap', 'Ms. TexMex'); |
| 39 | +const mrTacoFired = new ChefFired('Mr. Taco'); |
| 40 | + |
| 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'); |
0 commit comments