docs/docs/en/plugin-development/server/plugin.md
In NocoBase, Server Plugin is the primary way to extend server-side functionality. You can extend the Plugin base class provided by @nocobase/server in your plugin directory's src/server/plugin.ts, then register events, APIs, permissions, and other custom logic at different lifecycle stages.
A basic plugin class structure is as follows:
import { Plugin } from '@nocobase/server';
export class PluginHelloServer extends Plugin {
async afterAdd() {}
async beforeLoad() {}
async load() {}
async install() {}
async afterEnable() {}
async afterDisable() {}
async remove() {}
async handleSyncMessage(message: Record<string, any>) {}
static async staticImport() {}
}
export default PluginHelloServer;
Plugin lifecycle methods execute in the following order. Each method has its specific execution timing and purpose:
| Lifecycle Method | Execution Timing | Description |
|---|---|---|
| staticImport() | Before plugin loads | Static class method, executed during initialization phase independent of application or plugin state, used for initialization work that doesn't depend on plugin instances. |
| afterAdd() | Executed immediately after plugin is added to PluginManager | Plugin instance has been created, but not all plugins have finished initializing. Can perform some basic initialization. |
| beforeLoad() | Executed before all plugins' load() | Can access all enabled plugin instances at this point. Suitable for registering database models, listening to database events, registering middleware, and other preparation work. |
| load() | Executed when plugin loads | All plugins' beforeLoad() complete before load() starts. Suitable for registering resources, API interfaces, and other core business logic -- for example, registering custom REST APIs via resourceManager. Note: The database has not finished synchronizing during the load() phase, so you cannot perform database queries or writes -- database operations should be placed in install() or request handlers. |
| install() | Executed when plugin is first activated | Only executed once when plugin is first enabled, typically used for initializing database table structures, inserting initial data, and other installation logic. install() only runs on first activation -- if subsequent versions need to change table structures or migrate data, use Migration scripts instead. |
| afterEnable() | Executed after plugin is enabled | Executed every time plugin is enabled, can be used to start scheduled tasks, establish connections, etc. |
| afterDisable() | Executed after plugin is disabled | Can be used to clean up resources, stop tasks, close connections, etc. |
| remove() | Executed when plugin is removed | Used to write uninstallation logic, such as deleting database tables, cleaning files, etc. |
| handleSyncMessage(message) | Message synchronization in multi-node deployment | When the application runs in multi-node mode, used to handle messages synchronized from other nodes. |
Typical execution flow of lifecycle methods:
staticImport()afterAdd() → beforeLoad() → load()afterAdd() → beforeLoad() → load() → install()afterAdd() → beforeLoad() → load()afterDisable() is executed when plugin is disabledremove() is executed when plugin is removedIn plugin development, you can access various APIs provided by the application instance through this.app -- this is the core entry point for extending plugin functionality. The app object contains various functional modules of the system, which you can use in plugin lifecycle methods.
| Member Name | Type/Module | Main Purpose |
|---|---|---|
| logger | Logger | Record system logs, supporting info, warn, error, debug levels. See Logger |
| db | Database | ORM layer operations, model registration, event listening, transaction control, etc. See Database |
| resourceManager | ResourceManager | Register and manage REST API resources and action handlers. See ResourceManager |
| acl | ACL | Define permissions, roles, and resource access policies. See ACL |
| cacheManager | CacheManager | Manage system-level cache, supporting Redis, memory cache, and other backends. See Cache |
| cronJobManager | CronJobManager | Register and manage scheduled tasks, supporting Cron expressions. See CronJobManager |
| i18n | I18n | Multi-language translation and localization. See I18n |
| cli | CLI | Register custom commands, extending NocoBase CLI. See Command |
| dataSourceManager | DataSourceManager | Manage multiple data source instances and their connections. See DataSourceManager |
| pm | PluginManager | Dynamically load, enable, disable, and remove plugins, managing inter-plugin dependencies. |
:::tip Tip
For detailed usage of each module, please refer to the corresponding documentation chapters.
:::