2022-01-19 02:09:30 +00:00
|
|
|
import { MagicAttributeModel } from '@nocobase/database';
|
|
|
|
import { Plugin } from '@nocobase/server';
|
|
|
|
import path from 'path';
|
|
|
|
import { uiSchemaActions } from './actions/ui-schema-action';
|
|
|
|
import UiSchemaRepository from './repository';
|
|
|
|
|
|
|
|
export default class PluginUiSchema extends Plugin {
|
|
|
|
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;
|
|
|
|
|
|
|
|
this.app.db.registerModels({ MagicAttributeModel });
|
|
|
|
|
|
|
|
this.registerRepository();
|
|
|
|
|
2022-02-08 08:36:48 +00:00
|
|
|
db.on('ui_schemas.beforeCreate', function setUid(model) {
|
2022-01-19 02:09:30 +00:00
|
|
|
model.set('uid', model.get('x-uid'));
|
|
|
|
});
|
|
|
|
|
2022-02-08 08:36:48 +00:00
|
|
|
db.on('ui_schemas.afterCreate', async function insertSchema(model, options) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const { transaction } = options;
|
|
|
|
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository;
|
|
|
|
|
|
|
|
await uiSchemaRepository.insert(model.toJSON(), {
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-08 08:36:48 +00:00
|
|
|
db.on('ui_schemas.afterUpdate', async function patchSchema(model, options) {
|
2022-01-19 02:09:30 +00:00
|
|
|
const { transaction } = options;
|
|
|
|
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository;
|
|
|
|
|
|
|
|
await uiSchemaRepository.patch(model.toJSON(), {
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.app.resourcer.define({
|
|
|
|
name: 'ui_schemas',
|
|
|
|
actions: uiSchemaActions,
|
|
|
|
});
|
|
|
|
}
|
2022-02-06 17:14:00 +00:00
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.db.import({
|
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
|
|
|
});
|
|
|
|
}
|
2022-01-19 02:09:30 +00:00
|
|
|
}
|