ChengLei Shao e991b2965a
feat: collection inheritance (#1069)
* chore: test

* chore: inherited-collection class

* feat: collection inherit

* feat: collection inherit

* feat: inhertis sync runner

* test: get parents fields

* feat: collection inherit style promote

* feat: sync

* feat: sync alter table

* feat: pgOnly Test

* fix: child collection create api

* feat: replace parent field

* chore: reload parent fields

* test: reload collection test

* feat: details are displayed according to conditions

* fix: typo

* feat: inheritance map class

* chore: is parent node

* feat: display where child row created from

* fix: find with appends

* feat: add parent collection fields

* fix: create table

* feat: load fields for all children

* refactor: sync fields from parent

* test: has one field inhertis

* feat: replace child association target

* feat: should not replace child field when parent field update

* test: should update inherit field when parent field update

* feat: only the blocks directly inherited from the current data are displayed

* fix: inherit from multiple collections

* feat: only the blocks directly inherited from the current data are displayed

* fix: test

* feat: parent collection expend

* fix: test

* test: belongsToMany inherits

* test: belongsToMany inherits

* feat: block display

* feat: collection inherite

* feat: collection inherite

* feat: multiple inherits

* fix: sync runner

* feat: collection inherite

* feat: collecton inherits

* feat: cannot be modified after inheritance and saving

* feat: collection inherit for graph

* feat: collection inherits

* fix: drop inhertied field

* fix: should throw error when type conflit

* feat: output inherited fields

* feat: bulk update collection fields

* feat: collection fields

* feat: collection fields

* test: create relation with child table

* fix: test

* fix: test

* fix: test

* feat: style impove

* test: should not replace field with difference type

* feat: add text

* fix: throw error when replace field with difference type

* feat: overriding

* feat: kan bankanban group fields

* feat: calendar block fields

* feat: kan bankanban group fields

* fix: test

* feat: relationship fields

* feat: should delete child's field when parent field deleted

* feat: foreign key filter

* fix: build error & multiple inherit destory field

* fix: test

* chore: disable error

* feat: no recursive update associations (#1091)

* feat: update associations

* fix(collection-manager): should update uiSchema

* chore: flip if

* feat: mutile inherits

* feat: db dialect

* feat: inherits show by database

* chore: git hash into docker image

* fix: js gzip

* fix: dockerfile

* chore: error message

* feat: overriding

* feat: overriding

* feat: overriding

* feat: local

* feat: filter fields by interface

* fix: database logging env

* test: replace hasOne target

* feat: add view

* feat: local

* chore: enable error

* fix: update docs

Co-authored-by: katherinehhh <katherine_15995@163.com>
Co-authored-by: chenos <chenlinxh@gmail.com>
2022-11-16 12:53:58 +08:00

119 lines
3.8 KiB
TypeScript

import { Plugin, PluginManager } from '@nocobase/server';
import send from 'koa-send';
import serve from 'koa-static';
import { isAbsolute, resolve } from 'path';
export class ClientPlugin extends Plugin {
async beforeLoad() {
// const cmd = this.app.findCommand('install');
// if (cmd) {
// cmd.option('--import-demo');
// }
this.app.on('afterInstall', async (app, options) => {
const [opts] = options?.cliArgs || [{}];
if (opts?.importDemo) {
//
}
});
}
async load() {
this.app.acl.allow('app', 'getLang');
this.app.acl.allow('app', 'getInfo');
this.app.acl.allow('app', 'getPlugins');
this.app.acl.allow('plugins', 'getPinned', 'loggedIn');
const dialect = this.app.db.sequelize.getDialect();
this.app.resource({
name: 'app',
actions: {
async getInfo(ctx, next) {
const SystemSetting = ctx.db.getRepository('systemSettings');
const systemSetting = await SystemSetting.findOne();
const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
const currentUser = ctx.state.currentUser;
let lang = enabledLanguages?.[0] || process.env.APP_LANG || 'en-US';
if (enabledLanguages.includes(currentUser?.appLang)) {
lang = currentUser?.appLang;
}
ctx.body = {
database: {
dialect,
},
version: await ctx.app.version.get(),
lang,
};
await next();
},
async getLang(ctx, next) {
const SystemSetting = ctx.db.getRepository('systemSettings');
const systemSetting = await SystemSetting.findOne();
const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
const currentUser = ctx.state.currentUser;
let lang = enabledLanguages?.[0] || process.env.APP_LANG || 'en-US';
if (enabledLanguages.includes(currentUser?.appLang)) {
lang = currentUser?.appLang;
}
ctx.body = {
lang,
};
await next();
},
async getPlugins(ctx, next) {
const pm = ctx.db.getRepository('applicationPlugins');
const items = await pm.find({
filter: {
enabled: true,
},
});
ctx.body = items
.filter((item) => {
try {
const packageName = PluginManager.getPackageName(item.name);
require.resolve(`${packageName}/client`);
return true;
} catch (error) {}
return false;
})
.map((item) => item.name);
await next();
},
},
});
this.app.resource({
name: 'plugins',
actions: {
// TODO: 临时
async getPinned(ctx, next) {
ctx.body = [
{ component: 'CollectionManagerShortcut' },
{ component: 'ACLShortcut' },
{ component: 'WorkflowShortcut' },
{ component: 'SchemaTemplateShortcut' },
{ component: 'SystemSettingsShortcut' },
{ component: 'FileStorageShortcut' },
];
await next();
},
},
});
let root = this.options.dist || `./packages/app/client/dist`;
if (!isAbsolute(root)) {
root = resolve(process.cwd(), root);
}
if (process.env.APP_ENV !== 'production' && root) {
this.app.middleware.nodes.unshift(async (ctx, next) => {
if (ctx.path.startsWith(this.app.resourcer.options.prefix)) {
return next();
}
await serve(root)(ctx, next);
// console.log('koa-send', root, ctx.status);
if (ctx.status == 404) {
return send(ctx, 'index.html', { root });
}
});
}
}
}
export default ClientPlugin;