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 and book about the basics of software design and architecture with TypeScript by Khalil Stemmler, Developer Advocate @ Apollo GraphQL .

This book’s mission is to teach developers the essential skills and practices to write testable, flexible, and maintainable code.

You can read more about the learning journey in the "Software Design and Architecture Roadmap 🖼️".

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 (it's currently on pre-sale for 33% off)! For recent updates, click here. 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 Twitter or khalil@khalilstemmler.com.