fix: filter by association field with underscored (#1537)
* fix: filter by association field with underscored * fix: test
This commit is contained in:
parent
3904aa7c11
commit
104be20c60
60
packages/core/database/src/__tests__/filter.test.ts
Normal file
60
packages/core/database/src/__tests__/filter.test.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
@ -178,7 +178,7 @@ export default class FilterParser {
|
|||||||
origins.push(attr);
|
origins.push(attr);
|
||||||
// if it is target model attribute
|
// if it is target model attribute
|
||||||
if (target.rawAttributes[attr]) {
|
if (target.rawAttributes[attr]) {
|
||||||
associationKeys.push(attr);
|
associationKeys.push(target.rawAttributes[attr].field || attr);
|
||||||
target = null;
|
target = null;
|
||||||
} else if (target.associations[attr]) {
|
} else if (target.associations[attr]) {
|
||||||
// if it is target model association (nested association filter)
|
// if it is target model association (nested association filter)
|
||||||
|
Loading…
Reference in New Issue
Block a user