mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-11-29 18:58:26 +08:00
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:
parent
4a62b6798f
commit
06e7617691
@ -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: {
|
||||
|
@ -164,6 +164,9 @@ export class Collection {
|
||||
|
||||
return this.primaryKey;
|
||||
}
|
||||
getFilterTargetKey() {
|
||||
return this.filterTargetKey || this.getPrimaryKey() || 'id';
|
||||
}
|
||||
|
||||
get inherits() {
|
||||
return this.options.inherits || [];
|
||||
|
@ -164,7 +164,6 @@ export class CollectionManager {
|
||||
);
|
||||
return;
|
||||
}
|
||||
const getTargetKey = (collection: Collection) => collection.filterTargetKey || collection.getPrimaryKey() || 'id';
|
||||
|
||||
const buildFilterByTk = (targetKey: string | string[], record: Record<string, any>) => {
|
||||
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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user