nocobase/docs/en-US/development/server/index.md

73 lines
1.6 KiB
Markdown
Raw Normal View History

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