2022-11-06 00:34:06 +00:00
|
|
|
# Develop the first plugin
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2022-11-06 00:34:06 +00:00
|
|
|
Before start, you need to install NocoBase:.
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2022-11-06 00:34:06 +00:00
|
|
|
- [create-nocobase-app installation](/welcome/getting-started/installation/create-nocobase-app)
|
|
|
|
- [Git source installation](/welcome/getting-started/installation/git-clone)
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2022-11-06 00:34:06 +00:00
|
|
|
Once NocoBase is installed, we can start our plugin development journey.
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2022-11-06 00:34:06 +00:00
|
|
|
## Create a plugin
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2022-11-06 00:34:06 +00:00
|
|
|
First, you can quickly create an empty plugin via the CLI with the following command.
|
2022-10-31 03:52:17 +00:00
|
|
|
|
|
|
|
```bash
|
2023-09-12 14:39:23 +00:00
|
|
|
yarn pm create @my-project/plugin-hello
|
2022-10-31 03:52:17 +00:00
|
|
|
```
|
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
The directory where the plugin located is `packages/plugins/@my-project/plugin-hello` and the plugin directory structure is
|
2022-10-31 03:52:17 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
|- /hello
|
|
|
|
|- /src
|
2022-11-06 00:34:06 +00:00
|
|
|
|- /client # plugin client code
|
|
|
|
|- /server # plugin server code
|
2022-10-31 03:52:17 +00:00
|
|
|
|- client.d.ts
|
|
|
|
|- client.js
|
2022-11-06 00:34:06 +00:00
|
|
|
|- package.json # plugin package information
|
2022-10-31 03:52:17 +00:00
|
|
|
|- server.d.ts
|
|
|
|
|- server.js
|
|
|
|
```
|
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
Visit the Plugin Manager to view the plugin you just added, the default address is http://localhost:13000/admin/pm/list/local/
|
|
|
|
|
|
|
|
<img src="https://nocobase.oss-cn-beijing.aliyuncs.com/b04d16851fc1bbc2796ecf8f9bc0c3f4.png" />
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
If the plugin is not shown in the plugin manager, you can add it manually with the `pm add` command.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
yarn pm add @my-project/plugin-hello
|
|
|
|
```
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2022-11-06 00:34:06 +00:00
|
|
|
## Code the plugin
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
Look at the `packages/plugins/@my-project/plugin-hello/src/server/plugin.ts` file and modify it to
|
2022-10-31 03:52:17 +00:00
|
|
|
|
|
|
|
```ts
|
|
|
|
import { InstallOptions, Plugin } from '@nocobase/server';
|
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
export class PluginHelloServer extends Plugin {
|
2022-10-31 03:52:17 +00:00
|
|
|
afterAdd() {}
|
|
|
|
|
|
|
|
beforeLoad() {}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
this.db.collection({
|
|
|
|
name: 'hello',
|
2023-09-12 14:39:23 +00:00
|
|
|
fields: [{ type: 'string', name: 'name' }],
|
2022-10-31 03:52:17 +00:00
|
|
|
});
|
|
|
|
this.app.acl.allow('hello', '*');
|
|
|
|
}
|
|
|
|
|
|
|
|
async install(options?: InstallOptions) {}
|
|
|
|
|
|
|
|
async afterEnable() {}
|
|
|
|
|
|
|
|
async afterDisable() {}
|
|
|
|
|
|
|
|
async remove() {}
|
|
|
|
}
|
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
export default PluginHelloServer;
|
2022-10-31 03:52:17 +00:00
|
|
|
```
|
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
## Activate the plugin
|
|
|
|
|
|
|
|
**Operated by command**
|
2022-10-31 03:52:17 +00:00
|
|
|
|
|
|
|
```bash
|
2023-09-12 14:39:23 +00:00
|
|
|
yarn pm enable @my-project/plugin-hello
|
2022-10-31 03:52:17 +00:00
|
|
|
```
|
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
**Operated by UI**
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
Visit the Plugin Manager to view the plugin you just added and click enable.
|
|
|
|
The Plugin Manager page defaults to http://localhost:13000/admin/pm/list/local/
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
<img src="https://nocobase.oss-cn-beijing.aliyuncs.com/7b7df26a8ecc32bb1ebc3f99767ff9f9.png" />
|
|
|
|
|
|
|
|
Node: When the plugin is activated, the hello collection that you just configured is automatically created.
|
2022-10-31 03:52:17 +00:00
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
## Debug the Plugin
|
|
|
|
|
|
|
|
If the app is not started, you need to start the app first
|
2022-10-31 03:52:17 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
# for development
|
|
|
|
yarn dev
|
|
|
|
|
|
|
|
# for production
|
|
|
|
yarn build
|
|
|
|
yarn start
|
|
|
|
```
|
|
|
|
|
2023-09-12 14:39:23 +00:00
|
|
|
Insert data into the hello collection of the plugin
|
2022-10-31 03:52:17 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
curl --location --request POST 'http://localhost:13000/api/hello:create' \
|
|
|
|
--header 'Content-Type: application/json' \
|
|
|
|
--data-raw '{
|
|
|
|
"name": "Hello world"
|
|
|
|
}'
|
|
|
|
```
|
|
|
|
|
2022-11-06 00:34:06 +00:00
|
|
|
View the data
|
2022-10-31 03:52:17 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
curl --location --request GET 'http://localhost:13000/api/hello:list'
|
|
|
|
```
|
2023-09-12 14:39:23 +00:00
|
|
|
|
|
|
|
## Build the plugin
|
|
|
|
|
|
|
|
```bash
|
|
|
|
yarn build plugins/@my-project/plugin-hello --tar
|
|
|
|
|
|
|
|
# step-by-step
|
|
|
|
yarn build plugins/@my-project/plugin-hello
|
|
|
|
yarn nocobase tar plugins/@my-project/plugin-hello
|
|
|
|
```
|
|
|
|
|
|
|
|
The default saved path for the plugin tar is `storage/tar/@my-project/plugin-hello.tar.gz`
|
|
|
|
|
|
|
|
## Upload to other NocoBase applications
|
|
|
|
|
|
|
|
Only supported in v0.14 and above
|
|
|
|
|
|
|
|
<img src="https://nocobase.oss-cn-beijing.aliyuncs.com/8aa8a511aa8c1e87a8f7ee82cf8a1359.gif" />
|