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
This commit is contained in:
Zeke Zhang 2023-12-22 09:52:28 +08:00 committed by GitHub
parent 5e89a02044
commit d570a2c704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -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",

View File

@ -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: {

View File

@ -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);