Production-grade Event Sourcing library
$ dotnet add package Eventuous.Projections.MongoDBThis package adds MongoDB projections to applications built with Eventuous.
There are two main components provided by this package:
MongoCheckpointStore, implements ICheckpointStoreMongoProjection, implements IEventHandlerYou can register MongoCheckpointStore so it will be used by all subscriptions that need to store checkpoints (normally, EventStoreDB catch-up subscriptions).
services.AddSingleton<ICheckpointStore, MongoCheckpointStore>();
It will add a checkpoint collection to your Mongo database. Each subscription will have its own checkpoint document. The document id would be the subscription id.
Make sure to use unique subscription ids across different applications if they use the same Mongo database.
Create your own projection class that inherits MongoProjection<T> abstract class.
Here, T is the document type, which must be a record. Your document type should inherit from ProjectedDocument record.
Use the On<TEvent> function to register event projection handlers:
public class ProjectWithTasksProjection : MongoProjection<ProjectWithTasks> {
public ProjectWithTasksProjection(
IMongoDatabase database,
ILoggerFactory? loggerFactory
) : base(database, QuerySubscription.Id, loggerFactory) {
On<V1.ProjectRegistered>(
ctx => ctx.Message.ProjectId,
(ctx, update) => update.SetOnInsert(x => x.ProjectName, ctx.Message.Name)
);
On<V1.TaskCreated>(
ctx => ctx.Message.ProjectId,
(ctx, update) => update.AddToSet(
x => x.Tasks,
new ProjectTaskRecord(ctx.Message.TaskId, ctx.Message.Description)
)
);
}
}
Read more in Eventuous documentation.