tachybase_todo/packages/plugin-pages/src/actions/getCollection.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-11-11 20:57:18 +08:00
import { Model, ModelCtor } from '@nocobase/database';
export default async (ctx, next) => {
const { resourceName, resourceKey } = ctx.action.params;
const [Collection, Field, Tab, View] = ctx.db.getModels(['collections', 'fields', 'tabs', 'views']) as ModelCtor<Model>[];
2020-11-11 20:57:18 +08:00
const collection = await Collection.findOne(Collection.parseApiJson({
filter: {
name: resourceName,
},
}));
2020-12-12 16:36:02 +08:00
const defaultView = await collection.getViews({
2020-11-11 20:57:18 +08:00
where: {
default: true,
},
2020-12-12 16:36:02 +08:00
limit: 1,
plain: true,
2020-11-11 20:57:18 +08:00
});
2020-12-12 16:36:02 +08:00
collection.setDataValue('defaultViewName', defaultView.get('name'));
2020-12-25 16:15:58 +08:00
const options = Tab.parseApiJson({
filter: {
2020-12-12 16:36:02 +08:00
enabled: true,
2020-12-08 14:33:28 +08:00
developerMode: ctx.state.developerMode,
2020-12-07 08:53:18 +08:00
},
2020-12-25 16:15:58 +08:00
fields: {
appends: ['associationField'],
},
sort: ['sort'],
});
const tabs = await collection.getTabs(options) as Model[];
const tabItems = [];
for (const tab of tabs) {
const itemTab = {
2020-12-12 16:36:02 +08:00
...tab.get(),
};
2020-12-12 16:36:02 +08:00
if (itemTab.type === 'details' && !itemTab.viewName) {
itemTab.viewName = 'details';
}
2020-12-25 16:15:58 +08:00
// if (itemTab.type == 'association') {
// itemTab.field = await collection.getFields({
// where: {
// name: itemTab.association,
// },
// limit: 1,
// plain: true,
// });
// }
tabItems.push(itemTab);
}
ctx.body = {
...collection.toJSON(),
tabs: tabItems,
2020-11-11 20:57:18 +08:00
};
await next();
}