2022-04-13 22:50:02 +08:00
|
|
|
import { Collection } from '@nocobase/database';
|
2022-02-14 21:57:13 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-04-13 22:50:02 +08:00
|
|
|
import lodash from 'lodash';
|
2022-02-14 21:57:13 +08:00
|
|
|
import path from 'path';
|
2022-02-17 01:06:42 +08:00
|
|
|
import { CollectionRepository } from '.';
|
2022-02-14 21:57:13 +08:00
|
|
|
import {
|
|
|
|
afterCreateForReverseField,
|
|
|
|
beforeCreateForChildrenCollection,
|
|
|
|
beforeCreateForReverseField,
|
2022-06-10 17:46:46 +08:00
|
|
|
beforeInitOptions
|
2022-02-14 21:57:13 +08:00
|
|
|
} from './hooks';
|
2022-06-14 15:46:48 +08:00
|
|
|
import AlertSubTableMigration from './migrations/20220613103214-alert-sub-table';
|
2022-02-14 21:57:13 +08:00
|
|
|
import { CollectionModel, FieldModel } from './models';
|
|
|
|
|
|
|
|
export class CollectionManagerPlugin extends Plugin {
|
|
|
|
async beforeLoad() {
|
|
|
|
this.app.db.registerModels({
|
|
|
|
CollectionModel,
|
|
|
|
FieldModel,
|
|
|
|
});
|
|
|
|
|
2022-06-14 15:46:48 +08:00
|
|
|
this.db.addMigration({
|
|
|
|
name: 'collection-manager/20220613103214-alert-sub-table',
|
|
|
|
migration: AlertSubTableMigration,
|
|
|
|
});
|
|
|
|
|
2022-02-17 01:06:42 +08:00
|
|
|
this.app.db.registerRepositories({
|
|
|
|
CollectionRepository,
|
|
|
|
});
|
|
|
|
|
2022-03-11 10:08:58 +08: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')
|
|
|
|
) {
|
|
|
|
throw new Error('cant update field without a reverseField key');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-14 21:57:13 +08:00
|
|
|
// 要在 beforeInitOptions 之前处理
|
|
|
|
this.app.db.on('fields.beforeCreate', beforeCreateForReverseField(this.app.db));
|
|
|
|
this.app.db.on('fields.beforeCreate', beforeCreateForChildrenCollection(this.app.db));
|
|
|
|
this.app.db.on('fields.beforeCreate', async (model, options) => {
|
|
|
|
const type = model.get('type');
|
2022-03-04 23:17:28 +08:00
|
|
|
await this.app.db.emitAsync(`fields.${type}.beforeInitOptions`, model, {
|
|
|
|
...options,
|
|
|
|
database: this.app.db,
|
|
|
|
});
|
2022-02-14 21:57:13 +08:00
|
|
|
});
|
|
|
|
for (const key in beforeInitOptions) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(beforeInitOptions, key)) {
|
|
|
|
const fn = beforeInitOptions[key];
|
|
|
|
this.app.db.on(`fields.${key}.beforeInitOptions`, fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db));
|
|
|
|
|
|
|
|
this.app.db.on('collections.afterCreateWithAssociations', async (model, { context, transaction }) => {
|
|
|
|
if (context) {
|
|
|
|
await model.migrate({ transaction });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-22 08:50:58 +08:00
|
|
|
this.app.db.on('collections.afterDestroy', async (model, { transaction }) => {
|
|
|
|
const name = model.get('name');
|
|
|
|
|
|
|
|
const fields = await this.app.db.getRepository('fields').find({
|
|
|
|
filter: {
|
|
|
|
'type.$in': ['belongsToMany', 'belongsTo', 'hasMany', 'hasOne'],
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const deleteFieldsKey = fields
|
|
|
|
.filter((field) => (field.get('options') as any)?.target === name)
|
|
|
|
.map((field) => field.get('key') as string);
|
|
|
|
|
|
|
|
await this.app.db.getRepository('fields').destroy({
|
|
|
|
filter: {
|
|
|
|
'key.$in': deleteFieldsKey,
|
|
|
|
},
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-14 21:57:13 +08:00
|
|
|
this.app.db.on('fields.afterCreate', async (model, { context, transaction }) => {
|
|
|
|
if (context) {
|
|
|
|
await model.migrate({ transaction });
|
|
|
|
}
|
|
|
|
});
|
2022-02-17 01:06:42 +08:00
|
|
|
|
2022-06-10 17:46:46 +08:00
|
|
|
this.app.db.on('fields.afterCreateWithAssociations', async (model, { context, transaction }) => {
|
|
|
|
if (context) {
|
|
|
|
await model.load({ transaction });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-11 20:46:30 +08:00
|
|
|
// this.app.db.on('fields.afterCreateWithAssociations', async (model, { context, transaction }) => {
|
|
|
|
// return;
|
|
|
|
// if (!context) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// if (!model.get('through')) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// const [throughName, sourceName, targetName] = [
|
|
|
|
// model.get('through'),
|
|
|
|
// model.get('collectionName'),
|
|
|
|
// model.get('target'),
|
|
|
|
// ];
|
|
|
|
// const db = this.app.db;
|
|
|
|
// const through = await db.getRepository('collections').findOne({
|
|
|
|
// filter: {
|
|
|
|
// name: throughName,
|
|
|
|
// },
|
|
|
|
// transaction,
|
|
|
|
// });
|
|
|
|
// if (!through) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// const repository = db.getRepository('collections.fields', throughName);
|
|
|
|
// await repository.create({
|
|
|
|
// transaction,
|
|
|
|
// values: {
|
|
|
|
// name: `f_${uid()}`,
|
|
|
|
// type: 'belongsTo',
|
|
|
|
// target: sourceName,
|
|
|
|
// targetKey: model.get('sourceKey'),
|
|
|
|
// foreignKey: model.get('foreignKey'),
|
|
|
|
// interface: 'linkTo',
|
|
|
|
// reverseField: {
|
|
|
|
// interface: 'subTable',
|
|
|
|
// uiSchema: {
|
|
|
|
// type: 'void',
|
|
|
|
// title: through.get('title'),
|
|
|
|
// 'x-component': 'TableField',
|
|
|
|
// 'x-component-props': {},
|
|
|
|
// },
|
|
|
|
// // uiSchema: {
|
|
|
|
// // title: through.get('title'),
|
|
|
|
// // 'x-component': 'RecordPicker',
|
|
|
|
// // 'x-component-props': {
|
|
|
|
// // // mode: 'tags',
|
|
|
|
// // multiple: true,
|
|
|
|
// // fieldNames: {
|
|
|
|
// // label: 'id',
|
|
|
|
// // value: 'id',
|
|
|
|
// // },
|
|
|
|
// // },
|
|
|
|
// // },
|
|
|
|
// },
|
|
|
|
// uiSchema: {
|
|
|
|
// title: db.getCollection(sourceName)?.options?.title || sourceName,
|
|
|
|
// 'x-component': 'RecordPicker',
|
|
|
|
// 'x-component-props': {
|
|
|
|
// // mode: 'tags',
|
|
|
|
// multiple: false,
|
|
|
|
// fieldNames: {
|
|
|
|
// label: 'id',
|
|
|
|
// value: 'id',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
// await repository.create({
|
|
|
|
// transaction,
|
|
|
|
// values: {
|
|
|
|
// name: `f_${uid()}`,
|
|
|
|
// type: 'belongsTo',
|
|
|
|
// target: targetName,
|
|
|
|
// targetKey: model.get('targetKey'),
|
|
|
|
// foreignKey: model.get('otherKey'),
|
|
|
|
// interface: 'linkTo',
|
|
|
|
// reverseField: {
|
|
|
|
// interface: 'subTable',
|
|
|
|
// uiSchema: {
|
|
|
|
// type: 'void',
|
|
|
|
// title: through.get('title'),
|
|
|
|
// 'x-component': 'TableField',
|
|
|
|
// 'x-component-props': {},
|
|
|
|
// },
|
|
|
|
// // interface: 'linkTo',
|
|
|
|
// // uiSchema: {
|
|
|
|
// // title: through.get('title'),
|
|
|
|
// // 'x-component': 'RecordPicker',
|
|
|
|
// // 'x-component-props': {
|
|
|
|
// // // mode: 'tags',
|
|
|
|
// // multiple: true,
|
|
|
|
// // fieldNames: {
|
|
|
|
// // label: 'id',
|
|
|
|
// // value: 'id',
|
|
|
|
// // },
|
|
|
|
// // },
|
|
|
|
// // },
|
|
|
|
// },
|
|
|
|
// uiSchema: {
|
|
|
|
// title: db.getCollection(targetName)?.options?.title || targetName,
|
|
|
|
// 'x-component': 'RecordPicker',
|
|
|
|
// 'x-component-props': {
|
|
|
|
// // mode: 'tags',
|
|
|
|
// multiple: false,
|
|
|
|
// fieldNames: {
|
|
|
|
// label: 'id',
|
|
|
|
// value: 'id',
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
// await db.getRepository<CollectionRepository>('collections').load({
|
|
|
|
// filter: {
|
|
|
|
// 'name.$in': [throughName, sourceName, targetName],
|
|
|
|
// },
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
|
|
|
|
this.app.db.on('fields.afterDestroy', async (model, options) => {
|
|
|
|
await model.remove(options);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.db.on('collections.afterDestroy', async (model, options) => {
|
|
|
|
await model.remove(options);
|
2022-04-18 18:57:21 +08:00
|
|
|
});
|
|
|
|
|
2022-02-17 01:06:42 +08:00
|
|
|
this.app.on('beforeStart', async () => {
|
|
|
|
await this.app.db.getRepository<CollectionRepository>('collections').load();
|
|
|
|
});
|
2022-02-18 20:26:11 +08: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 10:10:57 +08:00
|
|
|
|
2022-04-13 22:50:02 +08:00
|
|
|
this.app.resourcer.use(async (ctx, next) => {
|
|
|
|
const { resourceName, actionName } = ctx.action;
|
|
|
|
if (actionName === 'update') {
|
|
|
|
const { updateAssociationValues = [] } = ctx.action.params;
|
|
|
|
const [collectionName, associationName] = resourceName.split('.');
|
|
|
|
if (!associationName) {
|
|
|
|
const collection: Collection = ctx.db.getCollection(collectionName);
|
|
|
|
if (collection) {
|
|
|
|
for (const [, field] of collection.fields) {
|
2022-06-14 15:46:48 +08:00
|
|
|
if (['subTable', 'o2m'].includes(field.options.interface)) {
|
2022-04-13 22:50:02 +08:00
|
|
|
updateAssociationValues.push(field.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const association = ctx.db.getCollection(collectionName)?.getField?.(associationName);
|
|
|
|
if (association?.target) {
|
|
|
|
const collection: Collection = ctx.db.getCollection(association?.target);
|
|
|
|
if (collection) {
|
|
|
|
for (const [, field] of collection.fields) {
|
2022-06-14 15:46:48 +08:00
|
|
|
if (['subTable', 'o2m'].includes(field.options.interface)) {
|
2022-04-13 22:50:02 +08:00
|
|
|
updateAssociationValues.push(field.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updateAssociationValues.length) {
|
|
|
|
ctx.action.mergeParams({
|
|
|
|
updateAssociationValues,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
2022-04-24 10:14:46 +08:00
|
|
|
this.app.acl.allow('collections', 'list', 'loggedIn');
|
|
|
|
this.app.acl.allow('collections', ['create', 'update', 'destroy'], 'allowConfigure');
|
2022-02-14 21:57:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.app.db.import({
|
|
|
|
directory: path.resolve(__dirname, './collections'),
|
|
|
|
});
|
|
|
|
}
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
getName(): string {
|
|
|
|
return this.getPackageName(__dirname);
|
|
|
|
}
|
2022-02-14 21:57:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default CollectionManagerPlugin;
|