JSPM

  • Created
  • Published
  • Downloads 171
  • Score
    100M100P100Q111676F
  • License MIT

CQRS and event base core

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@mbc-cqrs-serverless/core) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    MBC CQRS serverless framework

    MBC CQRS serverless framework Core package

    Description

    The Core package provides the foundational functionality for the MBC CQRS Serverless framework. It implements:

    • Command Query Responsibility Segregation (CQRS) patterns
    • Event-driven architecture support
    • Data persistence and retrieval
    • Authentication and authorization
    • Multi-tenant data isolation
    • AWS service integrations

    Installation

    npm install @mbc-cqrs-serverless/core

    Usage

    Basic Setup

    1. Import and configure the core module:
    import { CoreModule } from '@mbc-cqrs-serverless/core';
    import { Module } from '@nestjs/common';
    
    @Module({
      imports: [
        CoreModule.forRoot({
          region: 'ap-northeast-1',
          stage: 'dev',
        }),
      ],
    })
    export class AppModule {}

    Command Handling

    1. Create a command:
    import { CommandInputModel } from '@mbc-cqrs-serverless/core';
    
    export class CreateUserCommand implements CommandInputModel {
      readonly pk: string;
      readonly sk: string;
      readonly id: string;
      
      constructor(public readonly userId: string, public readonly userData: any) {
        this.pk = `USER#${userId}`;
        this.sk = `METADATA#${userId}`;
        this.id = userId;
      }
    }
    1. Implement a command handler:
    import { CommandHandler, ICommandHandler } from '@mbc-cqrs-serverless/core';
    
    @CommandHandler(CreateUserCommand)
    export class CreateUserHandler implements ICommandHandler<CreateUserCommand> {
      async execute(command: CreateUserCommand): Promise<void> {
        // Implementation
      }
    }

    Event Handling

    1. Create an event handler:
    import { EventsHandler, IEventHandler } from '@mbc-cqrs-serverless/core';
    import { UserCreatedEvent } from './user-created.event';
    
    @EventsHandler(UserCreatedEvent)
    export class UserCreatedHandler implements IEventHandler<UserCreatedEvent> {
      async handle(event: UserCreatedEvent): Promise<void> {
        // Implementation
      }
    }

    Data Access

    1. Use the DataService for persistence:
    import { DataService, InjectDataService } from '@mbc-cqrs-serverless/core';
    
    @Injectable()
    export class UserService {
      constructor(
        @InjectDataService() private readonly dataService: DataService
      ) {}
    
      async getUser(userId: string): Promise<User> {
        return this.dataService.findOne({
          pk: `USER#${userId}`,
          sk: `METADATA#${userId}`,
        });
      }
    }

    Authentication & Authorization

    1. Implement role-based access:
    import { RolesGuard, Roles } from '@mbc-cqrs-serverless/core';
    
    @Controller('users')
    @UseGuards(RolesGuard)
    export class UserController {
      @Post()
      @Roles('admin')
      async createUser(@Body() userData: CreateUserDto): Promise<void> {
        // Implementation
      }
    }

    Multi-tenancy

    1. Configure tenant isolation:
    import { TenantContext, UseTenant } from '@mbc-cqrs-serverless/core';
    
    @Injectable()
    export class UserService {
      @UseTenant()
      async getUsersForTenant(
        @TenantContext() tenantId: string
      ): Promise<User[]> {
        // Implementation with automatic tenant isolation
      }
    }

    AWS Integration

    1. Use AWS services:
    import { 
      StepFunctionService, 
      NotificationService 
    } from '@mbc-cqrs-serverless/core';
    
    @Injectable()
    export class WorkflowService {
      constructor(
        private readonly stepFunctions: StepFunctionService,
        private readonly notifications: NotificationService
      ) {}
    
      async startWorkflow(data: any): Promise<void> {
        await this.stepFunctions.startExecution({
          stateMachineArn: 'your-state-machine-arn',
          input: JSON.stringify(data),
        });
      }
    }

    Error Handling

    The core package provides standardized error handling:

    import { 
      CommandError,
      ValidationError,
      NotFoundError 
    } from '@mbc-cqrs-serverless/core';
    
    @CommandHandler(UpdateUserCommand)
    export class UpdateUserHandler implements ICommandHandler<UpdateUserCommand> {
      async execute(command: UpdateUserCommand): Promise<void> {
        if (!command.userId) {
          throw new ValidationError('User ID is required');
        }
        
        const user = await this.userService.findById(command.userId);
        if (!user) {
          throw new NotFoundError(`User ${command.userId} not found`);
        }
        
        // Implementation
      }
    }

    Documentation

    Visit https://mbc-cqrs-serverless.mbc-net.com/ to view the full documentation, including:

    • Architecture overview
    • Core concepts and patterns
    • AWS integration details
    • Security features
    • API reference

    License

    Copyright © 2024, Murakami Business Consulting, Inc. https://www.mbc-net.com/
    This project and sub projects are under the MIT License.