fix: array operator with camel case field (#4032)

* fix: array operator with camel case field

* fix: test

* fix: test
This commit is contained in:
ChengLei Shao 2024-04-14 10:51:14 +08:00 committed by GitHub
parent ed26c2ebea
commit ad75debeaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 8 deletions

View File

@ -14,6 +14,27 @@ describe('filter', () => {
await db.close(); 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 () => { it('should filter by belongs to many association', async () => {
const A = db.collection({ const A = db.collection({
name: 'a', name: 'a',

View File

@ -44,7 +44,7 @@ describe('array field operator', function () {
test('array field update', async () => { test('array field update', async () => {
const Post = db.collection({ const Post = db.collection({
name: 'posts', name: 'posts',
fields: [{ type: 'array', name: 'tags' }], fields: [{ type: 'array', name: 'tagsFields' }],
}); });
await db.sync({ force: true }); await db.sync({ force: true });
@ -52,13 +52,13 @@ describe('array field operator', function () {
await Post.repository.create({}); await Post.repository.create({});
const p1 = await Post.repository.create({ const p1 = await Post.repository.create({
values: { values: {
tags: ['t1', 't2'], tagsFields: ['t1', 't2'],
}, },
}); });
let result = await Post.repository.findOne({ let result = await Post.repository.findOne({
filter: { filter: {
'tags.$match': ['t2', 't1'], 'tagsFields.$match': ['t2', 't1'],
}, },
}); });
@ -67,13 +67,13 @@ describe('array field operator', function () {
await Post.repository.update({ await Post.repository.update({
filterByTk: <any>p1.get('id'), filterByTk: <any>p1.get('id'),
values: { values: {
tags: ['t3', 't2'], tagsFields: ['t3', 't2'],
}, },
}); });
result = await Post.repository.findOne({ result = await Post.repository.findOne({
filter: { filter: {
'tags.$match': ['t3', 't2'], 'tagsFields.$match': ['t3', 't2'],
}, },
}); });

View File

@ -3,8 +3,7 @@ import { Op, Sequelize } from 'sequelize';
import { isMySQL, isPg } from './utils'; import { isMySQL, isPg } from './utils';
const getFieldName = (ctx) => { const getFieldName = (ctx) => {
const fieldName = ctx.fieldName; return ctx.model.rawAttributes[ctx.fieldName]?.field || ctx.fieldName;
return fieldName;
}; };
const escape = (value, ctx) => { const escape = (value, ctx) => {
@ -43,11 +42,14 @@ const emptyQuery = (ctx, operator: '=' | '>') => {
funcName = 'json_length'; 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 { export default {
$match(value, ctx) { $match(value, ctx) {
const queryInterface = getQueryInterface(ctx);
const fieldName = getFieldName(ctx); const fieldName = getFieldName(ctx);
if (isPg(ctx)) { if (isPg(ctx)) {