nocobase/packages/plugin-pages/src/server.ts

152 lines
4.3 KiB
TypeScript
Raw Normal View History

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';
import getRoutes from './actions/getRoutes';
import getPageInfo from './actions/getPageInfo';
import * as rolesPagesActions from './actions/roles.pages';
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({
directory: path.resolve(__dirname, 'collections'),
});
resourcer.registerActionHandler('getCollection', getCollection);
resourcer.registerActionHandler('getView', getView);
resourcer.registerActionHandler('getPageInfo', getPageInfo);
resourcer.registerActionHandler('pages:getRoutes', getRoutes);
Object.keys(rolesPagesActions).forEach(actionName => {
resourcer.registerActionHandler(`roles.pages:${actionName}`, rolesPagesActions[actionName]);
});
2020-12-29 03:31:19 +00:00
/*
const [Collection, Page, View] = database.getModels(['collections', 'pages', 'views']);
2020-12-07 09:25:36 +00:00
2020-12-12 08:36:02 +00:00
async function createCollectionPage(model, options) {
// const {
// transaction = await database.sequelize.transaction(),
// } = options;
if (model.get('internal')) {
return;
}
2020-12-12 08:36:02 +00:00
const transaction = await database.sequelize.transaction();
2020-12-07 09:25:36 +00:00
const parent = await Page.findOne({
2020-12-12 08:36:02 +00:00
transaction,
2020-12-07 09:25:36 +00:00
where: {
path: '/collections',
}
});
let page = await Page.findOne({
2020-12-12 08:36:02 +00:00
transaction,
2020-12-07 09:25:36 +00:00
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,
2020-12-12 08:36:02 +00:00
}, {
transaction,
2020-12-07 09:25:36 +00:00
});
}
page.set({
title: model.get('title'),
icon: model.get('icon'),
2020-12-10 12:32:44 +00:00
showInMenu: !!model.get('showInDataMenu'),
2020-12-07 09:25:36 +00:00
});
2020-12-12 08:36:02 +00:00
await page.save({
transaction,
});
2020-12-29 03:31:19 +00:00
await Page.collectionPagesResort({transaction});
2020-12-12 08:36:02 +00:00
await transaction.commit();
2020-12-07 09:25:36 +00:00
}
Collection.addHook('afterCreate', createCollectionPage);
Collection.addHook('afterUpdate', createCollectionPage);
2020-12-12 08:36:02 +00:00
Collection.addHook('afterDestroy', async (model, options) => {
const { transaction } = options;
2020-12-23 12:29:11 +00:00
// console.log('afterDestroy', model);
2020-12-12 08:36:02 +00:00
await Page.destroy({
transaction,
where: {
path: `/collections/${model.get('name')}`,
},
});
2020-12-29 03:31:19 +00:00
await Page.collectionPagesResort({transaction});
2020-12-12 08:36:02 +00:00
});
async function syncViewCollectionPage(model, options) {
const transaction = await database.sequelize.transaction();
const parentPath = `/collections/${model.get('collection_name')}`;
const currentPath = `${parentPath}/views/${model.get('name')}`;
try {
const parent = await Page.findOne({
transaction,
where: {
path: parentPath,
},
});
if (!parent) {
await transaction.rollback();
return;
}
let page = await Page.findOne({
transaction,
where: {
collection: model.get('collection_name'),
path: currentPath,
},
});
if (!page) {
page = await Page.create({
type: 'collection',
collection: model.get('collection_name'),
path: currentPath,
sort: 100,
parent_id: parent.id,
}, {
transaction,
});
}
page.set({
title: model.get('title'),
viewName: model.get('name'),
viewId: model.get('id'),
// icon: model.get('icon'),
showInMenu: !!model.get('showInDataMenu'),
});
await page.save({
transaction,
});
} catch (error) {
await transaction.rollback();
console.error(error);
}
await transaction.commit();
}
View.addHook('beforeValidate', (model) => {
if (model.get('default')) {
model.set('showInDataMenu', true);
}
});
View.addHook('afterCreate', syncViewCollectionPage);
View.addHook('afterUpdate', syncViewCollectionPage);
View.addHook('afterDestroy', async (model, options) => {
await Page.destroy({
where: {
viewId: model.get('id'),
},
});
});
2020-12-29 03:31:19 +00:00
*/
}