Usage Chart
How strongly this package shapes the runtime
Package Flow
How this package moves through xtaskjs runtime phases
Before startup
Configure the package with @EventSource(...) or configureEventSource(), then decorate aggregates and subscribers so metadata exists before the container is scanned.
During CreateApplication()
The lifecycle manager resolves the configured event store and publisher, registers aggregate repositories and tokens in the container, and wires stored-event subscribers from DI-managed services.
During app.close()
Configured stores and publishers are destroyed automatically so in-memory, TypeORM-backed, and queue-backed event infrastructure shuts down with the application.
API Surface
Representative exports from the upstream package
Configuration and publishers
- EventSource
- configureEventSource
- createQueueEventPublisher
- QueueEventPublisher
- createTypeOrmEventStore
- TypeOrmEventStore
Aggregates and repositories
- EventSourcedAggregate
- ApplyEvent
- EventSourcedAggregateRoot
- EventSourceRepository
- InjectEventSourceRepository
Subscribers and lifecycle
- EventSourceSubscriber
- StoredEventSubscriber
- InjectEventStore
- InjectEventSourceBus
- InjectEventPublisher
- InjectEventSourceLifecycleManager
- EventSourceLifecycleManager
- initializeEventSourceIntegration
- shutdownEventSourceIntegration
- getEventSourceLifecycleManager
- InMemoryEventStore
- EventSourceBus
Usage
Typical adoption flow
1. Configure storage and publication
Start with the default in-memory store for tests and local workflows, or plug in createTypeOrmEventStore() and createQueueEventPublisher() when streams must be durable and publish outside the process.
2. Model aggregates around raised events
Decorate aggregate roots with EventSourcedAggregate(), raise domain events from command methods, and use ApplyEvent() handlers to mutate state during live execution and historical rehydration.
3. Save through repositories and react with subscribers
Inject EventSourceRepository into services or command handlers, save aggregates to append stored events, and attach EventSourceSubscriber() listeners for projections, integrations, or side effects.
Example
Reference snippet
import { Service } from "@xtaskjs/core";
import {
ApplyEvent,
EventSource,
EventSourcedAggregate,
EventSourcedAggregateRoot,
EventSourceRepository,
InjectEventSourceRepository,
createTypeOrmEventStore,
} from "@xtaskjs/event-source";
@EventSource({
store: createTypeOrmEventStore({ dataSourceName: "write-db", tableName: "event_store" }),
})
class EventSourceConfiguration {}
class UserRegisteredEvent {
constructor(public readonly id: string, public readonly email: string) {}
}
@EventSourcedAggregate({ stream: "users" })
class UserAggregate extends EventSourcedAggregateRoot {
public email?: string;
register(id: string, email: string) {
this.assignStreamId(id);
this.raiseEvent(new UserRegisteredEvent(id, email));
}
@ApplyEvent(UserRegisteredEvent)
onRegistered(event: UserRegisteredEvent) {
this.email = event.email;
}
}
@Service()
export class UserRegistrationService {
constructor(
@InjectEventSourceRepository(UserAggregate)
private readonly users: EventSourceRepository<UserAggregate>
) {}
async register(id: string, email: string) {
const user = this.users.create(id);
user.register(id, email);
await this.users.save(user);
}
}
Samples
Official samples to inspect next
Reference samples: 21-event_source_rabbitmq_app and 22-event_source_cqrs_app
Related
Packages commonly used with this one