Chapter 3
In this chapter, we will create a projection that utilizes both events from our application: task added and task completed. Projections allow us to transform and store data in a format that is optimized for querying.
Third Usecase: Creating a Projection
-
Create the Projection Usecase:
- Name the readmodel active task. This readmodel will store details about tasks that are currently active.
-
Define Readmodel Properties:
- Add the following properties to the active task readmodel:
task id(UUID): The unique identifier for the task.title(string): The title of the task.description(string): A description of the task.assignee id(UUID): The ID of the user assigned to the task.
- Add the following properties to the active task readmodel:
-
Configure Event Consumption:
-
Add the
task addedEvent:- Set the Projection Operation to
upsert. This operation will insert a new record or update an existing one if thetask idmatches. - Set the Projection Condition: Use the
task idfrom the Metadata ValueSource ofaggregate id.
Define the Value Sources for the properties of the readmodel:
task id: Metadata Value Sourceaggregate idtitle: Schema Value Sourcetitledescription: Schema Value Sourcedescriptionassignee id: Schema Value Sourceassignee id
- Set the Projection Operation to
-
Add the
task completedEvent:- Set the Projection Operation to
delete. This operation will remove the record corresponding to thetask id. - Set the Projection Condition: Use the
task idfrom the Metadata ValueSource ofaggregate id.
No Value Sources are needed here since we are deleting the readmodel entry.
- Set the Projection Operation to
Consuming Events Documentation
Usecase Schema and Data Flows -
Generate the Code
-
Set the Usecase Status:
- Ensure your usecase is set to Ready. This step is crucial as it marks the usecase for code generation.
-
Generate Source Code:
- Verify that your local code generator is running. Generate the source code for the new projection.
Customize the Code
- No additional custom code is required for this projection. The code generated will be sufficient for handling the specified usecases.
Test the Code
-
Run the Tests:
- Execute the tests to ensure that the projection behaves as expected. The tests should cover both the creation and deletion of readmodel entries based on the events.
npm run test-integrationnpm run test-unit