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
-
Navigate to the Bounded Context:
- Enter the Bounded Context named task management.
-
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.
Creating Your First Usecase
-
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.
-
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.
- Add the following properties to the request:
Generating the Code
-
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.
-
Generate the Source Code:
- Set up your local code generator and generate the source code.
Customizing the Code
-
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.
tipRemember to enable Overwrite Protection for each code snippet you modify to avoid unintentional changes.
- Add the following rules to your code:
Testing the Code
-
Install Dependencies:
cd Task
npm install -
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
-
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.
-
Run Migrations for the Test Database:
npm run test-integration-setup
-
Execute Integration Tests:
npm run test-integration
-
Run Unit Tests:
npm run test-unit