2022-08-16 06:41:29 +00:00
|
|
|
/*
|
|
|
|
# 编写一个最简单的插件
|
|
|
|
|
|
|
|
# 步骤
|
|
|
|
|
|
|
|
Step 1: Start app
|
2022-09-18 06:10:01 +00:00
|
|
|
yarn run:example app/custom-plugin start
|
2022-08-16 06:41:29 +00:00
|
|
|
|
|
|
|
Step 2: View test list
|
|
|
|
http://localhost:13000/api/test:list
|
|
|
|
*/
|
|
|
|
import { Application, Plugin } from '@nocobase/server';
|
2022-09-18 06:10:01 +00:00
|
|
|
import { uid } from '@nocobase/utils';
|
2022-08-16 06:41:29 +00:00
|
|
|
|
|
|
|
const app = new Application({
|
|
|
|
database: {
|
|
|
|
logging: process.env.DB_LOGGING === 'on' ? console.log : false,
|
|
|
|
dialect: process.env.DB_DIALECT as any,
|
|
|
|
storage: process.env.DB_STORAGE,
|
|
|
|
username: process.env.DB_USER,
|
|
|
|
password: process.env.DB_PASSWORD,
|
|
|
|
database: process.env.DB_DATABASE,
|
|
|
|
host: process.env.DB_HOST,
|
|
|
|
port: process.env.DB_PORT as any,
|
|
|
|
timezone: process.env.DB_TIMEZONE,
|
2022-09-18 06:10:01 +00:00
|
|
|
tablePrefix: `t_${uid()}_`,
|
2022-08-16 06:41:29 +00:00
|
|
|
},
|
|
|
|
resourcer: {
|
|
|
|
prefix: '/api',
|
|
|
|
},
|
|
|
|
plugins: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Encapsulate modules into a plugin
|
|
|
|
class MyPlugin extends Plugin {
|
|
|
|
getName() {
|
|
|
|
return 'MyPlugin';
|
|
|
|
}
|
|
|
|
async load() {
|
2022-08-16 14:30:31 +00:00
|
|
|
this.app.resource({
|
2022-08-16 06:41:29 +00:00
|
|
|
name: 'test',
|
|
|
|
actions: {
|
|
|
|
async list(ctx) {
|
|
|
|
ctx.body = 'test list';
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register plugin
|
|
|
|
app.plugin(MyPlugin);
|
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
app.runAsCLI();
|
|
|
|
}
|
|
|
|
|
|
|
|
export default app;
|