|
| 1 | +# [14.3.0-projections.0] - 2021-3-30 [PR: #43](https://github.com/dolittle/JavaScript.SDK/pull/43) |
| 2 | +- Added a new package `projections` in the project. |
| 3 | +- New `client.withProjections()` for building Projections inline |
| 4 | +- New `ProjectionsBuilder` with `createProjection(projectionId)` method that returns a `ProjectionBuilder` that has 3 methods: |
| 5 | + - `inScope(scopeId)` for setting the scope (defaults to default scope) |
| 6 | + - `forReadModel(Constructor<any>)` that takes a class to register as a readmodel for the projection |
| 7 | + - `on()` with all of the variations for using `EventType`s, type arguments or `Generation`s etc |
| 8 | +- I made the API a bit more flat so that you can put the `forReadModel()`, `on()` and `inScope()` methods in whichever order. |
| 9 | + - I don't think we need a constraint on this, if users want to put them in a different order then that's on them. It also doesn't make the code suddenly stop working if you put `forReadModel()` before `inScope()` or stuff like that. |
| 10 | + - This also means that the code is more error prone as it's slightly easier to accidentally call `forReadModel()` twice I guess. This gets checked during the build and it will throw an error in that case. |
| 11 | +- New sample in `Samples/Tutorials/Projections` with a readmodel of all chefs and all the dishes they've prepared |
| 12 | + - This could be enough for the tutorial too, that can be discussed when we get there |
| 13 | +- Code regarding the decorators is not complete as we should just first focus on the inline |
| 14 | +- The `projections` project builds, but the whole repo doesn't as I'm waiting for the `ProjectionsClient` from the new contracts for JS. I've mocked the `ProjectionsClient` in places with a dummy type. |
| 15 | +- You can delete a readmodel by returning a `ProjectionResult.delete` in the `on()` method. |
| 16 | + |
| 17 | +Example how the inline code would look like: |
| 18 | + |
| 19 | +```typescript |
| 20 | +const client = Client |
| 21 | + .forMicroservice('f39b1f61-d360-4675-b859-53c05c87c0e6') |
| 22 | + .withEventTypes(eventTypes => { |
| 23 | + eventTypes.register(DishPrepared); |
| 24 | + eventTypes.register(ChefFired); |
| 25 | + }) |
| 26 | + .withProjections(projections => { |
| 27 | + projections.createProjection('4a4c5b13-d4dd-4665-a9df-27b8e9b2054c') |
| 28 | + .forReadModel(Chef) |
| 29 | + .on(DishPrepared, _ => _.keyFromProperty('Chef'), (chef, event, ctx) => { |
| 30 | + console.log(`Handling event ${JSON.stringify(event)} and read model ${JSON.stringify(chef)}`); |
| 31 | + chef.name = event.Chef; |
| 32 | + chef.dishes.push(event.Dish); |
| 33 | + return chef; |
| 34 | + }) |
| 35 | + .on(ChefFired, _ => _.keyFromProperty('Chef'), (chef, event, ctx) => { |
| 36 | + console.log(`Firing ${chef.name}`); |
| 37 | + return ProjectionResult.delete; |
| 38 | + }) |
| 39 | + }) |
| 40 | + .build(); |
| 41 | +``` |
| 42 | + |
| 43 | +And a diagram on how the builder works: |
| 44 | + |
| 45 | + |
| 46 | + |
1 | 47 | # [14.2.0] - 2021-2-15 [PR: #40](https://github.com/dolittle/JavaScript.SDK/pull/40) |
2 | 48 | ## Summary |
3 | 49 |
|
|
0 commit comments