I have been trying to figure out the preferred way of doing "Event Sourcing" while using the NestJS CQRS recipe (https://docs.nestjs.com/recipes/cqrs).
I've been looking at the NestJS framework during the last couple of weeks and love every aspect of it. Except for the docs, which are pretty thin in some areas.
Either NestJS doesn't really have an opinion on how to implement "Event Sourcing", or I'm missing something obvious.
My main question is: What's the easiest way to persist the events themselves?
Right now, my events look pretty basic:
import { IEvent } from '@nestjs/cqrs';
export class BookingChangedTitleEvent implements IEvent {
constructor(
public readonly bookingId: string,
public readonly title: string) {}
}
My initial idea was to use TypeORM (https://docs.nestjs.com/recipes/sql-typeorm) and make each of my events not only implement IEvent
, but also make it inherit a TypeORM @Entity()
.
But that would have one table (SQL) or collection (NoSQL) for each of the events, making it impossible to read all events that happened to a single aggregate. Am I missing something?
Another approach would be to dump each event to JSON, which sounds pretty easy. But how would I load the object IEvent
classes from the db then? (sounds like I'm implementing my own ORM then)