2022-02-14 13:57:13 +00:00
|
|
|
import path from 'path';
|
2022-08-20 15:23:13 +00:00
|
|
|
import { UniqueConstraintError } from 'sequelize';
|
|
|
|
|
|
|
|
import PluginErrorHandler from '@nocobase/plugin-error-handler';
|
|
|
|
import { Plugin } from '@nocobase/server';
|
2023-02-13 13:38:47 +00:00
|
|
|
import { Mutex } from 'async-mutex';
|
2022-08-20 15:23:13 +00:00
|
|
|
|
2022-02-16 17:06:42 +00:00
|
|
|
import { CollectionRepository } from '.';
|
2022-02-14 13:57:13 +00:00
|
|
|
import {
|
2022-10-28 07:09:14 +00:00
|
|
|
afterCreateForForeignKeyField,
|
2022-02-14 13:57:13 +00:00
|
|
|
afterCreateForReverseField,
|
|
|
|
beforeCreateForReverseField,
|
2022-10-28 07:09:14 +00:00
|
|
|
beforeDestroyForeignKey,
|
2023-02-18 01:27:54 +00:00
|
|
|
beforeInitOptions,
|
2022-02-14 13:57:13 +00:00
|
|
|
} from './hooks';
|
2022-10-31 14:45:39 +00:00
|
|
|
|
2022-11-16 04:53:58 +00:00
|
|
|
import { InheritedCollection } from '@nocobase/database';
|
2022-12-31 02:54:20 +00:00
|
|
|
import { CollectionModel, FieldModel } from './models';
|
2023-03-01 09:55:37 +00:00
|
|
|
import * as process from 'process';
|
|
|
|
import lodash from 'lodash';
|
2022-02-14 13:57:13 +00:00
|
|
|
|
|
|
|
export class CollectionManagerPlugin extends Plugin {
|
2023-03-01 09:55:37 +00:00
|
|
|
public schema: string;
|
|
|
|
|
2022-02-14 13:57:13 +00:00
|
|
|
async beforeLoad() {
|
2023-03-01 09:55:37 +00:00
|
|
|
if (process.env.COLLECTION_MANAGER_SCHEMA) {
|
|
|
|
this.schema = process.env.COLLECTION_MANAGER_SCHEMA;
|
|
|
|
}
|
|
|
|
|
2022-02-14 13:57:13 +00:00
|
|
|
this.app.db.registerModels({
|
|
|
|
CollectionModel,
|
|
|
|
FieldModel,
|
|
|
|
});
|
|
|
|
|
2022-06-17 02:25:59 +00:00
|
|
|
this.db.addMigrations({
|
|
|
|
namespace: 'collection-manager',
|
|
|
|
directory: path.resolve(__dirname, './migrations'),
|
|
|
|
context: {
|
|
|
|
plugin: this,
|
|
|
|
},
|
2022-06-14 07:46:48 +00:00
|
|
|
});
|
|
|
|
|
2022-02-16 17:06:42 +00:00
|
|
|
this.app.db.registerRepositories({
|
|
|
|
CollectionRepository,
|
|
|
|
});
|
|
|
|
|
2023-01-08 23:35:48 +00:00
|
|
|
this.app.acl.registerSnippet({
|
|
|
|
name: `pm.${this.name}.collections`,
|
|
|
|
actions: [
|
|
|
|
'collections:*',
|
|
|
|
// 'fields:*',
|
|
|
|
'collections.fields:*',
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2023-03-01 09:55:37 +00:00
|
|
|
this.app.db.on('collections.beforeCreate', async (model) => {
|
|
|
|
if (this.app.db.inDialect('postgres') && this.schema && model.get('from') != 'db2cm') {
|
|
|
|
model.set('schema', this.schema);
|
2022-03-11 02:08:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-01 09:55:37 +00:00
|
|
|
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) => {
|
|
|
|
await model.remove(options);
|
|
|
|
});
|
|
|
|
|
2022-02-14 13:57:13 +00:00
|
|
|
// 要在 beforeInitOptions 之前处理
|
|
|
|
this.app.db.on('fields.beforeCreate', beforeCreateForReverseField(this.app.db));
|
2022-11-16 04:53:58 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-14 13:57:13 +00:00
|
|
|
this.app.db.on('fields.beforeCreate', async (model, options) => {
|
|
|
|
const type = model.get('type');
|
2022-10-14 09:22:32 +00:00
|
|
|
const fn = beforeInitOptions[type];
|
|
|
|
if (fn) {
|
|
|
|
await fn(model, { database: this.app.db });
|
2022-02-14 13:57:13 +00:00
|
|
|
}
|
2022-10-14 09:22:32 +00:00
|
|
|
});
|
2022-10-27 05:00:16 +00:00
|
|
|
|
2022-02-14 13:57:13 +00:00
|
|
|
this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db));
|
|
|
|
|
2022-08-20 15:23:13 +00:00
|
|
|
this.app.db.on('fields.afterCreate', async (model: FieldModel, { context, transaction }) => {
|
2022-02-14 13:57:13 +00:00
|
|
|
if (context) {
|
2022-08-24 03:51:14 +00:00
|
|
|
await model.migrate({
|
|
|
|
isNew: true,
|
2022-10-10 07:34:00 +00:00
|
|
|
transaction,
|
2022-08-24 03:51:14 +00:00
|
|
|
});
|
2022-02-14 13:57:13 +00:00
|
|
|
}
|
|
|
|
});
|
2022-11-04 01:04:53 +00:00
|
|
|
|
2022-10-28 07:09:14 +00:00
|
|
|
// after migrate
|
|
|
|
this.app.db.on('fields.afterCreate', afterCreateForForeignKeyField(this.app.db));
|
2022-02-16 17:06:42 +00:00
|
|
|
|
2023-03-01 09:55:37 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-20 15:23:13 +00:00
|
|
|
this.app.db.on('fields.afterUpdate', async (model: FieldModel, { context, transaction }) => {
|
2022-10-14 01:56:16 +00:00
|
|
|
const prevOptions = model.previous('options');
|
|
|
|
const currentOptions = model.get('options');
|
|
|
|
|
2022-08-03 01:32:16 +00:00
|
|
|
if (context) {
|
2022-10-14 01:56:16 +00:00
|
|
|
const prev = prevOptions['unique'];
|
|
|
|
const next = currentOptions['unique'];
|
|
|
|
|
2022-08-23 00:59:36 +00:00
|
|
|
if (Boolean(prev) !== Boolean(next)) {
|
2023-02-13 13:38:47 +00:00
|
|
|
await model.syncUniqueIndex({ transaction });
|
2022-08-20 15:23:13 +00:00
|
|
|
}
|
2022-08-03 01:32:16 +00:00
|
|
|
}
|
2022-10-14 01:56:16 +00:00
|
|
|
|
|
|
|
const prevDefaultValue = prevOptions['defaultValue'];
|
|
|
|
const currentDefaultValue = currentOptions['defaultValue'];
|
|
|
|
|
|
|
|
if (prevDefaultValue != currentDefaultValue) {
|
|
|
|
await model.syncDefaultValue({ transaction, defaultValue: currentDefaultValue });
|
|
|
|
}
|
2022-11-10 13:28:33 +00:00
|
|
|
|
|
|
|
const prevOnDelete = prevOptions['onDelete'];
|
|
|
|
const currentOnDelete = currentOptions['onDelete'];
|
|
|
|
|
|
|
|
if (prevOnDelete != currentOnDelete) {
|
|
|
|
await model.syncReferenceCheckOption({ transaction });
|
|
|
|
}
|
2022-08-03 01:32:16 +00:00
|
|
|
});
|
|
|
|
|
2022-11-04 01:04:53 +00:00
|
|
|
this.app.db.on('fields.afterSaveWithAssociations', async (model: FieldModel, { context, transaction }) => {
|
2022-06-10 09:46:46 +00:00
|
|
|
if (context) {
|
|
|
|
await model.load({ transaction });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-28 07:09:14 +00:00
|
|
|
// before field remove
|
|
|
|
this.app.db.on('fields.beforeDestroy', beforeDestroyForeignKey(this.app.db));
|
2023-02-13 13:38:47 +00:00
|
|
|
|
|
|
|
const mutex = new Mutex();
|
2022-11-16 04:53:58 +00:00
|
|
|
this.app.db.on('fields.beforeDestroy', async (model: FieldModel, options) => {
|
2023-02-13 13:38:47 +00:00
|
|
|
await mutex.runExclusive(async () => {
|
|
|
|
await model.remove(options);
|
|
|
|
});
|
2022-06-11 12:46:30 +00:00
|
|
|
});
|
2022-04-18 10:57:21 +00:00
|
|
|
|
2022-11-16 04:53:58 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
this.app.on('afterLoad', async (app, options) => {
|
|
|
|
if (options?.method === 'install') {
|
|
|
|
return;
|
2022-07-09 16:41:36 +00:00
|
|
|
}
|
|
|
|
const exists = await this.app.db.collectionExistsInDb('collections');
|
|
|
|
if (exists) {
|
2022-11-04 07:38:08 +00:00
|
|
|
try {
|
|
|
|
await this.app.db.getRepository<CollectionRepository>('collections').load();
|
|
|
|
} catch (error) {
|
2022-11-26 01:24:53 +00:00
|
|
|
this.app.logger.warn(error);
|
2022-11-04 07:38:08 +00:00
|
|
|
await this.app.db.sync();
|
2022-11-26 01:24:53 +00:00
|
|
|
|
2022-11-04 07:38:08 +00:00
|
|
|
try {
|
|
|
|
await this.app.db.getRepository<CollectionRepository>('collections').load();
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2022-07-09 16:41:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-18 12:26:11 +00:00
|
|
|
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();
|
|
|
|
});
|
2022-03-11 02:10:57 +00:00
|
|
|
|
2022-04-24 02:14:46 +00:00
|
|
|
this.app.acl.allow('collections', 'list', 'loggedIn');
|
2022-02-14 13:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2023-01-08 04:45:02 +00:00
|
|
|
await this.importCollections(path.resolve(__dirname, './collections'));
|
2022-08-20 15:23:13 +00:00
|
|
|
|
2022-10-27 05:00:16 +00:00
|
|
|
const errorHandlerPlugin = <PluginErrorHandler>this.app.getPlugin('error-handler');
|
2022-08-20 15:23:13 +00:00
|
|
|
errorHandlerPlugin.errorHandler.register(
|
2022-08-23 00:59:36 +00:00
|
|
|
(err) => {
|
|
|
|
return err instanceof UniqueConstraintError;
|
|
|
|
},
|
2022-08-20 15:23:13 +00:00
|
|
|
(err, ctx) => {
|
|
|
|
return ctx.throw(400, ctx.t(`The value of ${Object.keys(err.fields)} field duplicated`));
|
|
|
|
},
|
|
|
|
);
|
2022-11-15 12:37:26 +00:00
|
|
|
|
|
|
|
this.app.resourcer.use(async (ctx, next) => {
|
|
|
|
if (ctx.action.resourceName === 'collections.fields' && ['create', 'update'].includes(ctx.action.actionName)) {
|
|
|
|
ctx.action.mergeParams({
|
|
|
|
updateAssociationValues: ['uiSchema', 'reverseField'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
});
|
2023-02-13 01:57:03 +00:00
|
|
|
|
|
|
|
this.app.db.extendCollection({
|
|
|
|
name: 'collectionCategory',
|
|
|
|
namespace: 'collection-manager',
|
|
|
|
duplicator: 'required',
|
|
|
|
});
|
2022-02-14 13:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CollectionManagerPlugin;
|