feat: add status field to multi-apps

This commit is contained in:
chenos 2021-09-30 09:44:23 +08:00
parent 55f6564ea8
commit 31ddfbee22
2 changed files with 46 additions and 12 deletions

View File

@ -43,7 +43,7 @@ app.parse(process.argv);
/* /*
yarn examples simple db sync yarn examples simple db:sync
yarn examples simple start yarn examples simple start

View File

@ -1,6 +1,6 @@
import compose from 'koa-compose';
import { Application, PluginOptions } from '@nocobase/server'; import { Application, PluginOptions } from '@nocobase/server';
import Koa from 'koa'; import Koa from 'koa';
import { Model } from '@nocobase/database';
function createApp(opts) { function createApp(opts) {
const { name } = opts; const { name } = opts;
@ -35,13 +35,14 @@ function createApp(opts) {
bodyParser: false, bodyParser: false,
// dataWrapping: false, // dataWrapping: false,
resourcer: { resourcer: {
prefix: `/api/saas/${name}`, prefix: `/api/multiapps/${name}`,
}, },
}; };
const app = new Application(options); const app = new Application(options);
app.db.sequelize.beforeDefine((model, options) => { app.db.sequelize.beforeDefine((model, options) => {
options.tableName = `saas_${name}_${options.tableName || options.name.plural options.tableName = `multiapps_${name}_${
options.tableName || options.name.plural
}`; }`;
}); });
@ -50,7 +51,9 @@ function createApp(opts) {
actions: { actions: {
async getInfo(ctx, next) { async getInfo(ctx, next) {
ctx.body = { ctx.body = {
m: Object.values(ctx.db.sequelize.models).map((m: any) => m.tableName), m: Object.values(ctx.db.sequelize.models).map(
(m: any) => m.tableName,
),
}; };
await next(); await next();
}, },
@ -102,7 +105,7 @@ function multiApps({ getAppName }) {
apps.set(appName, app); apps.set(appName, app);
} }
console.log('..........................start........................') console.log('..........................start........................');
// 完全隔离的做法 // 完全隔离的做法
const app = apps.get(appName) as Application; const app = apps.get(appName) as Application;
const bodyParser = async (ctx2, next) => { const bodyParser = async (ctx2, next) => {
@ -115,7 +118,7 @@ function multiApps({ getAppName }) {
await handleRequest(ctx.req, ctx.res); await handleRequest(ctx.req, ctx.res);
const index = app.middleware.indexOf(bodyParser); const index = app.middleware.indexOf(bodyParser);
app.middleware.splice(index, 1); app.middleware.splice(index, 1);
console.log('..........................end........................') console.log('..........................end........................');
// await next(); // await next();
}; };
} }
@ -129,16 +132,44 @@ export default {
title: '应用', title: '应用',
fields: [ fields: [
{ {
type: 'string', type: 'uid',
name: 'name', name: 'name',
prefix: 'a',
interface: 'string', interface: 'string',
unique: true, unique: true,
uiSchema: { uiSchema: {
type: 'string', type: 'string',
title: '名称', title: '应用标识',
'x-component': 'Input', 'x-component': 'Input',
}, },
}, },
{
type: 'string',
name: 'title',
interface: 'string',
unique: true,
uiSchema: {
type: 'string',
title: '应用名称',
'x-component': 'Input',
},
},
{
type: 'string',
name: 'status',
interface: 'select',
uiSchema: {
type: 'string',
title: '状态',
'x-component': 'Select',
default: 'initializing',
enum: [
{ value: 'initializing', label: '正在初始化' },
{ value: 'running', label: '运行中' },
{ value: 'stopped', label: '已停止' },
],
},
},
], ],
}); });
this.app.use( this.app.use(
@ -148,7 +179,7 @@ export default {
}, },
}), }),
); );
this.app.db.on('applications.afterCreate', async (model) => { this.app.db.on('applications.afterCreate', async (model: Model) => {
const name = model.get('name'); const name = model.get('name');
const app = createApp({ const app = createApp({
name, name,
@ -163,7 +194,10 @@ export default {
}); });
await app.emitAsync('db.init'); await app.emitAsync('db.init');
await app.destroy(); await app.destroy();
})() this.app['apps'].set(name, app);
model.set('status', 'running');
await model.save({ hooks: false });
})();
}); });
this.app this.app
.command('app:create') .command('app:create')