From 16e79ce196098032820dd680046d70f10823652c Mon Sep 17 00:00:00 2001 From: Chareice Date: Fri, 26 Jan 2024 12:00:11 +0800 Subject: [PATCH] fix: collection field create --- .../src/server/__tests__/data-sources.test.ts | 38 +++++++++++++++++-- .../src/server/models/remote-field-model.ts | 4 +- .../src/server/plugin.ts | 2 +- ....ts => data-sources-collections-fields.ts} | 26 ++++++------- 4 files changed, 48 insertions(+), 22 deletions(-) rename packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/{remote-fields.ts => data-sources-collections-fields.ts} (76%) diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-sources.test.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-sources.test.ts index 923b16f040..8a99f49d42 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-sources.test.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/__tests__/data-sources.test.ts @@ -70,10 +70,6 @@ describe('data source', async () => { type: 'string', name: 'content', }, - { - type: 'belongsTo', - name: 'post', - }, ], }); } @@ -156,5 +152,39 @@ describe('data source', async () => { const field = collection.getField('title'); expect(field.options.title).toBe('标题 Field'); }); + + it('should create collection field', async () => { + const dataSource = app.dataSourceManager.dataSources.get('mockInstance1'); + const collection = dataSource.collectionManager.getCollection('comments'); + + expect(collection.getField('post')).toBeFalsy(); + + const createResp = await app + .agent() + .resource('dataSourcesCollections.fields', 'mockInstance1.comments') + .create({ + values: { + type: 'belongsTo', + name: 'post', + target: 'posts', + foreignKey: 'post_id', + }, + }); + + expect(createResp.status).toBe(200); + + expect(collection.getField('post')).toBeTruthy(); + + // destroy field + const destroyResp = await app + .agent() + .resource('dataSourcesCollections.fields', 'mockInstance1.comments') + .destroy({ + filterByTk: 'post', + }); + + expect(destroyResp.status).toBe(200); + expect(collection.getField('post')).toBeFalsy(); + }); }); }); diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/remote-field-model.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/remote-field-model.ts index e727921cbe..fbd6af1466 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/remote-field-model.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/models/remote-field-model.ts @@ -37,8 +37,8 @@ export class RemoteFieldModel extends MagicAttributeModel { unload(loadOptions: LoadOptions) { const { app } = loadOptions; const options = this.get(); - const { collectionName, name, datasSourceKey } = options; - const dataSource = app.dataSourceManager.dataSources.get(datasSourceKey); + const { collectionName, name, dataSourceKey } = options; + const dataSource = app.dataSourceManager.dataSources.get(dataSourceKey); const collection = dataSource.collectionManager.getCollection(collectionName); collection.removeField(name); } diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts index 75d90c04d2..413362133b 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/plugin.ts @@ -3,7 +3,7 @@ import { resolve } from 'path'; import { DatabaseConnectionModel } from './models/database-connection'; import { Database } from '@nocobase/database'; import remoteCollectionsResourcer from './resourcers/data-sources-collections'; -import remoteFieldsResourcer from './resourcers/remote-fields'; +import remoteFieldsResourcer from './resourcers/data-sources-collections-fields'; import { RemoteCollectionModel } from './models/remote-collection-model'; import { RemoteFieldModel } from './models/remote-field-model'; import { rolesRemoteCollectionsResourcer } from './resourcers/roles-remote-collections'; diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/remote-fields.ts b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/data-sources-collections-fields.ts similarity index 76% rename from packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/remote-fields.ts rename to packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/data-sources-collections-fields.ts index 34083b8f0f..a0d95a0f51 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/remote-fields.ts +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/server/resourcers/data-sources-collections-fields.ts @@ -1,5 +1,3 @@ -import { Database } from '@nocobase/database'; - export default { name: 'dataSourcesCollections.fields', actions: { @@ -66,17 +64,16 @@ export default { }, async create(ctx, next) { - const databaseName = ctx.get('x-database'); - const mainDb = ctx.app.getDb() as Database; - const params = ctx.action.params; + const { associatedIndex: collectionNameWithDataSourceKey, values } = ctx.action.params; + const [dataSourceKey, collectionName] = collectionNameWithDataSourceKey.split('.'); - const { associatedIndex: collectionName } = params; + const mainDb = ctx.app.db; - const fieldRecord = await mainDb.getRepository('remoteFields').create({ + const fieldRecord = await mainDb.getRepository('dataSourcesFields').create({ values: { - ...params.values, + ...values, collectionName, - connectionName: databaseName, + dataSourceKey, }, }); @@ -86,17 +83,16 @@ export default { }, async destroy(ctx, next) { - const databaseName = ctx.get('x-database'); - const mainDb = ctx.app.getDb() as Database; - const params = ctx.action.params; + const { associatedIndex: collectionNameWithDataSourceKey, filterByTk: name } = ctx.action.params; + const [dataSourceKey, collectionName] = collectionNameWithDataSourceKey.split('.'); - const { associatedIndex: collectionName, filterByTk: name } = params; + const mainDb = ctx.app.db; - const fieldRecord = await mainDb.getRepository('remoteFields').findOne({ + const fieldRecord = await mainDb.getRepository('dataSourcesFields').findOne({ filter: { name, collectionName, - connectionName: databaseName, + dataSourceKey, }, });