How to use @nestjs/cqrs - 10 common examples

To help you get started, we’ve selected a few @nestjs/cqrs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github new-eden-social / new-eden-social / src / api / src / modules / notification / commands / handlers / seen.handler.ts View on Github external
import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { NotificationRepository } from '../../notification.repository';
import { SeenNotificationCommand } from '../seen.command';

@CommandHandler(SeenNotificationCommand)
export class SeenNotificationCommandHandler implements ICommandHandler {
  constructor(
    @InjectRepository(NotificationRepository)
    private readonly repository: NotificationRepository,
    private readonly publisher: EventPublisher,
  ) {
  }

  async execute(command: SeenNotificationCommand) {
    const { notification } = command;

    const entity = this.publisher.mergeObjectContext(
      await this.repository.markAsSeen(notification),
    );

    // Sends actual event
github qas / examples-nodejs-cqrs-es-swagger / src / users / events / handlers / user-deleted.handler.ts View on Github external
import { IEventHandler, EventsHandler } from '@nestjs/cqrs';
import { UserDeletedEvent } from '../impl/user-deleted.event';
import { Logger } from '@nestjs/common';

@EventsHandler(UserDeletedEvent)
export class UserDeletedHandler
  implements IEventHandler {
  handle(event: UserDeletedEvent) {
    Logger.log(event, 'UserDeletedEvent'); // write here
  }
}
github juicycleff / ultimate-backend / apps / service-tenant / src / cqrs / query / handlers / tenant / get-tenants.handler.ts View on Github external
import {Logger} from '@nestjs/common';
import {IQueryHandler, QueryHandler} from '@nestjs/cqrs';
import {ApolloError} from 'apollo-server-express';
import { TenantRepository } from '@graphqlcqrs/repository/repositories';
import { TenantEntity } from '@graphqlcqrs/repository/entities';
import { GetTenantsQuery } from '../../impl';
import { ObjectId } from 'bson';

@QueryHandler(GetTenantsQuery)
export class GetTenantsHandler implements IQueryHandler {
  constructor(
    private readonly tenantRepository: TenantRepository,
  ) {}

  async execute(query: GetTenantsQuery): Promise {
    Logger.log(query, 'GetTenantsQuery'); // write here
    const { where, user } = query;

    if (!user) { throw Error('Missing get current user'); }

    try {
      return await this.tenantRepository.find({ conditions: {...where, ownerId: new ObjectId(user.id)} });
    } catch (e) {
      throw new ApolloError(e);
    }
github juicycleff / ultimate-backend / apps / service-tenant / src / cqrs / query / handlers / tenant / get-tenant.handler.ts View on Github external
import {Logger} from '@nestjs/common';
import {IQueryHandler, QueryHandler} from '@nestjs/cqrs';
import { TenantRepository } from '@graphqlcqrs/repository/repositories';
import { TenantEntity } from '@graphqlcqrs/repository/entities';
import { GetTenantQuery } from '../../impl';
import { ObjectId } from 'bson';

@QueryHandler(GetTenantQuery)
export class GetTenantHandler implements IQueryHandler {
  constructor(
    private readonly tenantRepository: TenantRepository,
  ) {}

  async execute(query: GetTenantQuery): Promise {
    Logger.log(query, 'GetTenantQuery'); // write here
    const { where, user } = query;

    if (!where) { throw Error('Missing get inputs'); }
    return await this.tenantRepository.findOne({...where, ownerId: new ObjectId(user.id)});
  }
}
github juicycleff / ultimate-backend / libs / core / src / cqrs / queries / handlers / auth / get-auth.handler.ts View on Github external
import {Logger} from '@nestjs/common';
import {IQueryHandler, QueryHandler} from '@nestjs/cqrs';
import { AuthRepository, AuthEntity } from '@graphqlcqrs/repository';
import { GetAuthQuery } from '../../impl';

@QueryHandler(GetAuthQuery)
export class GetAuthHandler implements IQueryHandler {
  constructor(
    private readonly authRepository: AuthRepository,
  ) {}

  async execute(query: GetAuthQuery): Promise {
    Logger.log(query, 'GetAuthQuery');
    const { where } = query;
    if (!where) { throw Error('Missing get inputs'); }
    // @ts-ignore
    return await this.authRepository.findOne({ 'local.email': where.local.email});
  }
}
github juicycleff / ultimate-backend / apps / service-tenant / src / cqrs / query / handlers / tenant-member / get-tenant-members.handler.ts View on Github external
import {Logger} from '@nestjs/common';
import {IQueryHandler, QueryHandler} from '@nestjs/cqrs';
import { TenantMemberEmbed } from '@graphqlcqrs/repository/entities';
import { TenantRepository } from '@graphqlcqrs/repository';
import { GetTenantMembersQuery } from '../../impl';

@QueryHandler(GetTenantMembersQuery)
export class GetTenantMembersHandler implements IQueryHandler {
  constructor(
    private readonly tenantRepository: TenantRepository,
  ) {}

  async execute(query: GetTenantMembersQuery): Promise {
    Logger.log(query, 'GetUserQuery'); // write here
    const { where, tenantId } = query;

    if (!where) { throw Error('Missing get inputs'); }
    const tenant = await this.tenantRepository.aggregate([
      {
        $match: {
          $and: [
            {_id: tenantId},
            {
github juicycleff / ultimate-backend / libs / core / src / cqrs / queries / handlers / auth / auth-exist.handler.ts View on Github external
import {Logger} from '@nestjs/common';
import {IQueryHandler, QueryHandler} from '@nestjs/cqrs';
import { AuthRepository } from '@graphqlcqrs/repository';
import { AuthExistQuery } from '../../impl';

@QueryHandler(AuthExistQuery)
export class AuthExistHandler implements IQueryHandler {
  constructor(
    private readonly authRepository: AuthRepository,
  ) {}

  async execute(query: AuthExistQuery): Promise {
    Logger.log(query, 'AuthExistQuery');
    const { where } = query;
    if (!where) { throw Error('Missing get inputs'); }
    // @ts-ignore
    return await this.authRepository.exist({ 'local.email': where.local.email});
  }
}
github juicycleff / ultimate-backend / apps / service-payment / src / cqrs / query / handlers / plan / get-plan.handler.ts View on Github external
import { Logger, CACHE_MANAGER, Inject, CacheStore } from '@nestjs/common';
import {IQueryHandler, QueryHandler} from '@nestjs/cqrs';
import { InjectStripe } from 'nestjs-stripe';
import * as Stripe from 'stripe';
import { UserInputError, ApolloError } from 'apollo-server-express';
import { Plan } from '../../../../types';
import { convertToPlan } from '../../../../converter.util';
import { GetPlanQuery } from '../../impl';

@QueryHandler(GetPlanQuery)
export class GetPlanHandler implements IQueryHandler {
  logger = new Logger(this.constructor.name);

  public constructor(
    @InjectStripe() private readonly stripeClient: Stripe,
    @Inject(CACHE_MANAGER) private readonly cacheStore: CacheStore,
  ) {}

  async execute(query: GetPlanQuery): Promise {
    this.logger.log(`Async ${query.constructor.name}...`);
    const { id } = query;

    if (!id) { throw new UserInputError('Missing plan id input'); }

    try {
      // Check cache to see if data exist
github kamilmysliwiec / nest-cqrs-example / src / heroes / commands / handlers / drop-ancient-item.handler.ts View on Github external
import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs';
import * as clc from 'cli-color';
import { HeroRepository } from '../../repository/hero.repository';
import { DropAncientItemCommand } from '../impl/drop-ancient-item.command';

@CommandHandler(DropAncientItemCommand)
export class DropAncientItemHandler
  implements ICommandHandler {
  constructor(
    private readonly repository: HeroRepository,
    private readonly publisher: EventPublisher,
  ) {}

  async execute(command: DropAncientItemCommand) {
    console.log(clc.yellowBright('Async DropAncientItemCommand...'));

    const { heroId, itemId } = command;
    const hero = this.publisher.mergeObjectContext(
      await this.repository.findOneById(+heroId),
    );
    hero.addItem(itemId);
    hero.commit();
github notadd / next / packages / injection / events / handlers / addon-after-enable.event.handler.js View on Github external
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require("../");
const cqrs_1 = require("@nestjs/cqrs");
let AddonAfterEnableEventHandler = class AddonAfterEnableEventHandler {
    handle(event) {
        console.log("Addon After Enable Event applied: " + event.identification);
        return undefined;
    }
};
AddonAfterEnableEventHandler = __decorate([
    cqrs_1.EventsHandler(_1.AddonAfterEnableEvent)
], AddonAfterEnableEventHandler);
exports.AddonAfterEnableEventHandler = AddonAfterEnableEventHandler;

//# sourceMappingURL=addon-after-enable.event.handler.js.map

@nestjs/cqrs

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

MIT
Latest version published 2 months ago

Package Health Score

86 / 100
Full package analysis

Similar packages