feat: rename ui_schemas to uiSchemas

This commit is contained in:
chenos 2022-02-16 00:22:47 +08:00
parent 99bfd75776
commit 0440591868
22 changed files with 94 additions and 71 deletions

View File

@ -13,6 +13,11 @@ if (yargs.argv.dbDialect) {
process.env.DB_DIALECT = yargs.argv.dbDialect; process.env.DB_DIALECT = yargs.argv.dbDialect;
} }
if (yargs.argv.dbPort) {
process.env.DB_PORT = yargs.argv.dbPort;
}
console.log('DB_DIALECT:', process.env.DB_DIALECT); console.log('DB_DIALECT:', process.env.DB_DIALECT);
console.log('DB_PORT:', process.env.DB_PORT);
require('jest-cli/bin/jest'); require('jest-cli/bin/jest');

View File

@ -13,7 +13,7 @@
"start-server": "ts-node-dev -r dotenv/config -r tsconfig-paths/register ./packages/api/src/index.ts", "start-server": "ts-node-dev -r dotenv/config -r tsconfig-paths/register ./packages/api/src/index.ts",
"build": "lerna run build", "build": "lerna run build",
"build-docs": "dumi build", "build-docs": "dumi build",
"test": "node ./jest.cli.js", "test": "node ./jest.cli.js -i",
"lint": "eslint ." "lint": "eslint ."
}, },
"resolutions": { "resolutions": {

View File

@ -108,7 +108,7 @@ export default (apiClient: APIClient) => {
}, },
}; };
mock.onGet(/\/ui_schemas\:getJsonSchema\/(\w+)/).reply(function (config) { mock.onGet(/\/uiSchemas\:getJsonSchema\/(\w+)/).reply(function (config) {
const name = config?.url?.split('/')?.pop(); const name = config?.url?.split('/')?.pop();
if (name && jsonSchema[name]) { if (name && jsonSchema[name]) {

View File

@ -108,7 +108,7 @@ export default (apiClient: APIClient) => {
}, },
}; };
mock.onGet(/\/ui_schemas\:getJsonSchema\/(\w+)/).reply(function (config) { mock.onGet(/\/uiSchemas\:getJsonSchema\/(\w+)/).reply(function (config) {
const name = config.url.split('/').pop(); const name = config.url.split('/').pop();
console.log(name); console.log(name);
if (jsonSchema[name]) { if (jsonSchema[name]) {
@ -135,7 +135,7 @@ export default (apiClient: APIClient) => {
return [200, response]; return [200, response];
}); });
mock.onGet(/\/ui_schemas\:getProperties\/(\w+)/).reply(function (config) { mock.onGet(/\/uiSchemas\:getProperties\/(\w+)/).reply(function (config) {
// const name = config.url.split('/').pop(); // const name = config.url.split('/').pop();
// console.log(name); // console.log(name);
// if (jsonSchema[name]) { // if (jsonSchema[name]) {

View File

@ -21,7 +21,7 @@ const RequestSchemaComponent: React.FC<RemoteSchemaComponentProps> = (props) =>
const { onlyRenderProperties, hidden, scope, uid, onSuccess, schemaTransform = defaultTransform } = props; const { onlyRenderProperties, hidden, scope, uid, onSuccess, schemaTransform = defaultTransform } = props;
const { reset } = useSchemaComponentContext(); const { reset } = useSchemaComponentContext();
const conf = { const conf = {
url: `/ui_schemas:${onlyRenderProperties ? 'getProperties' : 'getJsonSchema'}/${uid}`, url: `/uiSchemas:${onlyRenderProperties ? 'getProperties' : 'getJsonSchema'}/${uid}`,
}; };
const { data, loading } = useRequest(conf, { const { data, loading } = useRequest(conf, {
refreshDeps: [uid], refreshDeps: [uid],

View File

@ -82,13 +82,13 @@ export class Designable {
this.on('insertAdjacent', async ({ current, position, schema, removed }) => { this.on('insertAdjacent', async ({ current, position, schema, removed }) => {
refresh(); refresh();
await api.request({ await api.request({
url: `/ui_schemas:insertAdjacent/${current['x-uid']}?position=${position}`, url: `/uiSchemas:insertAdjacent/${current['x-uid']}?position=${position}`,
method: 'post', method: 'post',
data: schema.toJSON(), data: schema.toJSON(),
}); });
if (removed?.['x-uid']) { if (removed?.['x-uid']) {
await api.request({ await api.request({
url: `/ui_schemas:remove/${removed['x-uid']}`, url: `/uiSchemas:remove/${removed['x-uid']}`,
method: 'post', method: 'post',
}); });
} }
@ -97,7 +97,7 @@ export class Designable {
refresh(); refresh();
if (removed?.['x-uid']) { if (removed?.['x-uid']) {
await api.request({ await api.request({
url: `/ui_schemas:remove/${removed['x-uid']}`, url: `/uiSchemas:remove/${removed['x-uid']}`,
method: 'post', method: 'post',
}); });
} }

View File

@ -42,7 +42,7 @@ export default {
{ {
type: 'belongsToMany', type: 'belongsToMany',
name: 'menuUiSchemas', name: 'menuUiSchemas',
target: 'ui_schemas', target: 'uiSchemas',
}, },
{ {
type: 'hasMany', type: 'hasMany',

View File

@ -53,7 +53,7 @@ export default {
{ {
type: 'belongsTo', type: 'belongsTo',
name: 'uiSchema', name: 'uiSchema',
target: 'ui_schemas', target: 'uiSchemas',
foreignKey: 'uiSchemaUid' foreignKey: 'uiSchemaUid'
}, },
{ {

View File

@ -30,7 +30,7 @@ export default defineCollection({
{ {
type: 'belongsTo', type: 'belongsTo',
name: 'uiSchema', name: 'uiSchema',
target: 'ui_schemas', target: 'uiSchemas',
foreignKey: 'uiSchemaUid' foreignKey: 'uiSchemaUid'
}, },
{ {

View File

@ -1,5 +1,5 @@
import { MockServer, mockServer } from '@nocobase/test';
import { Database } from '@nocobase/database'; import { Database } from '@nocobase/database';
import { MockServer, mockServer } from '@nocobase/test';
import PluginUiSchema from '../server'; import PluginUiSchema from '../server';
describe('action test', () => { describe('action test', () => {
@ -34,7 +34,7 @@ describe('action test', () => {
test('insert action', async () => { test('insert action', async () => {
const response = await app const response = await app
.agent() .agent()
.resource('ui_schemas') .resource('uiSchemas')
.insert({ .insert({
values: { values: {
'x-uid': 'n1', 'x-uid': 'n1',
@ -59,7 +59,7 @@ describe('action test', () => {
test('getJsonSchema', async () => { test('getJsonSchema', async () => {
await app await app
.agent() .agent()
.resource('ui_schemas') .resource('uiSchemas')
.insert({ .insert({
values: { values: {
'x-uid': 'n1', 'x-uid': 'n1',
@ -78,7 +78,7 @@ describe('action test', () => {
}, },
}); });
const response = await app.agent().resource('ui_schemas').getJsonSchema({ const response = await app.agent().resource('uiSchemas').getJsonSchema({
resourceIndex: 'n1', resourceIndex: 'n1',
}); });
@ -87,7 +87,7 @@ describe('action test', () => {
}); });
test('getJsonSchema when uid not exists', async () => { test('getJsonSchema when uid not exists', async () => {
const response = await app.agent().resource('ui_schemas').getJsonSchema({ const response = await app.agent().resource('uiSchemas').getJsonSchema({
resourceIndex: 'not-exists', resourceIndex: 'not-exists',
}); });
@ -95,7 +95,7 @@ describe('action test', () => {
}); });
test('get properties when uid not exists', async () => { test('get properties when uid not exists', async () => {
const response = await app.agent().resource('ui_schemas').getProperties({ const response = await app.agent().resource('uiSchemas').getProperties({
resourceIndex: 'not-exists', resourceIndex: 'not-exists',
}); });
@ -105,7 +105,7 @@ describe('action test', () => {
test('remove', async () => { test('remove', async () => {
await app await app
.agent() .agent()
.resource('ui_schemas') .resource('uiSchemas')
.insert({ .insert({
values: { values: {
'x-uid': 'n1', 'x-uid': 'n1',
@ -124,13 +124,13 @@ describe('action test', () => {
}, },
}); });
let response = await app.agent().resource('ui_schemas').remove({ let response = await app.agent().resource('uiSchemas').remove({
resourceIndex: 'n2', resourceIndex: 'n2',
}); });
expect(response.statusCode).toEqual(200); expect(response.statusCode).toEqual(200);
response = await app.agent().resource('ui_schemas').getJsonSchema({ response = await app.agent().resource('uiSchemas').getJsonSchema({
resourceIndex: 'n1', resourceIndex: 'n1',
}); });
@ -141,7 +141,7 @@ describe('action test', () => {
test('patch', async () => { test('patch', async () => {
await app await app
.agent() .agent()
.resource('ui_schemas') .resource('uiSchemas')
.insert({ .insert({
values: { values: {
'x-uid': 'n1', 'x-uid': 'n1',
@ -162,7 +162,7 @@ describe('action test', () => {
let response = await app let response = await app
.agent() .agent()
.resource('ui_schemas') .resource('uiSchemas')
.patch({ .patch({
values: { values: {
'x-uid': 'n1', 'x-uid': 'n1',
@ -179,7 +179,7 @@ describe('action test', () => {
}); });
expect(response.statusCode).toEqual(200); expect(response.statusCode).toEqual(200);
response = await app.agent().resource('ui_schemas').getJsonSchema({ response = await app.agent().resource('uiSchemas').getJsonSchema({
resourceIndex: 'n1', resourceIndex: 'n1',
}); });
@ -190,7 +190,7 @@ describe('action test', () => {
test('insert adjacent', async () => { test('insert adjacent', async () => {
await app await app
.agent() .agent()
.resource('ui_schemas') .resource('uiSchemas')
.insert({ .insert({
values: { values: {
'x-uid': 'n1', 'x-uid': 'n1',
@ -211,7 +211,7 @@ describe('action test', () => {
let response = await app let response = await app
.agent() .agent()
.resource('ui_schemas') .resource('uiSchemas')
.insertAdjacent({ .insertAdjacent({
resourceIndex: 'n2', resourceIndex: 'n2',
position: 'beforeBegin', position: 'beforeBegin',
@ -222,7 +222,7 @@ describe('action test', () => {
}); });
expect(response.statusCode).toEqual(200); expect(response.statusCode).toEqual(200);
response = await app.agent().resource('ui_schemas').getJsonSchema({ response = await app.agent().resource('uiSchemas').getJsonSchema({
resourceIndex: 'n1', resourceIndex: 'n1',
}); });

View File

@ -1,8 +1,8 @@
import { mockServer, MockServer } from '@nocobase/test'; import { BelongsToManyRepository, Database } from '@nocobase/database';
import { BelongsToManyRepository, Database, HasManyRepository } from '@nocobase/database';
import UiSchemaStoragePlugin, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
import PluginCollectionManager from '@nocobase/plugin-collection-manager';
import PluginACL from '@nocobase/plugin-acl'; import PluginACL from '@nocobase/plugin-acl';
import PluginCollectionManager from '@nocobase/plugin-collection-manager';
import UiSchemaStoragePlugin, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
import { mockServer, MockServer } from '@nocobase/test';
describe('server hooks', () => { describe('server hooks', () => {
let app: MockServer; let app: MockServer;
@ -27,7 +27,7 @@ describe('server hooks', () => {
await app.loadAndInstall(); await app.loadAndInstall();
uiSchemaRepository = db.getRepository('ui_schemas'); uiSchemaRepository = db.getRepository('uiSchemas');
uiSchemaPlugin = app.getPlugin<UiSchemaStoragePlugin>('UiSchemaStoragePlugin'); uiSchemaPlugin = app.getPlugin<UiSchemaStoragePlugin>('UiSchemaStoragePlugin');
}); });

View File

@ -1,7 +1,7 @@
import { mockServer, MockServer } from '@nocobase/test';
import { Database } from '@nocobase/database'; import { Database } from '@nocobase/database';
import UiSchemaStoragePlugin, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
import PluginCollectionManager from '@nocobase/plugin-collection-manager'; import PluginCollectionManager from '@nocobase/plugin-collection-manager';
import UiSchemaStoragePlugin, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
import { mockServer, MockServer } from '@nocobase/test';
describe('server hooks', () => { describe('server hooks', () => {
let app: MockServer; let app: MockServer;
@ -65,7 +65,7 @@ describe('server hooks', () => {
await app.loadAndInstall(); await app.loadAndInstall();
uiSchemaRepository = db.getRepository('ui_schemas'); uiSchemaRepository = db.getRepository('uiSchemas');
await uiSchemaRepository.insert(schema); await uiSchemaRepository.insert(schema);
uiSchemaPlugin = app.getPlugin<UiSchemaStoragePlugin>('UiSchemaStoragePlugin'); uiSchemaPlugin = app.getPlugin<UiSchemaStoragePlugin>('UiSchemaStoragePlugin');

View File

@ -1,5 +1,5 @@
import { mockServer, MockServer } from '@nocobase/test';
import { Collection, Database } from '@nocobase/database'; import { Collection, Database } from '@nocobase/database';
import { mockServer, MockServer } from '@nocobase/test';
import PluginUiSchema, { UiSchemaRepository } from '..'; import PluginUiSchema, { UiSchemaRepository } from '..';
describe('ui schema model', () => { describe('ui schema model', () => {
@ -32,7 +32,7 @@ describe('ui schema model', () => {
{ {
type: 'belongsTo', type: 'belongsTo',
name: 'uiSchema', name: 'uiSchema',
target: 'ui_schemas', target: 'uiSchemas',
}, },
], ],
}); });
@ -41,7 +41,7 @@ describe('ui schema model', () => {
}); });
it('should create schema tree after ui_schema created', async () => { it('should create schema tree after ui_schema created', async () => {
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
await RelatedCollection.repository.create({ await RelatedCollection.repository.create({
values: { values: {
@ -82,7 +82,7 @@ describe('ui schema model', () => {
}); });
it('should update schema tree after ui_schema updated', async () => { it('should update schema tree after ui_schema updated', async () => {
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
const relatedInstance = await RelatedCollection.repository.create({ const relatedInstance = await RelatedCollection.repository.create({
values: { values: {

View File

@ -1,8 +1,8 @@
import { mockServer, MockServer } from '@nocobase/test';
import { Collection, Database } from '@nocobase/database'; import { Collection, Database } from '@nocobase/database';
import PluginUiSchema from '../server'; import { mockServer, MockServer } from '@nocobase/test';
import UiSchemaRepository from '../repository';
import { SchemaNode } from '../dao/ui_schema_node_dao'; import { SchemaNode } from '../dao/ui_schema_node_dao';
import UiSchemaRepository from '../repository';
import PluginUiSchema from '../server';
describe('ui_schema repository', () => { describe('ui_schema repository', () => {
let app: MockServer; let app: MockServer;
@ -34,12 +34,12 @@ describe('ui_schema repository', () => {
drop: false, drop: false,
}, },
}); });
repository = db.getCollection('ui_schemas').repository as UiSchemaRepository; repository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
treePathCollection = db.getCollection('ui_schema_tree_path'); treePathCollection = db.getCollection('ui_schema_tree_path');
}); });
it('should be registered', async () => { it('should be registered', async () => {
expect(db.getCollection('ui_schemas').repository).toBeInstanceOf(UiSchemaRepository); expect(db.getCollection('uiSchemas').repository).toBeInstanceOf(UiSchemaRepository);
}); });
it('should insert single ui schema node', async () => { it('should insert single ui schema node', async () => {

View File

@ -1,6 +1,6 @@
import { ISchema } from '@formily/json-schema'; import { ISchema } from '@formily/json-schema';
import { Database } from '@nocobase/database';
import { mockServer, MockServer } from '@nocobase/test'; import { mockServer, MockServer } from '@nocobase/test';
import { Collection, Database } from '@nocobase/database';
import PluginUiSchema, { UiSchemaRepository } from '..'; import PluginUiSchema, { UiSchemaRepository } from '..';
describe('ui-schema', () => { describe('ui-schema', () => {
@ -25,7 +25,7 @@ describe('ui-schema', () => {
app.plugin(PluginUiSchema); app.plugin(PluginUiSchema);
await app.loadAndInstall(); await app.loadAndInstall();
uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
}); });
type SchemaProperties = Record<string, ISchema>; type SchemaProperties = Record<string, ISchema>;

View File

@ -2,7 +2,7 @@ import { Context } from '@nocobase/actions';
import UiSchemaRepository from '../repository'; import UiSchemaRepository from '../repository';
const getRepositoryFromCtx = (ctx: Context) => { const getRepositoryFromCtx = (ctx: Context) => {
return ctx.db.getCollection('ui_schemas').repository as UiSchemaRepository; return ctx.db.getCollection('uiSchemas').repository as UiSchemaRepository;
}; };
export const uiSchemaActions = { export const uiSchemaActions = {

View File

@ -6,7 +6,7 @@ export default {
// autoGenId: false, // autoGenId: false,
timestamps: false, timestamps: false,
fields: [ fields: [
{ type: 'belongsTo', name: 'uiSchema', target: 'ui_schemas', foreignKey: 'uid', onDelete: 'CASCADE' }, { type: 'belongsTo', name: 'uiSchema', target: 'uiSchemas', foreignKey: 'uid', onDelete: 'CASCADE' },
{ type: 'string', name: 'type' }, { type: 'string', name: 'type' },
{ {
type: 'string', type: 'string',

View File

@ -1,7 +1,7 @@
import { CollectionOptions } from '@nocobase/database'; import { CollectionOptions } from '@nocobase/database';
export default { export default {
name: 'ui_schemas', name: 'uiSchemas',
title: '字段配置', title: '字段配置',
autoGenId: false, autoGenId: false,
timestamps: false, timestamps: false,

View File

@ -12,6 +12,22 @@ interface GetJsonSchemaOptions {
const nodeKeys = ['properties', 'definitions', 'patternProperties', 'additionalProperties', 'items']; const nodeKeys = ['properties', 'definitions', 'patternProperties', 'additionalProperties', 'items'];
export class UiSchemaRepository extends Repository { export class UiSchemaRepository extends Repository {
get uiSchemasTableName() {
if (this.database.sequelize.getDialect() === 'postgres') {
return `"${this.model.tableName}"`;
}
return this.model.tableName;
}
get uiSchemaTreePathTableName() {
const model = this.database.getCollection('ui_schema_tree_path').model;
if (this.database.sequelize.getDialect() === 'postgres') {
return `"${model.tableName}"`;
}
return model.tableName;
}
static schemaToSingleNodes(schema: any, carry: SchemaNode[] = [], childOptions: ChildOptions = null): SchemaNode[] { static schemaToSingleNodes(schema: any, carry: SchemaNode[] = [], childOptions: ChildOptions = null): SchemaNode[] {
const node = lodash.cloneDeep( const node = lodash.cloneDeep(
lodash.isString(schema) lodash.isString(schema)
@ -71,16 +87,19 @@ export class UiSchemaRepository extends Repository {
async getProperties(uid: string) { async getProperties(uid: string) {
const db = this.database; const db = this.database;
const treeCollection = db.getCollection('ui_schema_tree_path');
const rawSql = ` const rawSql = `
SELECT SchemaTable.uid as uid, SchemaTable.name as name, SchemaTable.schema as "schema", SELECT SchemaTable.uid as uid, SchemaTable.name as name, SchemaTable.schema as "schema",
TreePath.depth as depth, TreePath.depth as depth,
NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
FROM ${treeCollection.model.tableName} as TreePath FROM ${this.uiSchemaTreePathTableName} as TreePath
LEFT JOIN ${this.model.tableName} as SchemaTable ON SchemaTable.uid = TreePath.descendant LEFT JOIN ${this.uiSchemasTableName} as SchemaTable ON SchemaTable.uid = TreePath.descendant
LEFT JOIN ${treeCollection.model.tableName} as NodeInfo ON NodeInfo.descendant = SchemaTable.uid and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0 LEFT JOIN ${
LEFT JOIN ${treeCollection.model.tableName} as ParentPath ON (ParentPath.descendant = SchemaTable.uid AND ParentPath.depth = 1) this.uiSchemaTreePathTableName
} as NodeInfo ON NodeInfo.descendant = SchemaTable.uid and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
LEFT JOIN ${
this.uiSchemaTreePathTableName
} as ParentPath ON (ParentPath.descendant = SchemaTable.uid AND ParentPath.depth = 1)
WHERE TreePath.ancestor = :ancestor AND (NodeInfo.async = false or TreePath.depth = 1)`; WHERE TreePath.ancestor = :ancestor AND (NodeInfo.async = false or TreePath.depth = 1)`;
const nodes = await db.sequelize.query(rawSql, { const nodes = await db.sequelize.query(rawSql, {
@ -99,16 +118,15 @@ export class UiSchemaRepository extends Repository {
async getJsonSchema(uid: string, options?: GetJsonSchemaOptions) { async getJsonSchema(uid: string, options?: GetJsonSchemaOptions) {
const db = this.database; const db = this.database;
const treeCollection = db.getCollection('ui_schema_tree_path');
const treeTable = treeCollection.model.tableName; const treeTable = this.uiSchemaTreePathTableName;
const rawSql = ` const rawSql = `
SELECT SchemaTable.uid as uid, SchemaTable.name as name, SchemaTable.schema as "schema" , SELECT SchemaTable.uid as uid, SchemaTable.name as name, SchemaTable.schema as "schema" ,
TreePath.depth as depth, TreePath.depth as depth,
NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort
FROM ${treeTable} as TreePath FROM ${treeTable} as TreePath
LEFT JOIN ${this.model.tableName} as SchemaTable ON SchemaTable.uid = TreePath.descendant LEFT JOIN ${this.uiSchemasTableName} as SchemaTable ON SchemaTable.uid = TreePath.descendant
LEFT JOIN ${treeTable} as NodeInfo ON NodeInfo.descendant = SchemaTable.uid and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0 LEFT JOIN ${treeTable} as NodeInfo ON NodeInfo.descendant = SchemaTable.uid and NodeInfo.descendant = NodeInfo.ancestor and NodeInfo.depth = 0
LEFT JOIN ${treeTable} as ParentPath ON (ParentPath.descendant = SchemaTable.uid AND ParentPath.depth = 1) LEFT JOIN ${treeTable} as ParentPath ON (ParentPath.descendant = SchemaTable.uid AND ParentPath.depth = 1)
WHERE TreePath.ancestor = :ancestor ${options?.includeAsyncNode ? '' : 'AND (NodeInfo.async != true )'} WHERE TreePath.ancestor = :ancestor ${options?.includeAsyncNode ? '' : 'AND (NodeInfo.async != true )'}
@ -269,7 +287,7 @@ export class UiSchemaRepository extends Repository {
const parentChildrenCount = countResult[0]['count']; const parentChildrenCount = countResult[0]['count'];
if (parentChildrenCount == 1) { if (parentChildrenCount == 1) {
const schema = await db.getRepository('ui_schemas').findOne({ const schema = await db.getRepository('uiSchemas').findOne({
filter: { filter: {
uid: parent.get('ancestor') as string, uid: parent.get('ancestor') as string,
}, },
@ -311,11 +329,11 @@ export class UiSchemaRepository extends Repository {
transaction = await this.database.sequelize.transaction(); transaction = await this.database.sequelize.transaction();
} }
const treePathTable = this.treeCollection().model.tableName; const treePathTable = this.uiSchemaTreePathTableName;
try { try {
await this.database.sequelize.query( await this.database.sequelize.query(
`DELETE FROM ${this.model.tableName} WHERE uid IN ( `DELETE FROM ${this.uiSchemasTableName} WHERE uid IN (
SELECT descendant FROM ${treePathTable} WHERE ancestor = :uid SELECT descendant FROM ${treePathTable} WHERE ancestor = :uid
) )
`, `,
@ -362,7 +380,7 @@ export class UiSchemaRepository extends Repository {
}); });
const db = this.database; const db = this.database;
const treeTable = this.treeCollection().model.tableName; const treeTable = this.uiSchemaTreePathTableName;
const typeQuery = await db.sequelize.query(`SELECT type from ${treeTable} WHERE ancestor = :uid AND depth = 0;`, { const typeQuery = await db.sequelize.query(`SELECT type from ${treeTable} WHERE ancestor = :uid AND depth = 0;`, {
type: 'SELECT', type: 'SELECT',
replacements: { replacements: {
@ -503,7 +521,7 @@ export class UiSchemaRepository extends Repository {
transaction, transaction,
}); });
const treeTable = treeCollection.model.tableName; const treeTable = this.uiSchemaTreePathTableName;
if (existsNode) { if (existsNode) {
savedNode = existsNode; savedNode = existsNode;

View File

@ -2,7 +2,7 @@ import { UiSchemaRepository } from '../../repository';
export async function removeSchema({ schemaInstance, options, db, params }) { export async function removeSchema({ schemaInstance, options, db, params }) {
const { transaction } = options; const { transaction } = options;
const uiSchemaRepository: UiSchemaRepository = db.getRepository('ui_schemas'); const uiSchemaRepository: UiSchemaRepository = db.getRepository('uiSchemas');
const uid = schemaInstance.get('uid') as string; const uid = schemaInstance.get('uid') as string;
if (params?.removeEmptyParents) { if (params?.removeEmptyParents) {

View File

@ -1,6 +1,6 @@
import { Database } from '@nocobase/database'; import { Database } from '@nocobase/database';
import { ServerHookModel } from './model';
import { hooks } from './hooks'; import { hooks } from './hooks';
import { ServerHookModel } from './model';
export type HookType = export type HookType =
| 'onSelfDestroy' | 'onSelfDestroy'
@ -31,7 +31,7 @@ export class ServerHooks {
await this.onCollectionDestroy(model, options); await this.onCollectionDestroy(model, options);
}); });
this.db.on('ui_schemas.afterCreateWithAssociations', async (model, options) => { this.db.on('uiSchemas.afterCreateWithAssociations', async (model, options) => {
await this.onUiSchemaCreate(model, options); await this.onUiSchemaCreate(model, options);
}); });
} }

View File

@ -26,16 +26,16 @@ export class UiSchemaStoragePlugin extends Plugin {
this.registerRepository(); this.registerRepository();
db.on('ui_schemas.beforeCreate', function setUid(model) { db.on('uiSchemas.beforeCreate', function setUid(model) {
model.set('uid', model.get('x-uid')); model.set('uid', model.get('x-uid'));
if (!model.get('name')) { if (!model.get('name')) {
model.set('name', uid()); model.set('name', uid());
} }
}); });
db.on('ui_schemas.afterCreate', async function insertSchema(model, options) { db.on('uiSchemas.afterCreate', async function insertSchema(model, options) {
const { transaction } = options; const { transaction } = options;
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
const context = options.context; const context = options.context;
@ -48,9 +48,9 @@ export class UiSchemaStoragePlugin extends Plugin {
}); });
}); });
db.on('ui_schemas.afterUpdate', async function patchSchema(model, options) { db.on('uiSchemas.afterUpdate', async function patchSchema(model, options) {
const { transaction } = options; const { transaction } = options;
const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository;
await uiSchemaRepository.patch(model.toJSON(), { await uiSchemaRepository.patch(model.toJSON(), {
transaction, transaction,
@ -58,7 +58,7 @@ export class UiSchemaStoragePlugin extends Plugin {
}); });
this.app.resourcer.define({ this.app.resourcer.define({
name: 'ui_schemas', name: 'uiSchemas',
actions: uiSchemaActions, actions: uiSchemaActions,
}); });
} }