2022-11-06 09:01:38 +08:00
|
|
|
# Overview
|
2022-10-31 22:41:24 +08:00
|
|
|
|
2022-11-06 09:01:38 +08:00
|
|
|
The server-side for an initialized, empty plugin has the following directory structure.
|
2022-10-31 22:41:24 +08:00
|
|
|
|
|
|
|
```bash
|
|
|
|
|- /my-plugin
|
|
|
|
|- /src
|
2022-11-06 09:01:38 +08:00
|
|
|
|- /server # Server-side code of the plugin
|
|
|
|
|- plugin.ts # Classes of the plugin
|
|
|
|
|- index.ts # server-side entry
|
2022-10-31 22:41:24 +08:00
|
|
|
|- server.d.ts
|
|
|
|
|- server.js
|
|
|
|
```
|
|
|
|
|
2022-11-06 09:01:38 +08:00
|
|
|
`plugin.ts` provides calls to various methods of the plugin lifecycle
|
2022-10-31 22:41:24 +08:00
|
|
|
|
|
|
|
```ts
|
|
|
|
import { InstallOptions, Plugin } from '@nocobase/server';
|
|
|
|
|
|
|
|
export class MyPlugin extends Plugin {
|
|
|
|
afterAdd() {
|
2022-11-06 09:01:38 +08:00
|
|
|
// After the plugin pm.add is registered. This is mainly used to place a listener for the app beforeLoad event
|
2022-11-02 11:33:07 +08:00
|
|
|
this.app.on('beforeLoad');
|
2022-10-31 22:41:24 +08:00
|
|
|
}
|
|
|
|
beforeLoad() {
|
2022-11-06 09:01:38 +08:00
|
|
|
// Custom classes or methods
|
2022-10-31 22:41:24 +08:00
|
|
|
this.db.registerFieldTypes()
|
|
|
|
this.db.registerModels()
|
|
|
|
this.db.registerRepositories()
|
|
|
|
this.db.registerOperators()
|
2022-11-06 09:01:38 +08:00
|
|
|
// Event Listening
|
2022-10-31 22:41:24 +08:00
|
|
|
this.app.on();
|
|
|
|
this.db.on();
|
|
|
|
}
|
|
|
|
async load() {
|
2022-11-06 09:01:38 +08:00
|
|
|
// Define collection
|
2022-10-31 22:41:24 +08:00
|
|
|
this.db.collection();
|
2022-11-06 09:01:38 +08:00
|
|
|
// Import collection
|
2022-10-31 22:41:24 +08:00
|
|
|
this.db.import();
|
|
|
|
this.db.addMigrations();
|
|
|
|
|
2022-11-06 09:01:38 +08:00
|
|
|
// Define resource
|
2022-10-31 22:41:24 +08:00
|
|
|
this.resourcer.define();
|
|
|
|
// resource action
|
|
|
|
this.resourcer.registerActions();
|
|
|
|
|
2022-11-06 09:01:38 +08:00
|
|
|
// Register middleware
|
2022-10-31 22:41:24 +08:00
|
|
|
this.resourcer.use();
|
|
|
|
this.acl.use();
|
|
|
|
this.app.use();
|
|
|
|
|
2022-11-06 09:01:38 +08:00
|
|
|
// Customize the multilingual package
|
2022-10-31 22:41:24 +08:00
|
|
|
this.app.i18n.addResources();
|
2022-11-06 09:01:38 +08:00
|
|
|
// Customize command line
|
2022-10-31 22:41:24 +08:00
|
|
|
this.app.command();
|
|
|
|
}
|
|
|
|
async install(options?: InstallOptions) {
|
2022-11-06 09:01:38 +08:00
|
|
|
// Logic for installing
|
2022-10-31 22:41:24 +08:00
|
|
|
}
|
|
|
|
async afterEnable() {
|
2022-11-06 09:01:38 +08:00
|
|
|
// After activation
|
2022-10-31 22:41:24 +08:00
|
|
|
}
|
|
|
|
async afterDisable() {
|
2022-11-06 09:01:38 +08:00
|
|
|
// After disable
|
2022-10-31 22:41:24 +08:00
|
|
|
}
|
|
|
|
async remove() {
|
2022-11-06 09:01:38 +08:00
|
|
|
// Logic for removing
|
2022-10-31 22:41:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MyPlugin;
|
|
|
|
```
|