Event

NocoBase's server triggers corresponding events during application lifecycle, plugin lifecycle, and database operations. You can listen to these events to implement extension logic, automated operations, or custom behaviors.

NocoBase's event system is mainly divided into two levels:

  • app.on() -- Application Level Events: Listen to application lifecycle events, such as startup, installation, enabling plugins, etc.
  • db.on() -- Database Level Events: Listen to data model level operation events, such as creating, updating, deleting records, etc.

Both inherit from Node.js's EventEmitter, supporting standard .on(), .off(), .emit() interfaces. NocoBase also extends emitAsync, used to asynchronously trigger events and wait for all listeners to complete execution.

Where to Register Event Listeners

Event listeners are typically registered in the plugin's beforeLoad() method, ensuring events are ready during the plugin loading phase, and subsequent logic can respond correctly.

import { Plugin } from '@nocobase/server';

export default class PluginHelloServer extends Plugin {
  async beforeLoad() {

    // Listen to application events
    this.app.on('afterStart', () => {
      app.logger.info('NocoBase has started');
    });

    // Listen to database events
    this.db.on('afterCreate', (model) => {
      if (model.collectionName === 'posts') {
        app.logger.info(`New post: ${model.get('title')}`);
      }
    });
  }
}

Listen to Application Events app.on()

Application events are used to capture NocoBase application and plugin lifecycle changes, suitable for initialization logic, resource registration, or dependency detection.

Common Event Types

Event NameTrigger TimingTypical Uses
beforeLoad / afterLoadBefore / after application loadRegister resources, initialize configuration
beforeStart / afterStartBefore / after service startupStart tasks, print startup logs
beforeInstall / afterInstallBefore / after application installationInitialize data, import templates
beforeStop / afterStopBefore / after service stopClean up resources, save state
beforeDestroy / afterDestroyBefore / after application destructionDelete cache, disconnect connections
beforeLoadPlugin / afterLoadPluginBefore / after plugin loadModify plugin configuration or extend functionality
beforeEnablePlugin / afterEnablePluginBefore / after plugin enableCheck dependencies, initialize plugin logic
beforeDisablePlugin / afterDisablePluginBefore / after plugin disableClean up plugin resources
afterUpgradeAfter application upgrade completesExecute data migration or compatibility fixes

Example: Listen to application startup event

app.on('afterStart', async () => {
  app.logger.info('NocoBase service has started');
});

Example: Listen to plugin load event

app.on('afterLoadPlugin', ({ plugin }) => {
  app.logger.info(`Plugin ${plugin.name} has been loaded`);
});

Listen to Database Events db.on()

Database events capture various data changes at the model level, suitable for auditing, synchronization, auto-filling, and other operations.

Common Event Types

Event NameTrigger Timing
beforeSync / afterSyncBefore / after synchronizing database structure
beforeValidate / afterValidateBefore / after data validation
beforeCreate / afterCreateBefore / after creating records
beforeUpdate / afterUpdateBefore / after updating records
beforeSave / afterSaveBefore / after save (includes create and update)
beforeDestroy / afterDestroyBefore / after deleting records
afterCreateWithAssociations / afterUpdateWithAssociations / afterSaveWithAssociationsAfter operations including association data
beforeDefineCollection / afterDefineCollectionBefore / after defining collections
beforeRemoveCollection / afterRemoveCollectionBefore / after removing collections

Example: Listen for the event after data creation

db.on('afterCreate', async (model, options) => {
  db.logger.info('Data has been created!');
});

Example: Listen for the event before data update

db.on('beforeUpdate', async (model, options) => {
  db.logger.info('Data is about to be updated');
});
  • Plugin — Register event listeners in plugin lifecycle methods
  • Database — Database-level event sources and data operation APIs
  • Collections — Collection definitions and model relationships in database events
  • Middleware — Collaboration between middleware and events in request processing
  • Server Development Overview — The role of the event system in server architecture