Skip to main content

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

  1. Create the Projection Usecase:

    • Name the readmodel active task. This readmodel will store details about tasks that are currently active.

    Learn About Usecases
    Usecase Naming Conventions

  2. 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.

    Usecase Schema Details
    Data Flows for Usecases

  3. Configure Event Consumption:

    • Add the task added Event:

      • Set the Projection Operation to upsert. This operation will insert a new record or update an existing one if the task id matches.
      • Set the Projection Condition: Use the task id from the Metadata ValueSource of aggregate id.

      Define the Value Sources for the properties of the readmodel:

      • task id: Metadata Value Source aggregate id
      • title: Schema Value Source title
      • description: Schema Value Source description
      • assignee id: Schema Value Source assignee id
    • Add the task completed Event:

      • Set the Projection Operation to delete. This operation will remove the record corresponding to the task id.
      • Set the Projection Condition: Use the task id from the Metadata ValueSource of aggregate id.

      No Value Sources are needed here since we are deleting the readmodel entry.

    Consuming Events Documentation
    Usecase Schema and Data Flows

Generate the Code

  1. Set the Usecase Status:

    • Ensure your usecase is set to Ready. This step is crucial as it marks the usecase for code generation.

    Usecase Status Documentation

  2. Generate Source Code:

    • Verify that your local code generator is running. Generate the source code for the new projection.

    Code Generation Instructions

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

  1. 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-integration
    npm run test-unit