2020-11-11 07:23:39 +00:00
|
|
|
import path from 'path';
|
|
|
|
import Database from '@nocobase/database';
|
|
|
|
import Resourcer from '@nocobase/resourcer';
|
2020-11-11 12:57:18 +00:00
|
|
|
import getCollection from './actions/getCollection';
|
|
|
|
import getView from './actions/getView';
|
2020-11-13 14:01:14 +00:00
|
|
|
import getRoutes from './actions/getRoutes';
|
|
|
|
import getPageInfo from './actions/getPageInfo';
|
2020-11-11 07:23:39 +00:00
|
|
|
|
|
|
|
export default async function (options = {}) {
|
|
|
|
const database: Database = this.database;
|
|
|
|
const resourcer: Resourcer = this.resourcer;
|
|
|
|
|
2020-11-11 12:57:18 +00:00
|
|
|
database.import({
|
2020-11-11 07:23:39 +00:00
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
|
|
|
});
|
|
|
|
|
2020-12-02 05:48:19 +00:00
|
|
|
resourcer.registerActionHandler('getCollection', getCollection);
|
|
|
|
resourcer.registerActionHandler('getView', getView);
|
|
|
|
resourcer.registerActionHandler('getPageInfo', getPageInfo);
|
|
|
|
resourcer.registerActionHandler('pages:getRoutes', getRoutes);
|
2020-12-07 09:25:36 +00:00
|
|
|
|
|
|
|
const [Collection, Page] = database.getModels(['collections', 'pages']);
|
|
|
|
|
|
|
|
async function createCollectionPage(model) {
|
|
|
|
if (!model.get('showInDataMenu')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const parent = await Page.findOne({
|
|
|
|
where: {
|
|
|
|
path: '/collections',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let page = await Page.findOne({
|
|
|
|
where: {
|
|
|
|
collection: model.get('name'),
|
|
|
|
path: `/collections/${model.get('name')}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!page) {
|
|
|
|
page = await Page.create({
|
|
|
|
type: 'collection',
|
|
|
|
collection: model.get('name'),
|
|
|
|
path: `/collections/${model.get('name')}`,
|
|
|
|
sort: 100,
|
|
|
|
parent_id: parent.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
page.set({
|
|
|
|
title: model.get('title'),
|
|
|
|
icon: model.get('icon'),
|
|
|
|
showInMenu: model.get('showInDataMenu'),
|
|
|
|
});
|
|
|
|
page.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
Collection.addHook('afterCreate', createCollectionPage);
|
|
|
|
Collection.addHook('afterUpdate', createCollectionPage);
|
2020-11-11 07:23:39 +00:00
|
|
|
}
|