From 04405918689ec8ebbfaa4ade5ec2eff7b02dda84 Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 16 Feb 2022 00:22:47 +0800 Subject: [PATCH] feat: rename ui_schemas to uiSchemas --- jest.cli.js | 7 ++- package.json | 2 +- packages/app/src/pages/mock.ts | 2 +- .../src/application/demos/demo2/mock.ts | 4 +- .../core/RemoteSchemaComponent.tsx | 2 +- .../schema-component/hooks/useDesignable.tsx | 6 +-- packages/plugin-acl/src/collections/roles.ts | 2 +- .../src/collections/fields.ts | 2 +- .../src/collections/uiRoutes.ts | 2 +- .../src/__tests__/action.test.ts | 30 ++++++------- .../src/__tests__/server-hook-impl.test.ts | 10 ++--- .../src/__tests__/server-hook.test.ts | 6 +-- .../src/__tests__/ui-schema-model.test.ts | 8 ++-- .../__tests__/ui-schema-repository.test.ts | 10 ++--- .../src/__tests__/ui-schema.test.ts | 4 +- .../src/actions/ui-schema-action.ts | 2 +- .../src/collections/uiSchemaServerHooks.ts | 2 +- .../src/collections/ui_schemas.ts | 2 +- .../src/repository.ts | 44 +++++++++++++------ .../src/server-hooks/hooks/remove-schema.ts | 2 +- .../src/server-hooks/index.ts | 4 +- .../plugin-ui-schema-storage/src/server.ts | 12 ++--- 22 files changed, 94 insertions(+), 71 deletions(-) diff --git a/jest.cli.js b/jest.cli.js index c14e84318..8be1963dd 100644 --- a/jest.cli.js +++ b/jest.cli.js @@ -13,6 +13,11 @@ if (yargs.argv.dbDialect) { process.env.DB_DIALECT = yargs.argv.dbDialect; } -console.log('DB_DIALECT: ', process.env.DB_DIALECT); +if (yargs.argv.dbPort) { + process.env.DB_PORT = yargs.argv.dbPort; +} + +console.log('DB_DIALECT:', process.env.DB_DIALECT); +console.log('DB_PORT:', process.env.DB_PORT); require('jest-cli/bin/jest'); diff --git a/package.json b/package.json index fe7dca661..f6d0e94da 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "start-server": "ts-node-dev -r dotenv/config -r tsconfig-paths/register ./packages/api/src/index.ts", "build": "lerna run build", "build-docs": "dumi build", - "test": "node ./jest.cli.js", + "test": "node ./jest.cli.js -i", "lint": "eslint ." }, "resolutions": { diff --git a/packages/app/src/pages/mock.ts b/packages/app/src/pages/mock.ts index b6baf047a..5b05e1178 100644 --- a/packages/app/src/pages/mock.ts +++ b/packages/app/src/pages/mock.ts @@ -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(); if (name && jsonSchema[name]) { diff --git a/packages/client/src/application/demos/demo2/mock.ts b/packages/client/src/application/demos/demo2/mock.ts index fa9799184..af20beb6e 100644 --- a/packages/client/src/application/demos/demo2/mock.ts +++ b/packages/client/src/application/demos/demo2/mock.ts @@ -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(); console.log(name); if (jsonSchema[name]) { @@ -135,7 +135,7 @@ export default (apiClient: APIClient) => { 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(); // console.log(name); // if (jsonSchema[name]) { diff --git a/packages/client/src/schema-component/core/RemoteSchemaComponent.tsx b/packages/client/src/schema-component/core/RemoteSchemaComponent.tsx index e15884201..843360ba2 100644 --- a/packages/client/src/schema-component/core/RemoteSchemaComponent.tsx +++ b/packages/client/src/schema-component/core/RemoteSchemaComponent.tsx @@ -21,7 +21,7 @@ const RequestSchemaComponent: React.FC = (props) => const { onlyRenderProperties, hidden, scope, uid, onSuccess, schemaTransform = defaultTransform } = props; const { reset } = useSchemaComponentContext(); const conf = { - url: `/ui_schemas:${onlyRenderProperties ? 'getProperties' : 'getJsonSchema'}/${uid}`, + url: `/uiSchemas:${onlyRenderProperties ? 'getProperties' : 'getJsonSchema'}/${uid}`, }; const { data, loading } = useRequest(conf, { refreshDeps: [uid], diff --git a/packages/client/src/schema-component/hooks/useDesignable.tsx b/packages/client/src/schema-component/hooks/useDesignable.tsx index cd1f15543..ba52b39ba 100644 --- a/packages/client/src/schema-component/hooks/useDesignable.tsx +++ b/packages/client/src/schema-component/hooks/useDesignable.tsx @@ -82,13 +82,13 @@ export class Designable { this.on('insertAdjacent', async ({ current, position, schema, removed }) => { refresh(); await api.request({ - url: `/ui_schemas:insertAdjacent/${current['x-uid']}?position=${position}`, + url: `/uiSchemas:insertAdjacent/${current['x-uid']}?position=${position}`, method: 'post', data: schema.toJSON(), }); if (removed?.['x-uid']) { await api.request({ - url: `/ui_schemas:remove/${removed['x-uid']}`, + url: `/uiSchemas:remove/${removed['x-uid']}`, method: 'post', }); } @@ -97,7 +97,7 @@ export class Designable { refresh(); if (removed?.['x-uid']) { await api.request({ - url: `/ui_schemas:remove/${removed['x-uid']}`, + url: `/uiSchemas:remove/${removed['x-uid']}`, method: 'post', }); } diff --git a/packages/plugin-acl/src/collections/roles.ts b/packages/plugin-acl/src/collections/roles.ts index a5b7728cb..e8b51f266 100644 --- a/packages/plugin-acl/src/collections/roles.ts +++ b/packages/plugin-acl/src/collections/roles.ts @@ -42,7 +42,7 @@ export default { { type: 'belongsToMany', name: 'menuUiSchemas', - target: 'ui_schemas', + target: 'uiSchemas', }, { type: 'hasMany', diff --git a/packages/plugin-collection-manager/src/collections/fields.ts b/packages/plugin-collection-manager/src/collections/fields.ts index b4880b919..f812d7ac7 100644 --- a/packages/plugin-collection-manager/src/collections/fields.ts +++ b/packages/plugin-collection-manager/src/collections/fields.ts @@ -53,7 +53,7 @@ export default { { type: 'belongsTo', name: 'uiSchema', - target: 'ui_schemas', + target: 'uiSchemas', foreignKey: 'uiSchemaUid' }, { diff --git a/packages/plugin-ui-routes-storage/src/collections/uiRoutes.ts b/packages/plugin-ui-routes-storage/src/collections/uiRoutes.ts index 5c15dabe3..06846ea94 100644 --- a/packages/plugin-ui-routes-storage/src/collections/uiRoutes.ts +++ b/packages/plugin-ui-routes-storage/src/collections/uiRoutes.ts @@ -30,7 +30,7 @@ export default defineCollection({ { type: 'belongsTo', name: 'uiSchema', - target: 'ui_schemas', + target: 'uiSchemas', foreignKey: 'uiSchemaUid' }, { diff --git a/packages/plugin-ui-schema-storage/src/__tests__/action.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/action.test.ts index d74ab68ea..989ad3131 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/action.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/action.test.ts @@ -1,5 +1,5 @@ -import { MockServer, mockServer } from '@nocobase/test'; import { Database } from '@nocobase/database'; +import { MockServer, mockServer } from '@nocobase/test'; import PluginUiSchema from '../server'; describe('action test', () => { @@ -34,7 +34,7 @@ describe('action test', () => { test('insert action', async () => { const response = await app .agent() - .resource('ui_schemas') + .resource('uiSchemas') .insert({ values: { 'x-uid': 'n1', @@ -59,7 +59,7 @@ describe('action test', () => { test('getJsonSchema', async () => { await app .agent() - .resource('ui_schemas') + .resource('uiSchemas') .insert({ values: { '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', }); @@ -87,7 +87,7 @@ describe('action test', () => { }); 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', }); @@ -95,7 +95,7 @@ describe('action test', () => { }); 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', }); @@ -105,7 +105,7 @@ describe('action test', () => { test('remove', async () => { await app .agent() - .resource('ui_schemas') + .resource('uiSchemas') .insert({ values: { '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', }); expect(response.statusCode).toEqual(200); - response = await app.agent().resource('ui_schemas').getJsonSchema({ + response = await app.agent().resource('uiSchemas').getJsonSchema({ resourceIndex: 'n1', }); @@ -141,7 +141,7 @@ describe('action test', () => { test('patch', async () => { await app .agent() - .resource('ui_schemas') + .resource('uiSchemas') .insert({ values: { 'x-uid': 'n1', @@ -162,7 +162,7 @@ describe('action test', () => { let response = await app .agent() - .resource('ui_schemas') + .resource('uiSchemas') .patch({ values: { 'x-uid': 'n1', @@ -179,7 +179,7 @@ describe('action test', () => { }); expect(response.statusCode).toEqual(200); - response = await app.agent().resource('ui_schemas').getJsonSchema({ + response = await app.agent().resource('uiSchemas').getJsonSchema({ resourceIndex: 'n1', }); @@ -190,7 +190,7 @@ describe('action test', () => { test('insert adjacent', async () => { await app .agent() - .resource('ui_schemas') + .resource('uiSchemas') .insert({ values: { 'x-uid': 'n1', @@ -211,7 +211,7 @@ describe('action test', () => { let response = await app .agent() - .resource('ui_schemas') + .resource('uiSchemas') .insertAdjacent({ resourceIndex: 'n2', position: 'beforeBegin', @@ -222,7 +222,7 @@ describe('action test', () => { }); expect(response.statusCode).toEqual(200); - response = await app.agent().resource('ui_schemas').getJsonSchema({ + response = await app.agent().resource('uiSchemas').getJsonSchema({ resourceIndex: 'n1', }); diff --git a/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts index 3caca820f..688f6e1e0 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/server-hook-impl.test.ts @@ -1,8 +1,8 @@ -import { mockServer, MockServer } from '@nocobase/test'; -import { BelongsToManyRepository, Database, HasManyRepository } from '@nocobase/database'; -import UiSchemaStoragePlugin, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage'; -import PluginCollectionManager from '@nocobase/plugin-collection-manager'; +import { BelongsToManyRepository, Database } from '@nocobase/database'; 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', () => { let app: MockServer; @@ -27,7 +27,7 @@ describe('server hooks', () => { await app.loadAndInstall(); - uiSchemaRepository = db.getRepository('ui_schemas'); + uiSchemaRepository = db.getRepository('uiSchemas'); uiSchemaPlugin = app.getPlugin('UiSchemaStoragePlugin'); }); diff --git a/packages/plugin-ui-schema-storage/src/__tests__/server-hook.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/server-hook.test.ts index 97afc8fb5..608034514 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/server-hook.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/server-hook.test.ts @@ -1,7 +1,7 @@ -import { mockServer, MockServer } from '@nocobase/test'; import { Database } from '@nocobase/database'; -import UiSchemaStoragePlugin, { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage'; 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', () => { let app: MockServer; @@ -65,7 +65,7 @@ describe('server hooks', () => { await app.loadAndInstall(); - uiSchemaRepository = db.getRepository('ui_schemas'); + uiSchemaRepository = db.getRepository('uiSchemas'); await uiSchemaRepository.insert(schema); uiSchemaPlugin = app.getPlugin('UiSchemaStoragePlugin'); diff --git a/packages/plugin-ui-schema-storage/src/__tests__/ui-schema-model.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/ui-schema-model.test.ts index 0a46fd4f2..f9a9b2819 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/ui-schema-model.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/ui-schema-model.test.ts @@ -1,5 +1,5 @@ -import { mockServer, MockServer } from '@nocobase/test'; import { Collection, Database } from '@nocobase/database'; +import { mockServer, MockServer } from '@nocobase/test'; import PluginUiSchema, { UiSchemaRepository } from '..'; describe('ui schema model', () => { @@ -32,7 +32,7 @@ describe('ui schema model', () => { { type: 'belongsTo', 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 () => { - const uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; + const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository; await RelatedCollection.repository.create({ values: { @@ -82,7 +82,7 @@ describe('ui schema model', () => { }); 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({ values: { diff --git a/packages/plugin-ui-schema-storage/src/__tests__/ui-schema-repository.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/ui-schema-repository.test.ts index 1d03b3f56..06849c24e 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/ui-schema-repository.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/ui-schema-repository.test.ts @@ -1,8 +1,8 @@ -import { mockServer, MockServer } from '@nocobase/test'; import { Collection, Database } from '@nocobase/database'; -import PluginUiSchema from '../server'; -import UiSchemaRepository from '../repository'; +import { mockServer, MockServer } from '@nocobase/test'; import { SchemaNode } from '../dao/ui_schema_node_dao'; +import UiSchemaRepository from '../repository'; +import PluginUiSchema from '../server'; describe('ui_schema repository', () => { let app: MockServer; @@ -34,12 +34,12 @@ describe('ui_schema repository', () => { drop: false, }, }); - repository = db.getCollection('ui_schemas').repository as UiSchemaRepository; + repository = db.getCollection('uiSchemas').repository as UiSchemaRepository; treePathCollection = db.getCollection('ui_schema_tree_path'); }); 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 () => { diff --git a/packages/plugin-ui-schema-storage/src/__tests__/ui-schema.test.ts b/packages/plugin-ui-schema-storage/src/__tests__/ui-schema.test.ts index da5dd0dbc..22dbb25d0 100644 --- a/packages/plugin-ui-schema-storage/src/__tests__/ui-schema.test.ts +++ b/packages/plugin-ui-schema-storage/src/__tests__/ui-schema.test.ts @@ -1,6 +1,6 @@ import { ISchema } from '@formily/json-schema'; +import { Database } from '@nocobase/database'; import { mockServer, MockServer } from '@nocobase/test'; -import { Collection, Database } from '@nocobase/database'; import PluginUiSchema, { UiSchemaRepository } from '..'; describe('ui-schema', () => { @@ -25,7 +25,7 @@ describe('ui-schema', () => { app.plugin(PluginUiSchema); await app.loadAndInstall(); - uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; + uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository; }); type SchemaProperties = Record; diff --git a/packages/plugin-ui-schema-storage/src/actions/ui-schema-action.ts b/packages/plugin-ui-schema-storage/src/actions/ui-schema-action.ts index 78f704aeb..872565c93 100644 --- a/packages/plugin-ui-schema-storage/src/actions/ui-schema-action.ts +++ b/packages/plugin-ui-schema-storage/src/actions/ui-schema-action.ts @@ -2,7 +2,7 @@ import { Context } from '@nocobase/actions'; import UiSchemaRepository from '../repository'; const getRepositoryFromCtx = (ctx: Context) => { - return ctx.db.getCollection('ui_schemas').repository as UiSchemaRepository; + return ctx.db.getCollection('uiSchemas').repository as UiSchemaRepository; }; export const uiSchemaActions = { diff --git a/packages/plugin-ui-schema-storage/src/collections/uiSchemaServerHooks.ts b/packages/plugin-ui-schema-storage/src/collections/uiSchemaServerHooks.ts index 3f0b6a3fe..651c9026c 100644 --- a/packages/plugin-ui-schema-storage/src/collections/uiSchemaServerHooks.ts +++ b/packages/plugin-ui-schema-storage/src/collections/uiSchemaServerHooks.ts @@ -6,7 +6,7 @@ export default { // autoGenId: false, timestamps: false, 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', diff --git a/packages/plugin-ui-schema-storage/src/collections/ui_schemas.ts b/packages/plugin-ui-schema-storage/src/collections/ui_schemas.ts index fe12b653d..46ea4168e 100644 --- a/packages/plugin-ui-schema-storage/src/collections/ui_schemas.ts +++ b/packages/plugin-ui-schema-storage/src/collections/ui_schemas.ts @@ -1,7 +1,7 @@ import { CollectionOptions } from '@nocobase/database'; export default { - name: 'ui_schemas', + name: 'uiSchemas', title: '字段配置', autoGenId: false, timestamps: false, diff --git a/packages/plugin-ui-schema-storage/src/repository.ts b/packages/plugin-ui-schema-storage/src/repository.ts index 88763a02f..03692f911 100644 --- a/packages/plugin-ui-schema-storage/src/repository.ts +++ b/packages/plugin-ui-schema-storage/src/repository.ts @@ -12,6 +12,22 @@ interface GetJsonSchemaOptions { const nodeKeys = ['properties', 'definitions', 'patternProperties', 'additionalProperties', 'items']; 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[] { const node = lodash.cloneDeep( lodash.isString(schema) @@ -71,16 +87,19 @@ export class UiSchemaRepository extends Repository { async getProperties(uid: string) { const db = this.database; - const treeCollection = db.getCollection('ui_schema_tree_path'); const rawSql = ` SELECT SchemaTable.uid as uid, SchemaTable.name as name, SchemaTable.schema as "schema", TreePath.depth as depth, NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort - FROM ${treeCollection.model.tableName} as TreePath - LEFT JOIN ${this.model.tableName} 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 ${treeCollection.model.tableName} as ParentPath ON (ParentPath.descendant = SchemaTable.uid AND ParentPath.depth = 1) + FROM ${this.uiSchemaTreePathTableName} as TreePath + LEFT JOIN ${this.uiSchemasTableName} as SchemaTable ON SchemaTable.uid = TreePath.descendant + LEFT JOIN ${ + 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)`; const nodes = await db.sequelize.query(rawSql, { @@ -99,16 +118,15 @@ export class UiSchemaRepository extends Repository { async getJsonSchema(uid: string, options?: GetJsonSchemaOptions) { const db = this.database; - const treeCollection = db.getCollection('ui_schema_tree_path'); - const treeTable = treeCollection.model.tableName; + const treeTable = this.uiSchemaTreePathTableName; const rawSql = ` SELECT SchemaTable.uid as uid, SchemaTable.name as name, SchemaTable.schema as "schema" , TreePath.depth as depth, NodeInfo.type as type, NodeInfo.async as async, ParentPath.ancestor as parent, ParentPath.sort as sort 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 ParentPath ON (ParentPath.descendant = SchemaTable.uid AND ParentPath.depth = 1) WHERE TreePath.ancestor = :ancestor ${options?.includeAsyncNode ? '' : 'AND (NodeInfo.async != true )'} @@ -269,7 +287,7 @@ export class UiSchemaRepository extends Repository { const parentChildrenCount = countResult[0]['count']; if (parentChildrenCount == 1) { - const schema = await db.getRepository('ui_schemas').findOne({ + const schema = await db.getRepository('uiSchemas').findOne({ filter: { uid: parent.get('ancestor') as string, }, @@ -311,11 +329,11 @@ export class UiSchemaRepository extends Repository { transaction = await this.database.sequelize.transaction(); } - const treePathTable = this.treeCollection().model.tableName; + const treePathTable = this.uiSchemaTreePathTableName; try { 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 ) `, @@ -362,7 +380,7 @@ export class UiSchemaRepository extends Repository { }); 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;`, { type: 'SELECT', replacements: { @@ -503,7 +521,7 @@ export class UiSchemaRepository extends Repository { transaction, }); - const treeTable = treeCollection.model.tableName; + const treeTable = this.uiSchemaTreePathTableName; if (existsNode) { savedNode = existsNode; diff --git a/packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove-schema.ts b/packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove-schema.ts index f43fb10cd..5fcd6e9e3 100644 --- a/packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove-schema.ts +++ b/packages/plugin-ui-schema-storage/src/server-hooks/hooks/remove-schema.ts @@ -2,7 +2,7 @@ import { UiSchemaRepository } from '../../repository'; export async function removeSchema({ schemaInstance, options, db, params }) { const { transaction } = options; - const uiSchemaRepository: UiSchemaRepository = db.getRepository('ui_schemas'); + const uiSchemaRepository: UiSchemaRepository = db.getRepository('uiSchemas'); const uid = schemaInstance.get('uid') as string; if (params?.removeEmptyParents) { diff --git a/packages/plugin-ui-schema-storage/src/server-hooks/index.ts b/packages/plugin-ui-schema-storage/src/server-hooks/index.ts index a474c74c3..f7def2344 100644 --- a/packages/plugin-ui-schema-storage/src/server-hooks/index.ts +++ b/packages/plugin-ui-schema-storage/src/server-hooks/index.ts @@ -1,6 +1,6 @@ import { Database } from '@nocobase/database'; -import { ServerHookModel } from './model'; import { hooks } from './hooks'; +import { ServerHookModel } from './model'; export type HookType = | 'onSelfDestroy' @@ -31,7 +31,7 @@ export class ServerHooks { 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); }); } diff --git a/packages/plugin-ui-schema-storage/src/server.ts b/packages/plugin-ui-schema-storage/src/server.ts index ca58f7c1b..9ee2378b2 100644 --- a/packages/plugin-ui-schema-storage/src/server.ts +++ b/packages/plugin-ui-schema-storage/src/server.ts @@ -26,16 +26,16 @@ export class UiSchemaStoragePlugin extends Plugin { this.registerRepository(); - db.on('ui_schemas.beforeCreate', function setUid(model) { + db.on('uiSchemas.beforeCreate', function setUid(model) { model.set('uid', model.get('x-uid')); if (!model.get('name')) { 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 uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; + const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository; 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 uiSchemaRepository = db.getCollection('ui_schemas').repository as UiSchemaRepository; + const uiSchemaRepository = db.getCollection('uiSchemas').repository as UiSchemaRepository; await uiSchemaRepository.patch(model.toJSON(), { transaction, @@ -58,7 +58,7 @@ export class UiSchemaStoragePlugin extends Plugin { }); this.app.resourcer.define({ - name: 'ui_schemas', + name: 'uiSchemas', actions: uiSchemaActions, }); }