#2 NestJS Paris meetup - NestJS & Event Store

#2 NestJS Paris meetup - NestJS & Event Store

NestJS & Event Store (CQRS & Event Sourcing)

Presenation made during NestJS Paris meetup.

9298dcce5b2b0e88d8ffbb837192ca98?s=128

Sylvain Pontoreau

September 10, 2019
Tweet

Transcript

  1. September 10, 2019 | NestJS Paris | Sylvain Pontoreau NestJS

    & Event Store
  2. Formerly Microsoft In ❤ with Co-organizer Who I am? @spontoreau

  3. Paris TypeScript @ParisTypeScript www.typescript.paris

  4. Domain Driven Design • DDD is not a Framework nor

    a Methodology… • … it's an approach to dealing with complexity • DDD focusing: • The domain representation • The domain logic • Technical aspects are secondary • Just a part that enforcing the domain
  5. Opposite of Data Driven Development Data Driven Development Domain Driven

    Design != Data structure Entity Relationship CRUD Behavior Bounded Context Event
  6. Query Just a read operation Command Data validation Business logic

    Persistence CQRS
  7. CQRS GetBookById Query CreateBook Command GetAllBook Query DeleteBook Command …

    … Dispatcher Read DB Dispatcher QueryHandler CommandHandler Write DB Validation Plugin Monitoring Plugin Telemetry Plugin … Eventual consistency
  8. https://martinfowler.com/eaaDev/EventSourcing.html Event Sourcing

  9. Event Sourcing & CQRS

  10. CQRS & Event Sourcing GetBookById Query CreateBook Command GetAllBook Query

    DeleteBook Command … … Dispatcher Read DB Dispatcher QueryHandler CommandHandler Event Store Projection Projection AggregateRoot State Events
  11. Our focus in this talk GetBookById Query CreateBook Command GetAllBook

    Query DeleteBook Command … … Dispatcher Read DB Dispatcher QueryHandler CommandHandler Event Store Projection Projection AggregateRoot State Events
  12. NestJS, Commands, Aggregates & Events Command Bus EventBus EventPublisher Repository

    CommandHandler Controller/Service Aggregate Execute Comand Execute Handler Get Aggregate Merge Aggregate Apply events Execute behaviors IEventPublisher Publish events DefaultPubSub Default implementation EventHandler EventStore Publisher
  13. Custom EventPublisher & MessageSource import { IEvent } from './event.interface';

    export interface IEventPublisher { publish<T extends IEvent>(event: T); } import { Subject } from 'rxjs'; import { IEvent } from './event.interface'; export interface IMessageSource { bridgeEventsTo< T extends Ievent >(subject: Subject<T>); } • IEventPublisher • MessageSource export class UserModule implements OnModuleInit { constructor( private readonly eventBus: EventBus, private readonly eventStorePublisher: EventStorePublisher ) {} onModuleInit() { this.eventBus.publisher = this.eventStorePublisher; } } • Module
  14. Which database for storing events? Choose what you want! Please

    try me!!!
  15. Event Store, the stream database https://eventstore.org

  16. Demo

  17. Resources • CQRS & Event Sourcing • CQRS pattern: https://docs.microsoft.com/en-US/azure/architecture/patterns/cqrs

    • Event Sourcing pattern: https://docs.microsoft.com/en-us/azure/architecture/patterns/event-sourcing • Tackle business complexity in a Microservice with DDD and CQRS: https://docs.microsoft.com/en- us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/ • NestJS CQRS module: https://docs.nestjs.com/recipes/cqrs • Event Store website: https://eventstore.org • Talk’s sample: https://github.com/spontoreau/nestjs-eventstore-demo • node-eventstore-client NPM package: https://github.com/nicdex/node-eventstore-client • gereventstore-pomise NPM package : https://github.com/RemoteMetering/geteventstore-promise • Jérôme Rouaix presentation on CQRS & ES: https://gitpitch.com/jrouaix/cqrs-es-presentation/master#/ • Arnaud Lemaire talk on CQRS, ES and DDD: https://www.youtube.com/watch?v=qBLtZN3p3FU • Greg Young, A decade of DDD, CQRS and ES: https://www.youtube.com/watch?v=LDW0QWie21s
  18. Questions?

  19. Thank you!