Implementing the CQRS Pattern in NestJS with a Note API Example
Tags: nestjs, cqrs, node, typescript
Published: 2023-08-20 06:30:14

![CQRS Pattern in NestJS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hp7vmvh07a4hl5lkxuod.png)

In the world of software architecture, designing systems that are scalable, maintainable, and efficient is a paramount concern. One architectural pattern that has gained traction for achieving these goals is the Command Query Responsibility Segregation (CQRS) pattern. CQRS is particularly useful for applications that require complex data manipulation and querying. In this article, we’ll explore how to implement the CQRS pattern in a NestJS application by creating a Note API example.

## Understanding the CQRS Pattern
CQRS is an architectural pattern that separates the responsibilities of handling read and write operations in a system. It recognizes that the requirements for reading data are often different from those for writing data. By segregating these responsibilities, CQRS allows for optimizing the read and write paths independently, leading to improved performance, scalability, and maintainability.

In a CQRS-based architecture, there are two main components: the Command side and the Query side.

- Command Side: This side is responsible for handling write operations such as creating, updating, and deleting data. It uses commands to trigger changes in the application’s state.
- Query Side: This side is responsible for handling read operations. It focuses on delivering data in a format that is optimized for querying and displaying, without concerns about the underlying data storage or manipulation logic.

## Implementing CQRS in NestJS
NestJS is a popular Node.js framework that provides a solid foundation for building scalable and maintainable server-side applications. To implement the CQRS pattern in NestJS, we’ll use the @nestjs/cqrs package, which provides tools and decorators for managing commands, events, and queries.

Let’s walk through the implementation of a Note API using CQRS in NestJS.

### Step 1: Setting Up the Project
First, create a new NestJS project if you haven’t already:

```bash
nest new note-api-cqrs
```

Navigate to the project directory:

```bash
cd note-api-cqrs
```

### Step 2: Installing Dependencies
Install the required dependencies for CQRS and other functionalities:

```bash
npm install --save @nestjs/cqrs
```

### Step 3: Creating Commands, Events, and Queries
In CQRS, commands represent actions that modify the application’s state, events represent things that have happened in the past, and queries represent requests for information. Let’s create these components for our Note API.

Create a notes module:

```bash
nest generate module notes
```

### Step 4: Creating Command
Inside the notes module, create a create-note.command.ts file and implement the create note command.

```ts
export class CreateNoteCommand {
  constructor(public readonly title: string, public readonly content: string) {}
}
```

### Step 5: Creating Query
Inside the notes module, create a get-notes.query.ts file and implement the get notes query.

```ts
export class GetNotesQuery {}
```

### Step 6: Implementing Command Handlers
Create a create-note.handler.ts file and implement the command handler:

```ts
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { CreateNoteCommand } from './create-note.command';
import { NoteCreatedEvent } from '../events/note-created.event';

@CommandHandler(CreateNoteCommand)
export class CreateNoteHandler implements ICommandHandler<CreateNoteCommand> {
  async execute(command: CreateNoteCommand): Promise<void> {
    // Here, you can perform the necessary logic to create a new note
    const { title, content } = command;
    
    // Simulate note creation
    const noteId = Math.random().toString(36).substring(7);
    
    // Emit a NoteCreatedEvent
    const event = new NoteCreatedEvent(noteId, title, content);
    event.commit();
  }
}
```

### Step 7: Implementing Query Handlers
Create aget-notes.handler.ts file and implement the query handler:

```ts
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { GetNotesQuery } from './get-notes.query';

@QueryHandler(GetNotesQuery)
export class GetNotesHandler implements IQueryHandler<GetNotesQuery> {
  async execute(query: GetNotesQuery): Promise<any> {
    // Here, you can retrieve notes data for querying
    // For demonstration purposes, let's return a mock list of notes
    return [
      { id: 1, title: 'Note 1', content: 'Content of note 1' },
      { id: 2, title: 'Note 2', content: 'Content of note 2' },
    ];
  }
}
```

### Step 8: Wiring Everything Together
In the notes.module.ts file, configure the CQRS module to use the command handlers and query handlers:

```ts
import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { NotesController } from './notes.controller';
import { CreateNoteHandler } from './commands/create-note.command';
import { NoteCreatedHandler } from './events/note-created.event';
import { GetNotesHandler } from './queries/get-notes.query';

@Module({
  imports: [CqrsModule],
  controllers: [NotesController],
  providers: [CreateNoteHandler, GetNotesHandler],
})
export class NotesModule {}
```

### Step 9: Creating the Notes Controller
In the notes.controller.ts file, create a controller to handle the API requests:

```ts
import { Controller, Get, Post, Body } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { CreateNoteCommand } from './commands/create-note.command';
import { GetNotesQuery } from './queries/get-notes.query';

@Controller('notes')
export class NotesController {
  constructor(
    private readonly commandBus: CommandBus,
    private readonly queryBus: QueryBus,
  ) {}

  @Post()
  async createNote(@Body() body: { title: string, content: string }): Promise<void> {
    const { title, content } = body;
    await this.commandBus.execute(new CreateNoteCommand(title, content));
  }

  @Get()
  async getNotes(): Promise<any[]> {
    return this.queryBus.execute(new GetNotesQuery());
  }
}
```

### Step 10: Running the Application
Finally, start the NestJS application:

```bash
npm run start:dev
```

Your Note API with the CQRS pattern is now up and running! You can use tools like curl, Postman, or other API testing tools to interact with the API.

## Conclusion
The CQRS pattern is a powerful way to achieve separation of concerns in applications by segregating read and write operations. With the help of the @nestjs/cqrs package, implementing the CQRS pattern in a NestJS application becomes straightforward. By using commands and queries, you can efficiently handle complex data manipulation and querying scenarios while maintaining scalability and maintainability.

Here’s a link to a sample project repository for a more concrete demonstration of the concepts discussed in this article: [Github Repo](https://github.com/vishnucprasad/nestjs_note_api_cqrs)