diff --git a/packages/database/src/__tests__/operator/empty-operator.test.ts b/packages/database/src/__tests__/operator/empty-operator.test.ts index 6f3b98cbed..e46b870fc6 100644 --- a/packages/database/src/__tests__/operator/empty-operator.test.ts +++ b/packages/database/src/__tests__/operator/empty-operator.test.ts @@ -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); + }); }); diff --git a/packages/database/src/operators/empty.ts b/packages/database/src/operators/empty.ts index 627b0893f1..6e36767a7a 100644 --- a/packages/database/src/operators/empty.ts +++ b/packages/database/src/operators/empty.ts @@ -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);