Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nestjs/cqrs

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/cqrs

A lightweight CQRS module for Nest framework (node.js)

  • 9.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
171K
decreased by-2.74%
Maintainers
1
Weekly downloads
 
Created

What is @nestjs/cqrs?

@nestjs/cqrs is a package for the NestJS framework that provides tools to implement the Command Query Responsibility Segregation (CQRS) pattern. This pattern separates read and write operations to simplify complex business logic and improve performance. The package also supports event sourcing, which allows you to store the state of an application as a sequence of events.

What are @nestjs/cqrs's main functionalities?

Commands

Commands are used to encapsulate a request to perform an action. The `CreateUserCommand` class represents a command to create a user, and the `CreateUserHandler` class handles the execution of this command.

```typescript
import { CommandHandler, ICommand, ICommandHandler } from '@nestjs/cqrs';

export class CreateUserCommand implements ICommand {
  constructor(public readonly name: string) {}
}

@CommandHandler(CreateUserCommand)
export class CreateUserHandler implements ICommandHandler<CreateUserCommand> {
  async execute(command: CreateUserCommand) {
    console.log(`Creating user with name: ${command.name}`);
    // Add your business logic here
  }
}
```

Queries

Queries are used to encapsulate a request to retrieve data. The `GetUserQuery` class represents a query to get a user by ID, and the `GetUserHandler` class handles the execution of this query.

```typescript
import { IQuery, IQueryHandler, QueryHandler } from '@nestjs/cqrs';

export class GetUserQuery implements IQuery {
  constructor(public readonly id: string) {}
}

@QueryHandler(GetUserQuery)
export class GetUserHandler implements IQueryHandler<GetUserQuery> {
  async execute(query: GetUserQuery) {
    console.log(`Fetching user with id: ${query.id}`);
    // Add your business logic here
  }
}
```

Events

Events are used to notify the system that something has happened. The `UserCreatedEvent` class represents an event that a user has been created, and the `UserCreatedHandler` class handles this event.

```typescript
import { EventsHandler, IEvent, IEventHandler } from '@nestjs/cqrs';

export class UserCreatedEvent implements IEvent {
  constructor(public readonly userId: string) {}
}

@EventsHandler(UserCreatedEvent)
export class UserCreatedHandler implements IEventHandler<UserCreatedEvent> {
  handle(event: UserCreatedEvent) {
    console.log(`User created with id: ${event.userId}`);
    // Add your business logic here
  }
}
```

Sagas

Sagas are used to manage long-running transactions and orchestrate multiple events and commands. The `UserSagas` class listens for `UserCreatedEvent` and triggers a `CreateUserCommand` in response.

```typescript
import { Injectable } from '@nestjs/common';
import { Saga } from '@nestjs/cqrs';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { UserCreatedEvent } from './events/user-created.event';
import { CreateUserCommand } from './commands/create-user.command';

@Injectable()
export class UserSagas {
  @Saga()
  userCreated = (events$: Observable<any>): Observable<ICommand> => {
    return events$.ofType(UserCreatedEvent).pipe(
      map(event => new CreateUserCommand(event.userId))
    );
  };
}
```

Other packages similar to @nestjs/cqrs

FAQs

Package last updated on 09 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc