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

51 lines
1.3 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'));
const tabs = await collection.getTabs({
2020-12-07 08:53:18 +08:00
where: {
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
},
order: [['sort', 'asc']],
}) 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';
}
if (itemTab.type == 'association') {
2020-12-12 16:36:02 +08:00
itemTab.field = await collection.getFields({
where: {
name: itemTab.association,
},
2020-12-12 16:36:02 +08:00
limit: 1,
plain: true,
});
}
tabItems.push(itemTab);
}
ctx.body = {
...collection.toJSON(),
tabs: tabItems,
2020-11-11 20:57:18 +08:00
};
await next();
}