tachybase_todo/packages/plugins/@nocobase/plugin-collection-manager/src/server/server.ts
chenos 7779cd79ac
refactor: optimize the command line (#3339)
* fix: perform load action on boot main app

* feat: add dataType option in collection duplicator

* chore: reset optional dumpable config

* chore: dump command

* chore: dump & restore command

* chore: delay restore

* fix: dump test

* chore: restore command

* chore: dump command action

* chore: dumpable collection api

* chore: client collection option

* feat: backup& restore client

* chore: content disposition header in dump response

* chore: download backup field

* feat: collection origin option

* fix: test

* chore: collection manager collection origin

* chore: upload  backup field

* chore: upload restore file

* chore: upload restore file

* fix: test

* chore: backup and restore support learn more

* refactor: upload restore file

* refactor: upload restore file

* fix: test

* fix: test

* chore: dumpable collection with title

* chore: pg only test

* chore: test

* fix: test

* chore: test sleep

* style: locale improve

* refactor: download backup file

* refactor: start restore

* fix: restore key name

* refactor: start restore

* refactor: start restore

* refactor: start restore

* refactor: start restore

* refactor: start restore

* refactor: start restore

* chore: unify duplicator option

* fix: dump empty collection

* chore: test

* chore: test

* style: style improve

* refactor: locale improve

* chore: dumpalbe collection orders

* style: style improve

* style: style improve

* style: icon adjust

* chore: nginx body size

* chore: get file status

* feat: run dump task

* feat: download api

* chore: backup files resourcer

* feat: restore destroy api

* chore: backup files resoucer

* feat: list backup files action

* chore: get collection meta from dumped file

* fix: dump file name

* fix: test

* chore: backup and restore ui

* chore: swagger api for backup & restore

* chore: api doc

* chore: api doc

* chore: api doc

* chore: backup and restore ui

* chore: backup and restore ui

* chore: backup and restore ui

* chore: backup and restore ui

* chore: backup and restore ui

* fix: restore values

* style: style improve

* fix: download field respontype

* fix: restore form local file

* refactor: local improve

* refactor: delete backup file

* fix: in progress status

* refactor: locale improve

* refactor: locale improve

* refactor: style improve

* refactor: style improve

* refactor: style improve

* test: dump collection table attribute

* chore: dump collection with table attributes

* chore: test

* chore: create new table in restore

* fix: import error

* chore: restore table from backup file

* chore: sync collection after restore collections

* fix: restore json data

* style: style improve

* chore: restore with fields

* chore: test

* fix: test

* fix: test with underscored

* style: style improve

* fix: lock file state

* chore: add test file

* refactor: backup & restore plugin

* fix: mysql test

* chore: skip import view collection

* chore: restore collection with inherits topo order

* fix: import

* style: style improve

* fix: restore sequence fields

* fix: themeConfig collection duplicator option

* fix: restore with dialectOnly meta

* fix: throw error

* fix: restore

* fix: import backup file created in postgres into mysql

* fix: repeated items in inherits

* chore: upgrade after restore

* feat: check database env before restore

* feat: handle autoincr val in postgres

* chore: sqlite & mysql queryInterface

* chore: test

* fix: test

* chore: test

* fix: build

* fix: pg test

* fix: restore with date field

* chore: theme-config collection

* chore: chage import collections method to support collection origin

* chore: fallback get autoincr value in mysql

* fix: dataType normalize

* chore: delay restore

* chore: test

* fix: build

* feat: collectin onDump

* feat: collection onDump interface

* chore: dump with view collection

* chore: sync in restore

* refactor: locale improve

* refactor: code improve

* fix: test

* fix: data sync

* chore: rename backup & restore plugin

* chore: skip test

* style: style improve

* style: style improve

* style: style improve

* style: style improve

* chore: import version check

* chore: backup file dir

* chore: build

* fix: bugs

* fix: error

* fix: pageSize

* fix: import origin

* fix: improve code

* fix: remove namespace

* chore: dump rules config

* fix: dump custom collection

* chore: version

* fix: test

* fix: test

* fix: test

* fix: test

* chore: test

* fix: load custom collection

* fix: client

* fix: translation

* chore: code

* fix: bug

* fix:  support shared option

* fix: roles collection dumpRules

* chore: test

* fix: define collections

* chore: collection group

* fix: translation

* fix: translation

* fix: restore options

* chore: restore command

* refactor: optimize the command line

* chore: dump error

* fix: test error

* fix:  test error

* fix: test error

* fix: test error

* fix: test error

* fix: skip cli test cases

* fix: test error

* fix: too many open files

* fix: update migration version

* fix: migrations

* fix: upgrade

* fix: error

* fix: migration error

* fix: upgrade

* fix: test error

* fix: timeout

* fix: width

* feat: auto load collections

* fix: test error

* fix: test error

* fix: test error

* fix: test error

* fix: test error

* fix: test error

* fix: test error

* fix: ipc error

* fix: test error

---------

Co-authored-by: Chareice <chareice@live.com>
Co-authored-by: katherinehhh <katherine_15995@163.com>
2024-01-08 19:05:14 +08:00

371 lines
12 KiB
TypeScript

import { Filter, InheritedCollection, UniqueConstraintError } from '@nocobase/database';
import PluginErrorHandler from '@nocobase/plugin-error-handler';
import { Plugin } from '@nocobase/server';
import { Mutex } from 'async-mutex';
import lodash from 'lodash';
import path from 'path';
import * as process from 'process';
import { CollectionRepository } from '.';
import {
afterCreateForForeignKeyField,
afterCreateForReverseField,
beforeCreateForReverseField,
beforeDestroyForeignKey,
beforeInitOptions,
} from './hooks';
import { beforeCreateForValidateField } from './hooks/beforeCreateForValidateField';
import { beforeCreateForViewCollection } from './hooks/beforeCreateForViewCollection';
import { CollectionModel, FieldModel } from './models';
import collectionActions from './resourcers/collections';
import sqlResourcer from './resourcers/sql';
import viewResourcer from './resourcers/views';
export class CollectionManagerPlugin extends Plugin {
public schema: string;
private loadFilter: Filter = {};
setLoadFilter(filter: Filter) {
this.loadFilter = filter;
}
async beforeLoad() {
if (this.app.db.inDialect('postgres')) {
this.schema = process.env.COLLECTION_MANAGER_SCHEMA || this.db.options.schema || 'public';
}
this.app.db.registerRepositories({
CollectionRepository,
});
this.app.db.registerModels({
CollectionModel,
FieldModel,
});
this.db.addMigrations({
namespace: 'collection-manager',
directory: path.resolve(__dirname, './migrations'),
context: {
plugin: this,
},
});
this.app.acl.registerSnippet({
name: `pm.${this.name}.collections`,
actions: ['collections:*', 'collections.fields:*', 'dbViews:*', 'collectionCategories:*', 'sqlCollection:*'],
});
this.app.db.on('collections.beforeCreate', async (model) => {
if (this.app.db.inDialect('postgres') && this.schema && model.get('from') != 'db2cm' && !model.get('schema')) {
model.set('schema', this.schema);
}
});
this.app.db.on('collections.beforeCreate', beforeCreateForViewCollection(this.db));
this.app.db.on(
'collections.afterCreateWithAssociations',
async (model: CollectionModel, { context, transaction }) => {
if (context) {
await model.migrate({
transaction,
});
}
},
);
this.app.db.on('collections.beforeDestroy', async (model: CollectionModel, options) => {
const removeOptions = {};
if (options.transaction) {
removeOptions['transaction'] = options.transaction;
}
const cascade = lodash.get(options, 'context.action.params.cascade', false);
if (cascade === true || cascade === 'true') {
removeOptions['cascade'] = true;
}
await model.remove(removeOptions);
});
// 要在 beforeInitOptions 之前处理
this.app.db.on('fields.beforeCreate', beforeCreateForReverseField(this.app.db));
this.app.db.on('fields.beforeCreate', async (model, options) => {
const collectionName = model.get('collectionName');
const collection = this.app.db.getCollection(collectionName);
if (!collection) {
return;
}
if (collection.isInherited() && (<InheritedCollection>collection).parentFields().has(model.get('name'))) {
model.set('overriding', true);
}
});
this.app.db.on('fields.beforeValidate', async (model) => {
if (model.get('name') && model.get('name').includes('.')) {
model.set('field', model.get('name'));
model.set('name', model.get('name').replace(/\./g, '_'));
}
});
this.app.db.on('fields.beforeCreate', async (model, options) => {
if (model.get('source')) return;
const type = model.get('type');
const fn = beforeInitOptions[type];
if (fn) {
await fn(model, { database: this.app.db });
}
});
this.app.db.on('fields.beforeCreate', beforeCreateForValidateField());
this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db));
this.app.db.on('fields.afterCreate', async (model: FieldModel, { context, transaction }) => {
if (context) {
await model.migrate({
isNew: true,
transaction,
});
}
});
// after migrate
this.app.db.on('fields.afterCreate', afterCreateForForeignKeyField(this.app.db));
this.app.db.on('fields.beforeUpdate', async (model, options) => {
const newValue = options.values;
if (
model.get('reverseKey') &&
lodash.get(newValue, 'reverseField') &&
!lodash.get(newValue, 'reverseField.key')
) {
const field = await this.app.db
.getModel('fields')
.findByPk(model.get('reverseKey'), { transaction: options.transaction });
if (field) {
throw new Error('cant update field without a reverseField key');
}
}
// todo: 目前只支持一对多
if (model.get('sortable') && model.get('type') === 'hasMany') {
model.set('sortBy', model.get('foreignKey') + 'Sort');
}
});
this.app.db.on('fields.afterUpdate', async (model: FieldModel, { context, transaction }) => {
const prevOptions = model.previous('options');
const currentOptions = model.get('options');
if (context) {
const prev = prevOptions['unique'];
const next = currentOptions['unique'];
if (Boolean(prev) !== Boolean(next)) {
await model.syncUniqueIndex({ transaction });
}
}
const prevDefaultValue = prevOptions['defaultValue'];
const currentDefaultValue = currentOptions['defaultValue'];
if (prevDefaultValue != currentDefaultValue) {
await model.syncDefaultValue({ transaction, defaultValue: currentDefaultValue });
}
const prevOnDelete = prevOptions['onDelete'];
const currentOnDelete = currentOptions['onDelete'];
if (prevOnDelete != currentOnDelete) {
await model.syncReferenceCheckOption({ transaction });
}
if (model.get('type') === 'hasMany' && model.get('sortable') && model.get('sortBy')) {
await model.syncSortByField({ transaction });
}
});
this.app.db.on('fields.afterSaveWithAssociations', async (model: FieldModel, { context, transaction }) => {
if (context) {
await model.load({ transaction });
}
});
// before field remove
this.app.db.on('fields.beforeDestroy', beforeDestroyForeignKey(this.app.db));
const mutex = new Mutex();
this.app.db.on('fields.beforeDestroy', async (model: FieldModel, options) => {
await mutex.runExclusive(async () => {
await model.remove(options);
});
});
this.app.db.on('fields.afterDestroy', async (model: FieldModel, options) => {
const { transaction } = options;
const collectionName = model.get('collectionName');
const childCollections = this.db.inheritanceMap.getChildren(collectionName);
const childShouldRemoveField = Array.from(childCollections).filter((item) => {
const parents = Array.from(this.db.inheritanceMap.getParents(item))
.map((parent) => {
const collection = this.db.getCollection(parent);
const field = collection.getField(model.get('name'));
return field;
})
.filter(Boolean);
return parents.length == 0;
});
await this.db.getCollection('fields').repository.destroy({
filter: {
name: model.get('name'),
collectionName: {
$in: childShouldRemoveField,
},
options: {
overriding: true,
},
},
transaction,
});
});
const loadCollections = async () => {
this.log.debug('loading custom collections', { method: 'loadCollections' });
this.app.setMaintainingMessage('loading custom collections');
await this.app.db.getRepository<CollectionRepository>('collections').load({
filter: this.loadFilter,
});
};
// this.app.on('loadCollections', loadCollections);
this.app.on('beforeStart', loadCollections);
// this.app.on('beforeUpgrade', async () => {
// const syncOptions = {
// alter: {
// drop: false,
// },
// force: false,
// };
// await this.db.getCollection('collections').sync(syncOptions);
// await this.db.getCollection('fields').sync(syncOptions);
// await this.db.getCollection('collectionCategories').sync(syncOptions);
// await loadCollections();
// });
this.app.resourcer.use(async (ctx, next) => {
const { resourceName, actionName } = ctx.action;
if (resourceName === 'collections.fields' && actionName === 'update') {
const { updateAssociationValues = [] } = ctx.action.params;
updateAssociationValues.push('uiSchema');
ctx.action.mergeParams({
updateAssociationValues,
});
}
await next();
});
this.app.acl.allow('collections', 'list', 'loggedIn');
this.app.acl.allow('collectionCategories', 'list', 'loggedIn');
}
async load() {
await this.importCollections(path.resolve(__dirname, './collections'));
this.db.getRepository<CollectionRepository>('collections').setApp(this.app);
const errorHandlerPlugin = this.app.getPlugin<PluginErrorHandler>('error-handler');
errorHandlerPlugin.errorHandler.register(
(err) => {
return err instanceof UniqueConstraintError;
},
(err, ctx) => {
return ctx.throw(400, ctx.t(`The value of ${Object.keys(err.fields)} field duplicated`));
},
);
this.app.resourcer.use(async (ctx, next) => {
if (ctx.action.resourceName === 'collections.fields' && ['create', 'update'].includes(ctx.action.actionName)) {
ctx.action.mergeParams({
updateAssociationValues: ['reverseField'],
});
}
await next();
});
this.app.resource(viewResourcer);
this.app.resource(sqlResourcer);
this.app.actions(collectionActions);
const handleFieldSource = (fields) => {
for (const field of lodash.castArray(fields)) {
if (field.get('source')) {
const [collectionSource, fieldSource] = field.get('source').split('.');
// find original field
const collectionField = this.app.db.getCollection(collectionSource)?.getField(fieldSource);
if (!collectionField) {
continue;
}
const newOptions = {};
// write original field options
lodash.merge(newOptions, lodash.omit(collectionField.options, 'name'));
// merge with current field options
lodash.mergeWith(newOptions, field.get(), (objValue, srcValue) => {
if (srcValue === null) {
return objValue;
}
});
// set final options
field.set('options', newOptions);
}
}
};
this.app.resourcer.use(async (ctx, next) => {
await next();
// handle collections:list
if (
ctx.action.resourceName === 'collections' &&
ctx.action.actionName == 'list' &&
ctx.action.params?.paginate == 'false'
) {
for (const collection of ctx.body) {
if (collection.get('view')) {
const fields = collection.fields;
handleFieldSource(fields);
}
}
}
//handle collections:fields:list
if (ctx.action.resourceName == 'collections.fields' && ctx.action.actionName == 'list') {
handleFieldSource(ctx.action.params?.paginate == 'false' ? ctx.body : ctx.body.rows);
}
if (ctx.action.resourceName == 'collections.fields' && ctx.action.actionName == 'get') {
handleFieldSource(ctx.body);
}
});
this.app.db.extendCollection({
name: 'collectionCategory',
dumpRules: 'required',
origin: this.options.packageName,
});
}
}
export default CollectionManagerPlugin;