From d570a2c704fe7f3c02aabeacae71d967596f24e8 Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Fri, 22 Dec 2023 09:52:28 +0800 Subject: [PATCH] feat: the $anyof and $noneOf operators should support non-array values (#3244) * fix: the $anyof and $noneOf operators should support non-array values * chore: add lodash dependency * test: add unit test * test: add unit test --- packages/core/database/package.json | 3 ++- .../__tests__/operator/array-operator.test.ts | 26 ++++++++++++++++++- packages/core/database/src/operators/array.ts | 3 +++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/core/database/package.json b/packages/core/database/package.json index 708b11745..575b16ad5 100644 --- a/packages/core/database/package.json +++ b/packages/core/database/package.json @@ -23,7 +23,8 @@ "qs": "^6.11.2", "semver": "^7.3.7", "sequelize": "^6.26.0", - "umzug": "^3.1.1" + "umzug": "^3.1.1", + "lodash": "^4.17.21" }, "devDependencies": { "@types/glob": "^7.2.0", diff --git a/packages/core/database/src/__tests__/operator/array-operator.test.ts b/packages/core/database/src/__tests__/operator/array-operator.test.ts index 34f7a11ed..c7f98bc5d 100644 --- a/packages/core/database/src/__tests__/operator/array-operator.test.ts +++ b/packages/core/database/src/__tests__/operator/array-operator.test.ts @@ -1,5 +1,5 @@ -import { mockDatabase } from '../index'; import Database from '../../database'; +import { mockDatabase } from '../index'; describe('array field operator', function () { let db: Database; @@ -195,6 +195,18 @@ describe('array field operator', function () { expect(filter3[0].get('name')).toEqual(t2.get('name')); }); + // fix https://nocobase.height.app/T-2803 + test('$anyOf with string', async () => { + const filter3 = await Test.repository.find({ + filter: { + 'selected.$anyOf': 'aa', + }, + }); + + expect(filter3.length).toEqual(1); + expect(filter3[0].get('name')).toEqual(t2.get('name')); + }); + test('$anyOf with multiple items', async () => { const filter3 = await Test.repository.find({ filter: { @@ -216,6 +228,18 @@ describe('array field operator', function () { expect(filter[0].get('name')).toEqual(t1.get('name')); }); + // fix https://nocobase.height.app/T-2803 + test('$noneOf with string', async () => { + const filter = await Test.repository.find({ + filter: { + 'selected.$noneOf': 'aa', + }, + }); + + expect(filter.length).toEqual(1); + expect(filter[0].get('name')).toEqual(t1.get('name')); + }); + test('$noneOf with null', async () => { const t3 = await Test.repository.create({ values: { diff --git a/packages/core/database/src/operators/array.ts b/packages/core/database/src/operators/array.ts index c17b7220f..fc4d2703c 100644 --- a/packages/core/database/src/operators/array.ts +++ b/packages/core/database/src/operators/array.ts @@ -1,3 +1,4 @@ +import _ from 'lodash'; import { Op, Sequelize } from 'sequelize'; import { isMySQL, isPg } from './utils'; @@ -87,6 +88,7 @@ export default { $anyOf(value, ctx) { const fieldName = getFieldName(ctx); + value = _.castArray(value); if (isPg(ctx)) { const name = ctx.fullName === fieldName ? `"${ctx.model.name}"."${fieldName}"` : `"${fieldName}"`; @@ -111,6 +113,7 @@ export default { $noneOf(value, ctx) { let where; + value = _.castArray(value); if (isPg(ctx)) { const fieldName = getFieldName(ctx);