fix: empty logic operator filter (#961)

This commit is contained in:
ChengLei Shao 2022-10-27 09:33:34 +08:00 committed by chenos
parent 6b6951b279
commit afa172a9be
4 changed files with 17 additions and 2 deletions

View File

@ -163,6 +163,16 @@ describe('repository find', () => {
expect(users[1]).toEqual(2);
});
test('find with empty or', async () => {
const usersCount = await User.repository.count({
filter: {
$or: [],
},
});
expect(usersCount).toEqual(await User.repository.count());
});
test('find with fields', async () => {
let user = await User.repository.findOne({
fields: ['name'],

View File

@ -78,7 +78,6 @@ export default class FilterParser {
const include = {};
const where = {};
const filter2 = lodash.cloneDeep(flattenedFilter);
let skipPrefix = null;
const associations = model.associations;
@ -91,6 +90,11 @@ export default class FilterParser {
continue;
}
// skip empty logic operator value
if ((key == '$or' || key == '$and') && Array.isArray(value) && value.length == 0) {
continue;
}
debug('handle filter key "%s: "%s"', key, value);
let keys = key.split('.');

View File

@ -70,6 +70,7 @@ export class OptionsParser {
if (typeof sort === 'string') {
sort = sort.split(',');
}
const orderParams = [];
for (const sortKey of sort) {
let direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';

View File

@ -10,7 +10,7 @@ import {
ModelCtor,
Op,
Transactionable,
UpdateOptions as SequelizeUpdateOptions
UpdateOptions as SequelizeUpdateOptions,
} from 'sequelize';
import { WhereOperators } from 'sequelize/types/lib/model';
import { Collection } from './collection';