From 5d974d7e32c0e02dc15a4dc148d82a9b6896f4ec Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Mon, 28 Feb 2022 22:29:18 +0800 Subject: [PATCH] fix: empty operator with $or filter (#212) --- .../__tests__/operator/empty-operator.test.ts | 28 +++++++++++++++++++ packages/database/src/operators/empty.ts | 6 ++++ 2 files changed, 34 insertions(+) diff --git a/packages/database/src/__tests__/operator/empty-operator.test.ts b/packages/database/src/__tests__/operator/empty-operator.test.ts index 6f3b98cbe..e46b870fc 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 627b0893f..6e36767a7 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);