tachybase_todo/packages/plugin-ui-schema-storage/src/server.ts
ChengLei Shao 000587380b
fix: postgres sort with appends issue (#198)
* fix: postgres sort with appends issue

* fix: role menus api error

* feat: add exists params in roles.collections api

* refactor: remove uid field on uiSchemas table

* test: toggle role menus

* fix: plugin-acl test

* feat: sync data to acl before app start

* fix: mysql ui-schema sql compatibility

* feat: writeRolesToACL in plugin-acl
2022-02-20 01:23:04 +08:00

73 lines
1.9 KiB
TypeScript

import { MagicAttributeModel } from '@nocobase/database';
import { Plugin } from '@nocobase/server';
import { uid } from '@nocobase/utils';
import path from 'path';
import { uiSchemaActions } from './actions/ui-schema-action';
import { UiSchemaModel } from './model';
import UiSchemaRepository from './repository';
import { ServerHooks } from './server-hooks';
import { ServerHookModel } from './server-hooks/model';
export class UiSchemaStoragePlugin extends Plugin {
serverHooks: ServerHooks;
registerRepository() {
this.app.db.registerRepositories({
UiSchemaRepository,
});
}
async beforeLoad() {
const db = this.app.db;
this.serverHooks = new ServerHooks(db);
this.app.db.registerModels({ MagicAttributeModel, UiSchemaModel, ServerHookModel });
this.registerRepository();
db.on('uiSchemas.beforeCreate', function setUid(model) {
if (!model.get('name')) {
model.set('name', uid());
}
});
db.on('uiSchemas.afterCreate', async function insertSchema(model, options) {
const { transaction } = options;
const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
const context = options.context;
if (context?.disableInsertHook) {
return;
}
await uiSchemaRepository.insert(model.toJSON(), {
transaction,
});
});
db.on('uiSchemas.afterUpdate', async function patchSchema(model, options) {
const { transaction } = options;
const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
await uiSchemaRepository.patch(model.toJSON(), {
transaction,
});
});
this.app.resourcer.define({
name: 'uiSchemas',
actions: uiSchemaActions,
});
}
async load() {
await this.db.import({
directory: path.resolve(__dirname, 'collections'),
});
}
}
export default UiSchemaStoragePlugin;