Notifying subscribers and dispatching Domain Events from Sequelize Hooks
With Sequelize, recall that we can make use of the lovely Hollywood Principle ("don't call us, we'll call you"), by defining callbacks on the lifecycle hooks that get called when we perform operations against the database.
// shared/infra/database/sequelize/hooks/index.ts
import models from "../models"
import { UniqueEntityID } from "../../../../domain/UniqueEntityID"
import { DomainEvents } from "../../../../domain/events/DomainEvents"
const dispatchEventsCallback = (model: any, primaryKeyField: string) => {
// Get the aggregate id from the Sequelize model just saved/updated.
const aggregateId = new UniqueEntityID(model[primaryKeyField])
// Dispatch any domain events on that aggregate from a previous transaction.
DomainEvents.dispatchEventsForAggregate(aggregateId)
}
(async function createHooksForAggregateRoots() {
const { BaseUser, Member, Post } = models
// Notify subscribers when the User aggregate transactions complete
BaseUser.addHook("afterCreate", (m: any) =>
dispatchEventsCallback(m, "base_user_id")
)
BaseUser.addHook("afterDestroy", (m: any) =>
dispatchEventsCallback(m, "base_user_id")
)
BaseUser.addHook("afterUpdate", (m: any) =>
dispatchEventsCallback(m, "base_user_id")
)
BaseUser.addHook("afterSave", (m: any) =>
dispatchEventsCallback(m, "base_user_id")
)
BaseUser.addHook("afterUpsert", (m: any) =>
dispatchEventsCallback(m, "base_user_id")
)
// Notify subscribers when the Member aggregate transactions complete
Member.addHook("afterCreate", (m: any) =>
dispatchEventsCallback(m, "member_id")
)
Member.addHook("afterDestroy", (m: any) =>
dispatchEventsCallback(m, "member_id")
)
Member.addHook("afterUpdate", (m: any) =>
dispatchEventsCallback(m, "member_id")
)
Member.addHook("afterSave", (m: any) =>
dispatchEventsCallback(m, "member_id")
)
Member.addHook("afterUpsert", (m: any) =>
dispatchEventsCallback(m, "member_id")
)
// Notify subscribers when the Post aggregate transactions complete
Post.addHook("afterCreate", (m: any) => dispatchEventsCallback(m, "post_id"))
Post.addHook("afterDestroy", (m: any) => dispatchEventsCallback(m, "post_id"))
Post.addHook("afterUpdate", (m: any) => dispatchEventsCallback(m, "post_id"))
Post.addHook("afterSave", (m: any) => dispatchEventsCallback(m, "post_id"))
Post.addHook("afterUpsert", (m: any) => dispatchEventsCallback(m, "post_id"))
console.log("[Hooks]: Sequelize hooks setup.")
})()
We use the hooks to tell our Domain Events subject that we should notify all subscribers to the particular Domain Event (in this case, the UserCreated…
Howdy 👋
This is an online wiki about the main topics over the last 40 years of software design, architecture & testing. It was created by Khalil Stemmler.
This wiki's mission is to help eliminate the unknown unknowns, helping you get up to speed on the best practices and principles to write testable, flexible, and maintainable code.
I created this wiki while I was connecting the dots on my own path. You can read more about that learning journey in the introduction
Already bought it?
If you’ve already purchased the book, click here to re-send your link. You can read the online wiki or download a copy of the book in PDF, EPUB, and Kindle versions.
Want access?
You can read the intro to the book for free and visit solidbook.io to buy the book/wiki! To get an idea of my writing, read some of my best free content here and here.
Need help?
Something not working? Have a question? You can reach me on at khalil@khalilstemmler.com.