2022-02-14 13:57:13 +00:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-04-13 14:50:02 +00:00
|
|
|
import lodash from 'lodash';
|
2022-02-14 13:57:13 +00:00
|
|
|
import path from 'path';
|
2022-02-16 17:06:42 +00:00
|
|
|
import { CollectionRepository } from '.';
|
2022-02-14 13:57:13 +00:00
|
|
|
import {
|
|
|
|
afterCreateForReverseField,
|
|
|
|
beforeCreateForChildrenCollection,
|
|
|
|
beforeCreateForReverseField,
|
2022-06-10 09:46:46 +00:00
|
|
|
beforeInitOptions
|
2022-02-14 13:57:13 +00:00
|
|
|
} from './hooks';
|
|
|
|
import { CollectionModel, FieldModel } from './models';
|
|
|
|
|
|
|
|
export class CollectionManagerPlugin extends Plugin {
|
|
|
|
async beforeLoad() {
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2022-03-11 02:08:58 +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')
|
|
|
|
) {
|
|
|
|
throw new Error('cant update field without a reverseField key');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-14 13:57:13 +00: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 15:17:28 +00:00
|
|
|
await this.app.db.emitAsync(`fields.${type}.beforeInitOptions`, model, {
|
|
|
|
...options,
|
|
|
|
database: this.app.db,
|
|
|
|
});
|
2022-02-14 13:57:13 +00: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 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.db.on('fields.afterCreate', async (model, { context, transaction }) => {
|
|
|
|
if (context) {
|
|
|
|
await model.migrate({ transaction });
|
|
|
|
}
|
|
|
|
});
|
2022-02-16 17:06:42 +00:00
|
|
|
|
2022-06-10 09:46:46 +00:00
|
|
|
this.app.db.on('fields.afterCreateWithAssociations', async (model, { context, transaction }) => {
|
|
|
|
if (context) {
|
|
|
|
await model.load({ transaction });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-25 02:36:56 +00:00
|
|
|
this.app.db.on('fields.beforeDestroy', async (model, options) => {
|
2022-06-11 12:46:30 +00:00
|
|
|
await model.remove(options);
|
|
|
|
});
|
|
|
|
|
2022-06-25 02:36:56 +00:00
|
|
|
this.app.db.on('collections.beforeDestroy', async (model, options) => {
|
2022-06-11 12:46:30 +00:00
|
|
|
await model.remove(options);
|
2022-04-18 10:57:21 +00:00
|
|
|
});
|
|
|
|
|
2022-02-16 17:06:42 +00:00
|
|
|
this.app.on('beforeStart', async () => {
|
|
|
|
await this.app.db.getRepository<CollectionRepository>('collections').load();
|
|
|
|
});
|
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-06-29 04:57:43 +00: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) {
|
|
|
|
// if (['subTable', 'o2m'].includes(field.options.interface)) {
|
|
|
|
// 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) {
|
|
|
|
// if (['subTable', 'o2m'].includes(field.options.interface)) {
|
|
|
|
// updateAssociationValues.push(field.name);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (updateAssociationValues.length) {
|
|
|
|
// ctx.action.mergeParams({
|
|
|
|
// updateAssociationValues,
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// await next();
|
|
|
|
// });
|
2022-04-13 14:50:02 +00:00
|
|
|
|
2022-04-24 02:14:46 +00:00
|
|
|
this.app.acl.allow('collections', 'list', 'loggedIn');
|
|
|
|
this.app.acl.allow('collections', ['create', 'update', 'destroy'], 'allowConfigure');
|
2022-02-14 13:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.app.db.import({
|
|
|
|
directory: path.resolve(__dirname, './collections'),
|
|
|
|
});
|
|
|
|
}
|
2022-03-28 14:01:10 +00:00
|
|
|
|
|
|
|
getName(): string {
|
|
|
|
return this.getPackageName(__dirname);
|
|
|
|
}
|
2022-02-14 13:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default CollectionManagerPlugin;
|