2022-01-19 02:09:30 +00:00
|
|
|
import { MagicAttributeModel } from '@nocobase/database';
|
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-02-13 03:23:40 +00:00
|
|
|
import { uid } from '@nocobase/utils';
|
2022-01-19 02:09:30 +00:00
|
|
|
import path from 'path';
|
|
|
|
import { uiSchemaActions } from './actions/ui-schema-action';
|
2022-02-11 10:13:14 +00:00
|
|
|
import { UiSchemaModel } from './model';
|
2022-01-19 02:09:30 +00:00
|
|
|
import UiSchemaRepository from './repository';
|
2022-02-08 12:58:57 +00:00
|
|
|
import { ServerHooks } from './server-hooks';
|
2022-02-09 12:24:25 +00:00
|
|
|
import { ServerHookModel } from './server-hooks/model';
|
2022-01-19 02:09:30 +00:00
|
|
|
|
2022-02-11 10:13:14 +00:00
|
|
|
export class UiSchemaStoragePlugin extends Plugin {
|
2022-02-08 12:58:57 +00:00
|
|
|
serverHooks: ServerHooks;
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
registerRepository() {
|
|
|
|
this.app.db.registerRepositories({
|
|
|
|
UiSchemaRepository,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-06 17:14:00 +00:00
|
|
|
async beforeLoad() {
|
2022-01-19 02:09:30 +00:00
|
|
|
const db = this.app.db;
|
|
|
|
|
2022-02-08 12:58:57 +00:00
|
|
|
this.serverHooks = new ServerHooks(db);
|
|
|
|
|
2022-02-09 12:24:25 +00:00
|
|
|
this.app.db.registerModels({ MagicAttributeModel, UiSchemaModel, ServerHookModel });
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
this.registerRepository();
|
|
|
|
|
2022-02-15 16:22:47 +00:00
|
|
|
db.on('uiSchemas.beforeCreate', function setUid(model) {
|
2022-02-13 03:23:40 +00:00
|
|
|
if (!model.get('name')) {
|
|
|
|
model.set('name', uid());
|
|
|
|
}
|
2022-01-19 02:09:30 +00:00
|
|
|
});
|
|
|
|
|
2022-02-15 16:22:47 +00:00
|
|
|
db.on('uiSchemas.afterCreate', async function insertSchema(model, options) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const { transaction } = options;
|
2022-02-15 16:22:47 +00:00
|
|
|
const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
|
2022-01-19 02:09:30 +00:00
|
|
|
|
2022-02-10 14:53:26 +00:00
|
|
|
const context = options.context;
|
|
|
|
|
|
|
|
if (context?.disableInsertHook) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-19 02:09:30 +00:00
|
|
|
await uiSchemaRepository.insert(model.toJSON(), {
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-15 16:22:47 +00:00
|
|
|
db.on('uiSchemas.afterUpdate', async function patchSchema(model, options) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const { transaction } = options;
|
2022-02-15 16:22:47 +00:00
|
|
|
const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
|
2022-01-19 02:09:30 +00:00
|
|
|
|
|
|
|
await uiSchemaRepository.patch(model.toJSON(), {
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.resourcer.define({
|
2022-02-15 16:22:47 +00:00
|
|
|
name: 'uiSchemas',
|
2022-01-19 02:09:30 +00:00
|
|
|
actions: uiSchemaActions,
|
|
|
|
});
|
2022-03-11 02:10:57 +00:00
|
|
|
|
2022-04-24 02:14:46 +00:00
|
|
|
this.app.acl.allow('uiSchemas', '*', 'loggedIn');
|
|
|
|
this.app.acl.allow('uiSchemaTemplates', '*', 'loggedIn');
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
2022-02-06 17:14:00 +00:00
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.db.import({
|
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
|
|
|
});
|
|
|
|
}
|
2022-03-28 14:01:10 +00:00
|
|
|
|
|
|
|
getName(): string {
|
|
|
|
return this.getPackageName(__dirname);
|
|
|
|
}
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|
2022-02-11 10:13:14 +00:00
|
|
|
|
|
|
|
export default UiSchemaStoragePlugin;
|