fix: empty operator with $or filter (#212)

This commit is contained in:
ChengLei Shao 2022-02-28 22:29:18 +08:00 committed by GitHub
parent 9704f8a342
commit 5d974d7e32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -46,4 +46,32 @@ describe('empty operator', () => {
expect(result.length).toEqual(1);
expect(result[0].get('id')).toEqual(u2.get('id'));
});
test('string empty with or operator ', async () => {
const u1 = await User.repository.create({
values: {
name: 'u1',
},
});
const u2 = await User.repository.create({
values: {
name: '',
},
});
const u3 = await User.repository.create({
values: {
name: 'u3',
},
});
const result = await User.repository.find({
filter: {
$or: [{ 'name.$empty': true }, { name: 'u1' }],
},
});
expect(result.length).toEqual(2);
});
});

View File

@ -1,6 +1,7 @@
import { DataTypes, Op } from 'sequelize';
import { ArrayField, StringField } from '../fields';
import arrayOperators from './array';
import lodash, { parseInt } from 'lodash';
const findFilterFieldType = (ctx) => {
const db = ctx.db;
@ -17,6 +18,10 @@ const findFilterFieldType = (ctx) => {
const associationPath = path;
for (const association of associationPath) {
if (lodash.isNumber(parseInt(association)) || association.startsWith('$')) {
continue;
}
model = model.associations[association].target;
}
@ -24,6 +29,7 @@ const findFilterFieldType = (ctx) => {
return collection.getField(fieldName);
};
export default {
$empty(_, ctx) {
const field = findFilterFieldType(ctx);