feat: uiSchema with attributes
This commit is contained in:
parent
95f4406ba9
commit
af02e80b6f
@ -0,0 +1,77 @@
|
||||
import { mockServer, MockServer } from '@nocobase/test';
|
||||
import { Database } from '@nocobase/database';
|
||||
import PluginUiSchema, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
|
||||
|
||||
describe('server hooks', () => {
|
||||
let app: MockServer;
|
||||
let db: Database;
|
||||
let uiSchemaRepository: UiSchemaRepository;
|
||||
|
||||
afterEach(async () => {
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
app = mockServer({
|
||||
registerActions: true,
|
||||
});
|
||||
|
||||
db = app.db;
|
||||
|
||||
await app.cleanDb();
|
||||
app.plugin(PluginUiSchema);
|
||||
|
||||
await app.loadAndInstall();
|
||||
|
||||
uiSchemaRepository = db.getRepository('ui_schemas');
|
||||
});
|
||||
|
||||
it('should save uiSchemaAttrs', async () => {
|
||||
const schema = {
|
||||
name: 'row',
|
||||
'x-uid': 'table',
|
||||
'x-component': 'Table',
|
||||
'x-collection': 'posts',
|
||||
'x-server-hooks': {
|
||||
afterDestroyCollection: ['aaa'],
|
||||
},
|
||||
properties: {
|
||||
col1: {
|
||||
'x-uid': 'col1',
|
||||
'x-component': 'Col',
|
||||
properties: {
|
||||
field1: {
|
||||
'x-uid': 'field1',
|
||||
'x-component': 'Input',
|
||||
'x-collection-field': 'posts.title',
|
||||
'x-server-hooks': {
|
||||
afterDestroyField: ['bbb'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await uiSchemaRepository.insert(schema);
|
||||
const node = await uiSchemaRepository.findOne({
|
||||
filter: {
|
||||
uid: 'table',
|
||||
},
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
const nodeAttr = await node.getAttrs();
|
||||
expect(nodeAttr.get('collectionPath')).toEqual('posts');
|
||||
|
||||
const nodeField = await uiSchemaRepository.findOne({
|
||||
filter: {
|
||||
uid: 'field1',
|
||||
},
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
const nodeFieldAttr = await nodeField.getAttrs();
|
||||
expect(nodeFieldAttr.get('collectionPath')).toEqual('posts.title');
|
||||
});
|
||||
});
|
@ -0,0 +1,13 @@
|
||||
import { CollectionOptions } from '@nocobase/database';
|
||||
|
||||
export default {
|
||||
name: 'uiSchemaAttrs',
|
||||
autoGenId: false,
|
||||
timestamps: false,
|
||||
fields: [
|
||||
{
|
||||
type: 'string',
|
||||
name: 'collectionPath',
|
||||
},
|
||||
],
|
||||
} as CollectionOptions;
|
@ -25,6 +25,12 @@ export default {
|
||||
type: 'string',
|
||||
name: 'name',
|
||||
},
|
||||
{
|
||||
type: 'hasOne',
|
||||
name: 'attrs',
|
||||
target: 'uiSchemaAttrs',
|
||||
foreignKey: 'uid',
|
||||
},
|
||||
{
|
||||
type: 'json',
|
||||
name: 'schema',
|
||||
|
@ -385,6 +385,34 @@ export default class UiSchemaRepository extends Repository {
|
||||
});
|
||||
}
|
||||
|
||||
private async insertSchemaRecord(name, uid, schema, transaction) {
|
||||
const node = await this.create({
|
||||
values: {
|
||||
name,
|
||||
uid,
|
||||
schema,
|
||||
},
|
||||
transaction,
|
||||
hooks: false,
|
||||
});
|
||||
|
||||
await this.insertSchemaAttrs(node, transaction);
|
||||
return node;
|
||||
}
|
||||
|
||||
private async insertSchemaAttrs(schema, transaction) {
|
||||
const collectionPath = schema.get('x-collection') || schema.get('x-collection-field');
|
||||
|
||||
if (collectionPath) {
|
||||
await this.database.getRepository('uiSchemaAttrs').create({
|
||||
values: {
|
||||
uid: schema.get('uid'),
|
||||
collectionPath: collectionPath,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async insertSingleNode(schema: SchemaNode, transaction: Transaction) {
|
||||
const db = this.database;
|
||||
const treeCollection = db.getCollection('ui_schema_tree_path');
|
||||
@ -414,15 +442,7 @@ export default class UiSchemaRepository extends Repository {
|
||||
if (existsNode) {
|
||||
savedNode = existsNode;
|
||||
} else {
|
||||
savedNode = await this.create({
|
||||
values: {
|
||||
name,
|
||||
uid,
|
||||
schema,
|
||||
},
|
||||
transaction,
|
||||
hooks: false,
|
||||
});
|
||||
savedNode = await this.insertSchemaRecord(name, uid, schema, transaction);
|
||||
}
|
||||
|
||||
if (childOptions) {
|
||||
|
@ -18,11 +18,11 @@ export default class PluginUiSchema extends Plugin {
|
||||
|
||||
this.registerRepository();
|
||||
|
||||
db.on('ui_schemas.beforeCreate', (model) => {
|
||||
db.on('ui_schemas.beforeCreate', function setUid(model) {
|
||||
model.set('uid', model.get('x-uid'));
|
||||
});
|
||||
|
||||
db.on('ui_schemas.afterCreate', async (model, options) => {
|
||||
db.on('ui_schemas.afterCreate', async function insertSchema(model, options) {
|
||||
const { transaction } = options;
|
||||
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository;
|
||||
|
||||
@ -31,7 +31,7 @@ export default class PluginUiSchema extends Plugin {
|
||||
});
|
||||
});
|
||||
|
||||
db.on('ui_schemas.afterUpdate', async (model, options) => {
|
||||
db.on('ui_schemas.afterUpdate', async function patchSchema(model, options) {
|
||||
const { transaction } = options;
|
||||
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user