From ad75debeaa49b126402ccdb2351e92b9233ab40e Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 14 Apr 2024 10:51:14 +0800 Subject: [PATCH] fix: array operator with camel case field (#4032) * fix: array operator with camel case field * fix: test * fix: test --- .../database/src/__tests__/filter.test.ts | 21 +++++++++++++++++++ .../__tests__/operator/array-operator.test.ts | 10 ++++----- packages/core/database/src/operators/array.ts | 8 ++++--- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/core/database/src/__tests__/filter.test.ts b/packages/core/database/src/__tests__/filter.test.ts index 6454f0dbd3..406b84dd8a 100644 --- a/packages/core/database/src/__tests__/filter.test.ts +++ b/packages/core/database/src/__tests__/filter.test.ts @@ -14,6 +14,27 @@ describe('filter', () => { await db.close(); }); + it('should filter by field in camel case', async () => { + const UserCollection = db.collection({ + name: 'users', + fields: [{ type: 'array', name: 'referralSource' }], + }); + + await db.sync(); + + await UserCollection.repository.create({ + values: { + referralSource: ['a', 'b'], + }, + }); + + const usersCount = await UserCollection.repository.count({ + filter: { $and: [{ referralSource: { $notEmpty: true } }] }, + }); + + expect(usersCount).toBe(1); + }); + it('should filter by belongs to many association', async () => { const A = db.collection({ name: 'a', 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 c7f98bc5da..0ffbefa62a 100644 --- a/packages/core/database/src/__tests__/operator/array-operator.test.ts +++ b/packages/core/database/src/__tests__/operator/array-operator.test.ts @@ -44,7 +44,7 @@ describe('array field operator', function () { test('array field update', async () => { const Post = db.collection({ name: 'posts', - fields: [{ type: 'array', name: 'tags' }], + fields: [{ type: 'array', name: 'tagsFields' }], }); await db.sync({ force: true }); @@ -52,13 +52,13 @@ describe('array field operator', function () { await Post.repository.create({}); const p1 = await Post.repository.create({ values: { - tags: ['t1', 't2'], + tagsFields: ['t1', 't2'], }, }); let result = await Post.repository.findOne({ filter: { - 'tags.$match': ['t2', 't1'], + 'tagsFields.$match': ['t2', 't1'], }, }); @@ -67,13 +67,13 @@ describe('array field operator', function () { await Post.repository.update({ filterByTk: p1.get('id'), values: { - tags: ['t3', 't2'], + tagsFields: ['t3', 't2'], }, }); result = await Post.repository.findOne({ filter: { - 'tags.$match': ['t3', 't2'], + 'tagsFields.$match': ['t3', 't2'], }, }); diff --git a/packages/core/database/src/operators/array.ts b/packages/core/database/src/operators/array.ts index fc4d2703cd..72b57cdb3d 100644 --- a/packages/core/database/src/operators/array.ts +++ b/packages/core/database/src/operators/array.ts @@ -3,8 +3,7 @@ import { Op, Sequelize } from 'sequelize'; import { isMySQL, isPg } from './utils'; const getFieldName = (ctx) => { - const fieldName = ctx.fieldName; - return fieldName; + return ctx.model.rawAttributes[ctx.fieldName]?.field || ctx.fieldName; }; const escape = (value, ctx) => { @@ -43,11 +42,14 @@ const emptyQuery = (ctx, operator: '=' | '>') => { funcName = 'json_length'; } - return `(select ${ifNull}(${funcName}(${fieldName}), 0) ${operator} 0)`; + const queryInterface = getQueryInterface(ctx); + + return `(select ${ifNull}(${funcName}(${queryInterface.quoteIdentifier(fieldName)}), 0) ${operator} 0)`; }; export default { $match(value, ctx) { + const queryInterface = getQueryInterface(ctx); const fieldName = getFieldName(ctx); if (isPg(ctx)) {