Hooks

Use lifecycle hooks to stay synced with NuxtHub.

onHubReady()

Use onHubReady() to ensure the execution of some code once NuxtHub environment bindings are set up and database migrations are applied.

onHubReady() is a shortcut using the hubHooks object under the hood that listens to the bindings:ready and database:migrations:done events.
server/plugins/migrations.ts
export default defineNitroPlugin(() => {
  // Only run in development
  if (import.meta.dev) {
    onHubReady(async () => {
      console.log('NuxtHub is ready! 🚀')
    })
  }
})

hubHooks

The hubHooks object is a collection of hooks that can be used to stay synced with NuxtHub.

Signature

Signature
export interface HubHooks {
  'bindings:ready': () => any | void
  'database:migrations:done': () => any | void
}

Usage

You can use the hubHooks object to listen to HubHooks events in your server plugins:

server/plugins/hub.ts
export default defineNitroPlugin(() => {
  hubHooks.hook('bindings:ready', () => {
    console.log('NuxtHub bindings are ready!')
    const db = hubDatabase()
  })
  // Only run in development and if the database is enabled
  if (import.meta.dev) {
    hubHooks.hook('database:migrations:done', () => {
      console.log('Database migrations are done!')
    })
  }
})
Note that hubHooks is a hookable instance.