Lifecycle hooks

Idempotent hooks run in deterministic order during module lifecycle transitions.

Implement install, upgrade, and uninstall on your ModuleDefinition. The engine may invoke before* and after* variants; all hooks must tolerate retries.

Best practices

  • Run database migrations from upgrade via ctx.migrations, not ad-hoc SQL in routes.
  • Enqueue long work to background jobs; keep the synchronous install path under a few seconds.
  • On uninstall, disable features first; data retention follows plan policy (soft delete, then hard delete).
  • Log significant actions through ctx.audit for compliance visibility.

Module context

Hooks receive ModuleContext: tenant id, config, prisma, notifications, search registration, email templates, and activity feed helpers. Use platform primitives instead of bespoke copies.