fix: empty operator with $or filter (#212)
This commit is contained in:
		
							parent
							
								
									9704f8a342
								
							
						
					
					
						commit
						5d974d7e32
					
				@ -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);
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user