refactor(client): move getFilterTargetKey api to collection (#5617)

* refactor(client): move getFilterTargetKey api to collection

* test(client): add test case

* fix(client): fix type error when build
This commit is contained in:
Junyi 2024-11-08 18:31:02 +08:00 committed by GitHub
parent 4a62b6798f
commit 06e7617691
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View File

@ -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', () => { test('properties', () => {
const app = new Application({ const app = new Application({
dataSourceManager: { dataSourceManager: {

View File

@ -164,6 +164,9 @@ export class Collection {
return this.primaryKey; return this.primaryKey;
} }
getFilterTargetKey() {
return this.filterTargetKey || this.getPrimaryKey() || 'id';
}
get inherits() { get inherits() {
return this.options.inherits || []; return this.options.inherits || [];

View File

@ -164,7 +164,6 @@ export class CollectionManager {
); );
return; return;
} }
const getTargetKey = (collection: Collection) => collection.filterTargetKey || collection.getPrimaryKey() || 'id';
const buildFilterByTk = (targetKey: string | string[], record: Record<string, any>) => { const buildFilterByTk = (targetKey: string | string[], record: Record<string, any>) => {
if (Array.isArray(targetKey)) { if (Array.isArray(targetKey)) {
@ -179,7 +178,7 @@ export class CollectionManager {
}; };
if (collectionOrAssociation instanceof Collection) { if (collectionOrAssociation instanceof Collection) {
const targetKey = getTargetKey(collectionOrAssociation); const targetKey = collectionOrAssociation.getFilterTargetKey();
return buildFilterByTk(targetKey, collectionRecordOrAssociationRecord); return buildFilterByTk(targetKey, collectionRecordOrAssociationRecord);
} }
@ -204,7 +203,7 @@ export class CollectionManager {
); );
return; return;
} }
const targetKey = getTargetKey(targetCollection); const targetKey = targetCollection.getFilterTargetKey();
return buildFilterByTk(targetKey, collectionRecordOrAssociationRecord); return buildFilterByTk(targetKey, collectionRecordOrAssociationRecord);
} }