diff --git a/packages/app/package.json b/packages/app/package.json index 99a8e3b4c..1fe8d6015 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -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}'", diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index c938d3314..5adcad2c1 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -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([]); diff --git a/packages/plugin-collections/src/server.ts b/packages/plugin-collections/src/server.ts index 62246c4ed..2e0684f07 100644 --- a/packages/plugin-collections/src/server.ts +++ b/packages/plugin-collections/src/server.ts @@ -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'), }); diff --git a/packages/plugin-file-manager/src/server.ts b/packages/plugin-file-manager/src/server.ts index e1858b31c..fa3521dd4 100644 --- a/packages/plugin-file-manager/src/server.ts +++ b/packages/plugin-file-manager/src/server.ts @@ -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, - }); } diff --git a/packages/plugin-pages/src/actions/getCollection.ts b/packages/plugin-pages/src/actions/getCollection.ts new file mode 100644 index 000000000..380df51c7 --- /dev/null +++ b/packages/plugin-pages/src/actions/getCollection.ts @@ -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[]; + 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(); +} diff --git a/packages/plugin-pages/src/actions/getView.ts b/packages/plugin-pages/src/actions/getView.ts new file mode 100644 index 000000000..76cb5d788 --- /dev/null +++ b/packages/plugin-pages/src/actions/getView.ts @@ -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[]; + 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(); +}; diff --git a/packages/plugin-pages/src/server.ts b/packages/plugin-pages/src/server.ts index 1d5bcdd8b..1a09d941d 100644 --- a/packages/plugin-pages/src/server.ts +++ b/packages/plugin-pages/src/server.ts @@ -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'), diff --git a/packages/plugin-permissions/src/server.ts b/packages/plugin-permissions/src/server.ts index e1858b31c..fa3521dd4 100644 --- a/packages/plugin-permissions/src/server.ts +++ b/packages/plugin-permissions/src/server.ts @@ -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, - }); } diff --git a/packages/plugin-users/src/server.ts b/packages/plugin-users/src/server.ts index e1858b31c..fa3521dd4 100644 --- a/packages/plugin-users/src/server.ts +++ b/packages/plugin-users/src/server.ts @@ -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, - }); }