fix: filter external data source collections (#5890)

* fix: filter external data source collections

* fix: test
This commit is contained in:
ChengLei Shao 2024-12-16 09:25:21 +08:00 committed by GitHub
parent 6ff60c1b8a
commit fbe63b1712
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 8 deletions

View File

@ -8,6 +8,7 @@
*/ */
import moment from 'moment'; import moment from 'moment';
export function filterMatch(model, where) { export function filterMatch(model, where) {
// Create an object that maps operator names to functions // Create an object that maps operator names to functions
const operatorFunctions = { const operatorFunctions = {
@ -43,6 +44,9 @@ export function filterMatch(model, where) {
$in: (value, condition) => condition.includes(value), $in: (value, condition) => condition.includes(value),
$or: (model, conditions) => Object.values(conditions).some((condition) => filterMatch(model, condition)), $or: (model, conditions) => Object.values(conditions).some((condition) => filterMatch(model, condition)),
$and: (model, conditions) => Object.values(conditions).every((condition) => filterMatch(model, condition)), $and: (model, conditions) => Object.values(conditions).every((condition) => filterMatch(model, condition)),
// boolean
$isFalsy: (value) => !value,
}; };
for (const [key, value] of Object.entries(where)) { for (const [key, value] of Object.entries(where)) {

View File

@ -8,6 +8,7 @@
*/ */
import lodash from 'lodash'; import lodash from 'lodash';
import { filterMatch } from '@nocobase/database';
export default { export default {
name: 'dataSources.collections', name: 'dataSources.collections',
@ -44,17 +45,11 @@ export default {
throw new Error(`dataSource ${dataSourceKey} not found`); throw new Error(`dataSource ${dataSourceKey} not found`);
} }
const { paginate, filter } = ctx.action.params; const { paginate, filter = {} } = ctx.action.params;
const filterTitle = lodash.get(filter, '$and.0.title.$includes')?.toLowerCase();
const filterName = lodash.get(filter, '$and.0.name.$includes')?.toLowerCase();
const collections = lodash.sortBy( const collections = lodash.sortBy(
dataSource.collectionManager.getCollections().filter((collection) => { dataSource.collectionManager.getCollections().filter((collection) => {
return ( return filterMatch(collection.options, filter);
(!filterTitle || lodash.get(collection, 'options.title')?.toLowerCase().includes(filterTitle)) &&
(!filterName || collection.options.name.toLowerCase().includes(filterName))
);
}), }),
'name', 'name',
); );