Skip to main content

Chapter 1

In this chapter, you will learn how to:

  • Set up your first aggregate and use case.
  • Generate code and understand how to customize it.
  • Set up and run tests for your application.

Setting Up Your First Aggregate

  1. Navigate to the Bounded Context:

    • Enter the Bounded Context named task management.
  2. Create the Aggregate:

    • We will begin by creating an aggregate called task. Aggregates are a key concept in CQRS and event sourcing, acting as a consistency boundary within your domain.

    Learn More About Aggregates

Creating Your First Usecase

  1. Add a Command API Usecase:

    • Create a Command API use case named add task. This use case will handle the addition of tasks to the system.

    Understanding Usecases
    Naming Your Usecases

  2. Define Request Properties:

    • Add the following properties to the request:
      • title (string): The title of the task.
      • description (string): A brief description of the task.
      • assignee id (UUID): The ID of the user assigned to the task.

    Usecase Schema
    Data Flows for Usecases

Generating the Code

  1. Set Usecase Status to Ready:

    • Change the status of your use case to Ready. This step is crucial as it prepares the use case for code generation.

    Usecase Status Documentation

  2. Generate the Source Code:

    • Set up your local code generator and generate the source code.

    Code Generation Instructions

Customizing the Code

  1. Implement Business Rules:

    • Add the following rules to your code:
      • Titles must be longer than 3 letters.
      • Descriptions must be longer than 7 letters and end with a period.
      • A valid task should have unique text for both title and description.
    tip

    Remember to enable Overwrite Protection for each code snippet you modify to avoid unintentional changes.

Testing the Code

  1. Install Dependencies:

    cd Task
    npm install
  2. Set Up the Local Test Database Container:

    You can use your own local database or use the docker image shown below:

    docker run -d -p 5432:5432 --name myPostgresDb -e POSTGRES_DB=postgresDB -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password postgres -c ssl=on -c ssl_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem -c ssl_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
  3. Configure Your Test Environment:

    Update your .env.testing file with the following settings:

    POSTGRES_URL="postgres://user:password@localhost/postgres"

    Or the credentials to your own local database.

  4. Run Migrations for the Test Database:

    npm run test-integration-setup
  5. Execute Integration Tests:

    npm run test-integration
  6. Run Unit Tests:

    npm run test-unit