nocobase/packages/plugin-ui-schema-storage/src/server.ts

70 lines
1.8 KiB
TypeScript
Raw Normal View History

import { MagicAttributeModel } from '@nocobase/database';
import { Plugin } from '@nocobase/server';
import path from 'path';
import { uiSchemaActions } from './actions/ui-schema-action';
2022-02-11 10:13:14 +00:00
import { UiSchemaModel } from './model';
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-02-11 10:13:14 +00:00
export class UiSchemaStoragePlugin extends Plugin {
2022-02-08 12:58:57 +00:00
serverHooks: ServerHooks;
registerRepository() {
this.app.db.registerRepositories({
UiSchemaRepository,
});
}
async beforeLoad() {
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 });
this.registerRepository();
2022-02-08 08:36:48 +00:00
db.on('ui_schemas.beforeCreate', function setUid(model) {
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) {
const { transaction } = options;
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository;
2022-02-10 14:53:26 +00:00
const context = options.context;
if (context?.disableInsertHook) {
return;
}
await uiSchemaRepository.insert(model.toJSON(), {
transaction,
});
});
2022-02-08 08:36:48 +00:00
db.on('ui_schemas.afterUpdate', async function patchSchema(model, options) {
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,
});
}
async load() {
await this.db.import({
directory: path.resolve(__dirname, 'collections'),
});
}
}
2022-02-11 10:13:14 +00:00
export default UiSchemaStoragePlugin;