diff --git a/packages/core/client/src/data-source/__tests__/collection/Collection.test.tsx b/packages/core/client/src/data-source/__tests__/collection/Collection.test.tsx index b929f8ffc5..020e68afc4 100644 --- a/packages/core/client/src/data-source/__tests__/collection/Collection.test.tsx +++ b/packages/core/client/src/data-source/__tests__/collection/Collection.test.tsx @@ -181,6 +181,23 @@ describe('Collection', () => { }); }); + describe('getFilterTargetKey()', () => { + test('not set as id', () => { + const collection = getCollection({ name: 'test' }); + expect(collection.getFilterTargetKey()).toBe('id'); + }); + + test('single ftk', () => { + const collection = getCollection({ name: 'test', filterTargetKey: 'a' }); + expect(collection.getFilterTargetKey()).toBe('a'); + }); + + test('multiple ftk', () => { + const collection = getCollection({ name: 'test', filterTargetKey: ['a', 'b'] }); + expect(collection.getFilterTargetKey()).toMatchObject(['a', 'b']); + }); + }); + test('properties', () => { const app = new Application({ dataSourceManager: { diff --git a/packages/core/client/src/data-source/collection/Collection.ts b/packages/core/client/src/data-source/collection/Collection.ts index 7eaf1daf9f..b8cdc04525 100644 --- a/packages/core/client/src/data-source/collection/Collection.ts +++ b/packages/core/client/src/data-source/collection/Collection.ts @@ -164,6 +164,9 @@ export class Collection { return this.primaryKey; } + getFilterTargetKey() { + return this.filterTargetKey || this.getPrimaryKey() || 'id'; + } get inherits() { return this.options.inherits || []; diff --git a/packages/core/client/src/data-source/collection/CollectionManager.ts b/packages/core/client/src/data-source/collection/CollectionManager.ts index 90bf4f23d5..dc0ba90ff4 100644 --- a/packages/core/client/src/data-source/collection/CollectionManager.ts +++ b/packages/core/client/src/data-source/collection/CollectionManager.ts @@ -164,7 +164,6 @@ export class CollectionManager { ); return; } - const getTargetKey = (collection: Collection) => collection.filterTargetKey || collection.getPrimaryKey() || 'id'; const buildFilterByTk = (targetKey: string | string[], record: Record) => { if (Array.isArray(targetKey)) { @@ -179,7 +178,7 @@ export class CollectionManager { }; if (collectionOrAssociation instanceof Collection) { - const targetKey = getTargetKey(collectionOrAssociation); + const targetKey = collectionOrAssociation.getFilterTargetKey(); return buildFilterByTk(targetKey, collectionRecordOrAssociationRecord); } @@ -204,7 +203,7 @@ export class CollectionManager { ); return; } - const targetKey = getTargetKey(targetCollection); + const targetKey = targetCollection.getFilterTargetKey(); return buildFilterByTk(targetKey, collectionRecordOrAssociationRecord); }