tachybase_todo/packages/plugin-ui-router/src/server.ts

65 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-07-11 22:20:54 +08:00
import path from 'path';
2021-09-23 00:16:04 +08:00
import { PluginOptions } from '@nocobase/server';
2021-07-11 22:20:54 +08:00
import { registerModels } from '@nocobase/database';
import * as models from './models';
2021-07-12 22:13:48 +08:00
import getAccessible from './actions/getAccessible';
2021-09-11 18:53:26 +08:00
import * as uiSchema from './ui-schema';
2021-07-11 22:20:54 +08:00
2021-09-23 00:16:04 +08:00
export default {
name: 'ui-router',
async load() {
const database = this.app.db;
registerModels(models);
2021-07-11 22:20:54 +08:00
2021-09-23 00:16:04 +08:00
database.import({
directory: path.resolve(__dirname, 'collections'),
});
this.app.resourcer.registerActionHandler('routes:getAccessible', getAccessible);
const Route = database.getModel('routes');
this.app.on('db.init', async () => {
const data = [
{
type: 'redirect',
from: '/',
to: '/admin',
exact: true,
},
{
type: 'route',
path: '/admin/:name(.+)?',
component: 'AdminLayout',
title: `NocoBase`,
2021-09-23 00:16:04 +08:00
uiSchema: uiSchema.menu,
},
{
type: 'route',
component: 'AuthLayout',
children: [
{
type: 'route',
path: '/signin',
2021-09-23 00:16:04 +08:00
component: 'RouteSchemaRenderer',
title: `{{t("Sign in")}}`,
uiSchema: uiSchema.signin,
2021-09-23 00:16:04 +08:00
},
{
type: 'route',
path: '/signup',
2021-09-23 00:16:04 +08:00
component: 'RouteSchemaRenderer',
title: `{{t("Sign up")}}`,
uiSchema: uiSchema.signup,
2021-09-23 00:16:04 +08:00
},
],
},
];
for (const item of data) {
const route = await Route.create(item);
await route.updateAssociations(item);
}
});
}
} as PluginOptions;