eventre
v0.1.1
Published
Event Sourcing Framework in MongoDB
Downloads
8
Readme
eventre
Event Sourcing Framework in MongoDB
Usage
import { eventre } from "eventre";
import { MongoClient } from "mongodb";
const mongoClient = new MongoClient();
const db = mongoClient.db("my_database");
/**
* 1. Define your domain event
*/
type MyDomainEvent =
| BaseDomainEvent<
"init",
{
// ...
}
>
| BaseDomainEvent<
"update",
{
// ...
}
>;
/**
* 2. Define your state
*/
type MyState = {
// ...
};
/**
* 3. Implement your own business logic clearly in reducer
*/
const reducer: BaseReducer<MyDomainEvent, MyState> = (prevState, event) => {
// ...
};
/**
* 4. Then, `eventre` automatically make common repository interface
*/
const repository = eventre({
db,
aggregateName: "MyModel",
reducer,
});
// Scan all aggregates
repository.all();
// Find one aggregate with `aggregateId`
repository.findOne({
aggregateId: "...",
});
// Find many aggregates with `aggregateIds`
repository.batchGet({
aggregateIds: ["...", "..."],
});
// Create aggregate with initial DomainEvent
repository.create({
initialEventName: "init",
initialEventData: {
// ...
},
});
// Dispatch DomainEvent to aggregate
repository.dispatch({
aggregate,
eventName: "edit",
eventData: {
// ...
},
});