Slide 1

Slide 1 text

September 10, 2019 | NestJS Paris | Sylvain Pontoreau NestJS & Event Store

Slide 2

Slide 2 text

Formerly Microsoft In ❤ with Co-organizer Who I am? @spontoreau

Slide 3

Slide 3 text

Paris TypeScript @ParisTypeScript www.typescript.paris

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Opposite of Data Driven Development Data Driven Development Domain Driven Design != Data structure Entity Relationship CRUD Behavior Bounded Context Event

Slide 6

Slide 6 text

Query Just a read operation Command Data validation Business logic Persistence CQRS

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

https://martinfowler.com/eaaDev/EventSourcing.html Event Sourcing

Slide 9

Slide 9 text

Event Sourcing & CQRS

Slide 10

Slide 10 text

CQRS & Event Sourcing GetBookById Query CreateBook Command GetAllBook Query DeleteBook Command … … Dispatcher Read DB Dispatcher QueryHandler CommandHandler Event Store Projection Projection AggregateRoot State Events

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Custom EventPublisher & MessageSource import { IEvent } from './event.interface'; export interface IEventPublisher { publish(event: T); } import { Subject } from 'rxjs'; import { IEvent } from './event.interface'; export interface IMessageSource { bridgeEventsTo< T extends Ievent >(subject: Subject); } • IEventPublisher • MessageSource export class UserModule implements OnModuleInit { constructor( private readonly eventBus: EventBus, private readonly eventStorePublisher: EventStorePublisher ) {} onModuleInit() { this.eventBus.publisher = this.eventStorePublisher; } } • Module

Slide 14

Slide 14 text

Which database for storing events? Choose what you want! Please try me!!!

Slide 15

Slide 15 text

Event Store, the stream database https://eventstore.org

Slide 16

Slide 16 text

Demo

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Questions?

Slide 19

Slide 19 text

Thank you!