logologo
Get Started
Guide
Development
Plugins
API
English
简体中文
Get Started
Guide
Development
Plugins
API
English
简体中文
logologo

Quick Start

Plugin Development Overview
Write Your First Plugin
Project Structure

Server Development

Overview
Plugin
Collections
Database
DataSourceManager
ResourceManager
ACL
Middleware
Cache
Event
Context
Migration
Logger
I18n
Command
CronJobManager
Test

Client Development

Overview
Plugin
Context
Router
ACL
DataSourceManager
Resource
Request
Styles & Themes
Logger
I18n
Test

Others

Plugin Upgrade Guide
Languages
Dependency Management
Build
Previous PageCache
Next PageContext

#Event

NocoBase's server (Server) triggers corresponding events (Event) during application lifecycle, plugin lifecycle, and database operations. Plugin developers 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 support for emitAsync, used to asynchronously trigger events and wait for all listeners to complete execution.

#Where to Register Event Listeners

Event listeners should generally be 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 plugin 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 can 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!'); // The original Chinese text had a copy-paste error, this is the corrected log message.
});