refactor: code cleanup

This commit is contained in:
chen 2020-11-11 20:57:18 +08:00
parent 812cd0766d
commit bf2e315c9e
9 changed files with 200 additions and 143 deletions

View File

@ -4,6 +4,7 @@
"private": true,
"scripts": {
"start": "concurrently \"nodemon\" \"umi dev\"",
"start-api": "nodemon",
"build": "father-build && umi build",
"postinstall": "umi generate tmp",
"prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'",

View File

@ -34,6 +34,112 @@ const api = Api.create({
},
});
const data = {
title: '后台应用',
path: '/',
type: 'layout',
template: 'TopMenuLayout',
order: 1,
children: [
{
title: '仪表盘',
type: 'page',
path: '/dashboard',
icon: 'dashboard',
template: 'page1',
order: 2,
},
{
title: '数据',
type: 'layout',
path: '/collections',
icon: 'dashboard',
template: 'SideMenuLayout',
order: 3,
children: [
{
title: '页面3',
type: 'page',
path: '/collections/page3',
icon: 'dashboard',
template: 'page3',
order: 5,
},
{
title: '页面4',
type: 'page',
path: '/collections/page4',
icon: 'dashboard',
template: 'page4',
order: 6,
},
{
title: '页面5',
type: 'collection',
path: '/collections/collection1',
icon: 'dashboard',
template: 'collection',
collection: 'collection1',
order: 7,
},
]
},
{
title: '用户',
type: 'layout',
path: '/users',
icon: 'dashboard',
template: 'SideMenuLayout',
order: 3,
children: [
{
title: '用户管理',
type: 'collection',
path: '/users/users',
icon: 'dashboard',
template: 'collection',
collection: 'users',
order: 1,
},
]
},
{
title: '配置',
type: 'layout',
path: '/settings',
icon: 'dashboard',
template: 'SideMenuLayout',
order: 4,
children: [
{
title: '页面与菜单',
type: 'collection',
collection: 'pages',
path: '/settings/pages',
icon: 'dashboard',
order: 1,
},
{
title: '数据表配置',
type: 'collection',
collection: 'collections',
path: '/settings/collections',
icon: 'dashboard',
order: 2,
},
{
title: '权限配置',
type: 'collection',
collection: 'roles',
path: '/settings/roles',
icon: 'dashboard',
order: 3,
},
]
},
],
};
(async () => {
await api
.plugins([
@ -48,6 +154,12 @@ const api = Api.create({
const database: Database = api.database;
await database.sync();
const Page = database.getModel('pages');
const page = await Page.create(data);
await page.updateAssociations(data);
const [Collection, View, Action, Tab] = database.getModels(['collections', 'views', 'actions', 'tabs']);
const tables = database.getTables([]);

View File

@ -1,27 +1,15 @@
import path from 'path';
import Database, { ModelCtor } from '@nocobase/database';
import Resourcer from '@nocobase/resourcer';
import CollectionModel from './models/collection';
import getCollection from './actions/getCollection';
import getView from './actions/getView';
export default async function (this: any, options = {}) {
const database: Database = this.database;
const resourcer: Resourcer = this.resourcer;
const tables = database.import({
database.import({
directory: path.resolve(__dirname, 'collections'),
});
await database.sync({
tables,
});
resourcer.registerHandler('getCollection', getCollection);
resourcer.registerHandler('getView', getView);
// console.log(resourcer.getRegisteredHandlers());
resourcer.import({
directory: path.resolve(__dirname, 'resources'),
});

View File

@ -6,15 +6,11 @@ export default async function (options = {}) {
const database: Database = this.database;
const resourcer: Resourcer = this.resourcer;
const tables = database.import({
database.import({
directory: path.resolve(__dirname, 'collections'),
});
resourcer.import({
directory: path.resolve(__dirname, 'resources'),
});
await database.sync({
tables,
});
}

View File

@ -0,0 +1,33 @@
import { Model, ModelCtor } from '@nocobase/database';
import { ResourceOptions } from '@nocobase/resourcer';
import { get } from 'lodash';
export default async (ctx, next) => {
const { resourceName, resourceKey } = ctx.action.params;
const [Collection, Tab, View] = ctx.db.getModels(['collections', 'tabs', 'views']) as ModelCtor<Model>[];
const collection = await Collection.findOne(Collection.parseApiJson({
filter: {
name: resourceName,
},
// fields: {
// // appends: ['tabs'],
// },
}));
const views = await collection.getViews({
where: {
default: true,
},
});
collection.setDataValue('defaultViewId', get(views, [0, 'id']));
collection.setDataValue('defaultViewName', get(views, [0, 'name']));
const tabs = await collection.getTabs();
ctx.body = {
...collection.toJSON(),
tabs: tabs.map(tab => ({
...tab.toJSON(),
...tab.options,
viewCollectionName: tab.type == 'association' ? tab.options.association : tab.collection_name,
})),
};
await next();
}

View File

@ -0,0 +1,45 @@
import { ResourceOptions } from '@nocobase/resourcer';
import { Model, ModelCtor } from '@nocobase/database';
import { get } from 'lodash';
export default async (ctx, next) => {
const { resourceName, resourceKey } = ctx.action.params;
const [View, Field, Action] = ctx.db.getModels(['views', 'fields', 'actions']) as ModelCtor<Model>[];
const view = await View.findOne(View.parseApiJson({
filter: {
collection_name: resourceName,
name: resourceKey,
},
fields: {
appends: ['actions', 'fields'],
},
}));
const collection = await view.getCollection();
const fields = await collection.getFields();
const actions = await collection.getActions();
const actionNames = view.options.actionNames||[];
console.log(view.options);
if (view.type === 'table') {
const defaultTabs = await collection.getTabs({
where: {
default: true,
},
});
view.setDataValue('defaultTabName', get(defaultTabs, [0, 'name']));
}
if (view.options.updateViewId) {
view.setDataValue('rowViewName', view.options.updateViewName);
}
view.setDataValue('viewCollectionName', view.collection_name);
ctx.body = {
...view.toJSON(),
...(view.options||{}),
fields,
actions: actions.filter(action => actionNames.includes(action.name)).map(action => ({
...action.toJSON(),
...action.options,
viewCollectionName: action.collection_name,
})),
};
await next();
};

View File

@ -1,129 +1,19 @@
import path from 'path';
import Database from '@nocobase/database';
import Resourcer from '@nocobase/resourcer';
const data = {
title: '后台应用',
path: '/',
type: 'layout',
template: 'TopMenuLayout',
order: 1,
children: [
{
title: '仪表盘',
type: 'page',
path: '/dashboard',
icon: 'dashboard',
template: 'page1',
order: 2,
},
{
title: '数据',
type: 'layout',
path: '/collections',
icon: 'dashboard',
template: 'SideMenuLayout',
order: 3,
children: [
{
title: '页面3',
type: 'page',
path: '/collections/page3',
icon: 'dashboard',
template: 'page3',
order: 5,
},
{
title: '页面4',
type: 'page',
path: '/collections/page4',
icon: 'dashboard',
template: 'page4',
order: 6,
},
{
title: '页面5',
type: 'collection',
path: '/collections/collection1',
icon: 'dashboard',
template: 'collection',
collection: 'collection1',
order: 7,
},
]
},
{
title: '用户',
type: 'layout',
path: '/users',
icon: 'dashboard',
template: 'SideMenuLayout',
order: 3,
children: [
{
title: '用户管理',
type: 'collection',
path: '/users/users',
icon: 'dashboard',
template: 'collection',
collection: 'users',
order: 1,
},
]
},
{
title: '配置',
type: 'layout',
path: '/settings',
icon: 'dashboard',
template: 'SideMenuLayout',
order: 4,
children: [
{
title: '页面与菜单',
type: 'collection',
collection: 'pages',
path: '/settings/pages',
icon: 'dashboard',
order: 1,
},
{
title: '数据表配置',
type: 'collection',
collection: 'collections',
path: '/settings/collections',
icon: 'dashboard',
order: 2,
},
{
title: '权限配置',
type: 'collection',
collection: 'roles',
path: '/settings/roles',
icon: 'dashboard',
order: 3,
},
]
},
],
};
import getCollection from './actions/getCollection';
import getView from './actions/getView';
export default async function (options = {}) {
const database: Database = this.database;
const resourcer: Resourcer = this.resourcer;
const tables = database.import({
database.import({
directory: path.resolve(__dirname, 'collections'),
});
await database.sync({
tables,
});
const Page = database.getModel('pages');
const page = await Page.create(data);
await page.updateAssociations(data);
resourcer.registerHandler('getCollection', getCollection);
resourcer.registerHandler('getView', getView);
resourcer.import({
directory: path.resolve(__dirname, 'resources'),

View File

@ -6,15 +6,11 @@ export default async function (options = {}) {
const database: Database = this.database;
const resourcer: Resourcer = this.resourcer;
const tables = database.import({
database.import({
directory: path.resolve(__dirname, 'collections'),
});
resourcer.import({
directory: path.resolve(__dirname, 'resources'),
});
await database.sync({
tables,
});
}

View File

@ -6,15 +6,11 @@ export default async function (options = {}) {
const database: Database = this.database;
const resourcer: Resourcer = this.resourcer;
const tables = database.import({
database.import({
directory: path.resolve(__dirname, 'collections'),
});
resourcer.import({
directory: path.resolve(__dirname, 'resources'),
});
await database.sync({
tables,
});
}