|
4 | 4 |
|
5 | 5 | import { Client } from '@dolittle/sdk'; |
6 | 6 | import { TenantId } from '@dolittle/sdk.execution'; |
7 | | -import { ProjectionResult } from '@dolittle/sdk.projections'; |
8 | | -import { Chef } from './Chef'; |
9 | | -import { ChefFired } from './ChefFired'; |
10 | | -import { ChefHired } from './ChefHired'; |
11 | | -import { DishCounter } from './DishCounter'; |
12 | | -import { DishHandler } from './DishHandler'; |
13 | | -import { DishPrepared } from './DishPrepared'; |
14 | | -import { DishRemoved } from './DishRemoved'; |
| 7 | +import { Employee } from './Employee'; |
| 8 | +import { EmployeeHired } from './EmployeeHired'; |
| 9 | +import { EmployeeRetired } from './EmployeeRetired'; |
| 10 | +import { EmployeeTransferred } from './EmployeeTransferred'; |
15 | 11 |
|
16 | 12 | const client = Client |
17 | 13 | .forMicroservice('f39b1f61-d360-4675-b859-53c05c87c0e6') |
18 | 14 | .withEventTypes(eventTypes => { |
19 | | - eventTypes.register(DishPrepared); |
20 | | - eventTypes.register(DishRemoved); |
21 | | - eventTypes.register(ChefHired); |
22 | | - eventTypes.register(ChefFired); |
| 15 | + eventTypes.register(EmployeeHired); |
| 16 | + eventTypes.register(EmployeeTransferred); |
| 17 | + eventTypes.register(EmployeeRetired); |
23 | 18 | }) |
24 | | - .withEventHandlers(builder => |
25 | | - builder.register(DishHandler)) |
26 | 19 | .withEmbeddings(builder => { |
27 | | - builder.register(DishCounter); |
28 | | - builder |
29 | | - .createEmbedding('999a6aa4-4412-4eaf-a99b-2842cb191e7c') |
30 | | - .forReadModel(Chef) |
31 | | - .compare((receivedState, currentState, context) => { |
32 | | - if (!currentState.name) { |
33 | | - return new ChefHired(receivedState.name); |
34 | | - } |
35 | | - }) |
36 | | - .remove((currentState, context) => new ChefFired(currentState.name)) |
37 | | - .on(ChefHired, (currentState, event, context) => { |
38 | | - currentState.name = event.Chef; |
39 | | - return currentState; |
40 | | - }) |
41 | | - .on(ChefFired, (currentState, event, context) => { |
42 | | - return ProjectionResult.delete; |
43 | | - }); |
| 20 | + builder.register(Employee); |
44 | 21 | }) |
45 | 22 | .build(); |
46 | 23 |
|
47 | 24 | (async () => { |
48 | 25 |
|
| 26 | + // wait for the registration to complete |
49 | 27 | setTimeout(async () => { |
50 | | - |
51 | | - const tacoCounter = new DishCounter(); |
52 | | - tacoCounter.dish = 'Taco'; |
53 | | - tacoCounter.numberOfTimesPrepared = 5; |
54 | | - |
55 | | - const burritoCounter = new DishCounter(); |
56 | | - burritoCounter.dish = 'Burrito'; |
57 | | - burritoCounter.numberOfTimesPrepared = 3; |
58 | | - |
59 | | - const wrapCounter = new DishCounter(); |
60 | | - wrapCounter.dish = 'Wrap'; |
61 | | - wrapCounter.numberOfTimesPrepared = 2; |
62 | | - |
63 | | - await Promise.all([tacoCounter, burritoCounter, wrapCounter] |
64 | | - .map(async counter => { |
65 | | - console.log(`Updating dish: ${counter.dish}`); |
66 | | - await client.embeddings |
67 | | - .forTenant(TenantId.development) |
68 | | - .update(DishCounter, counter.dish, counter); |
69 | | - |
70 | | - const counterState = await client.embeddings |
71 | | - .forTenant(TenantId.development) |
72 | | - .get(DishCounter, counter.dish); |
73 | | - console.log('Got dish:', counterState.state); |
74 | | - |
75 | | - console.log(`Removed dish counter: ${counter}`); |
76 | | - await client.embeddings |
77 | | - .forTenant(TenantId.development) |
78 | | - .remove(DishCounter, counter.dish); |
79 | | - |
80 | | - const deletedCounter = await client.embeddings |
81 | | - .forTenant(TenantId.development) |
82 | | - .get(DishCounter, counter.dish); |
83 | | - console.log(`Got a removed, initial dish counter: ${JSON.stringify(deletedCounter.state)}`); |
84 | | - })); |
85 | | - |
86 | | - const allDishCounters = await client.embeddings |
87 | | - .forTenant(TenantId.development) |
88 | | - .getAll(DishCounter); |
89 | | - console.log(`Got all dishes:\n${[...allDishCounters].map(([key, value]) => `${key.value}: ${JSON.stringify(value.state, undefined, 2)}`).join('\n')}`); |
90 | | - |
91 | | - const dishCounterKeys = await client.embeddings |
92 | | - .forTenant(TenantId.development) |
93 | | - .getKeys(DishCounter); |
94 | | - console.log(`Got all keys: ${dishCounterKeys}`); |
95 | | - |
96 | | - console.log('Hiring chefs!'); |
97 | | - const mrTaco = new Chef('Mr. Taco'); |
98 | | - const mrsTexMex = new Chef('Mrs. TexMex'); |
| 28 | + // mock of the state from the external HR system |
| 29 | + const updatedEmployee = new Employee( |
| 30 | + 'Mr. Taco', |
| 31 | + 'Street Food Taco Truck'); |
99 | 32 |
|
100 | 33 | await client.embeddings |
101 | 34 | .forTenant(TenantId.development) |
102 | | - .update(mrTaco.name, '999a6aa4-4412-4eaf-a99b-2842cb191e7c', mrTaco); |
103 | | - await client.embeddings |
104 | | - .forTenant(TenantId.development) |
105 | | - .update(mrsTexMex.name, '999a6aa4-4412-4eaf-a99b-2842cb191e7c', mrsTexMex); |
| 35 | + .update(Employee, updatedEmployee.name, updatedEmployee); |
| 36 | + console.log(`Updated ${updatedEmployee.name}`); |
106 | 37 |
|
107 | | - console.log('Getting all chefs!'); |
108 | | - const allChefs = await client.embeddings |
109 | | - .forTenant(TenantId.development) |
110 | | - .getAll('999a6aa4-4412-4eaf-a99b-2842cb191e7c'); |
111 | | - console.log(`Got all chefs:\n${[...allChefs].map(([key, value]) => `${key.value}: ${JSON.stringify(value.state, undefined, 2)}`).join('\n')}`); |
112 | | - |
113 | | - console.log('Removing Mr. Taco!'); |
114 | 38 | await client.embeddings |
115 | 39 | .forTenant(TenantId.development) |
116 | | - .remove(mrTaco.name, '999a6aa4-4412-4eaf-a99b-2842cb191e7c'); |
117 | | - const allChefsAgain = await client.embeddings |
118 | | - .forTenant(TenantId.development) |
119 | | - .getAll('999a6aa4-4412-4eaf-a99b-2842cb191e7c'); |
120 | | - console.log(`Getting all chefs again:\n${[...allChefsAgain].map(([key, value]) => `${key.value}: ${JSON.stringify(value.state, undefined, 2)}`).join('\n')}`); |
| 40 | + .delete(Employee, updatedEmployee.name); |
| 41 | + console.log(`Deleted ${updatedEmployee.name}`); |
121 | 42 |
|
122 | | - console.log('Getting Mr. Taco'); |
123 | | - const getMrTaco = await client.embeddings |
| 43 | + await client.embeddings |
124 | 44 | .forTenant(TenantId.development) |
125 | | - .get(mrTaco.name, '999a6aa4-4412-4eaf-a99b-2842cb191e7c'); |
126 | | - console.log('Got removed Mr. Taco:', getMrTaco.state); |
127 | | - |
| 45 | + .getKeys(Employee); |
128 | 46 | }, 1000); |
129 | 47 | })(); |
0 commit comments