fix: filter by association field with underscored (#1537)

* fix: filter by association field with underscored

* fix: test
This commit is contained in:
ChengLei Shao 2023-03-05 14:29:40 +08:00 committed by GitHub
parent 3904aa7c11
commit 104be20c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,60 @@
import Database from '../database';
import { mockDatabase } from '../mock-database';
describe('filter', () => {
let db: Database;
beforeEach(async () => {
db = mockDatabase();
await db.clean({ drop: true });
});
afterEach(async () => {
await db.close();
});
it('should filter by association field', async () => {
const UserCollection = db.collection({
name: 'users',
fields: [
{ type: 'string', name: 'name' },
{ type: 'hasMany', name: 'posts' },
],
});
const PostCollection = db.collection({
name: 'posts',
fields: [
{ type: 'string', name: 'title' },
{ type: 'belongsTo', name: 'user' },
],
});
await db.sync();
const user = await UserCollection.repository.create({
values: {
name: 'John',
posts: [
{
title: 'p1',
},
{
title: 'p2',
},
],
},
});
const response = await UserCollection.repository.find({
filter: {
'posts.createdAt': {
$dateOn: user.get('createdAt'),
},
},
});
expect(response).toHaveLength(1);
});
});

View File

@ -178,7 +178,7 @@ export default class FilterParser {
origins.push(attr);
// if it is target model attribute
if (target.rawAttributes[attr]) {
associationKeys.push(attr);
associationKeys.push(target.rawAttributes[attr].field || attr);
target = null;
} else if (target.associations[attr]) {
// if it is target model association (nested association filter)