Socket
Socket
Sign inDemoInstall

@nestjs/cqrs

Package Overview
Dependencies
1
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/cqrs


Version published
Maintainers
1
Install size
198 kB
Created

Package description

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

Readme

Source

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Travis Linux Coverage Discord Backers on Open Collective Sponsors on Open Collective

Description

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

Installation

$ npm install --save @nestjs/cqrs

Quick Start

Overview & CQRS Tutorial

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.

FAQs

Last updated on 14 Feb 2022

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc