fix: collection field create

This commit is contained in:
Chareice 2024-01-26 12:00:11 +08:00
parent 1f84914342
commit 16e79ce196
No known key found for this signature in database
4 changed files with 48 additions and 22 deletions

View File

@ -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();
});
});
});

View File

@ -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);
}

View File

@ -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';

View File

@ -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,
},
});