From b4dc7f6199ec128c7f82bb80d2e5dc21337f34d8 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Thu, 3 Mar 2022 22:02:15 +0800 Subject: [PATCH] fix: filter parser with number in key (#219) * fix: filter parser with number in key * fix:test --- .../__tests__/operator/array-operator.test.ts | 15 ++++++++++ .../src/__tests__/respsitory/find.test.ts | 29 ++++++++++++++++++- packages/database/src/filter-parser.ts | 4 ++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/database/src/__tests__/operator/array-operator.test.ts b/packages/database/src/__tests__/operator/array-operator.test.ts index e6145b91c..063beb906 100644 --- a/packages/database/src/__tests__/operator/array-operator.test.ts +++ b/packages/database/src/__tests__/operator/array-operator.test.ts @@ -141,6 +141,21 @@ describe('array field operator', function () { expect(filter1[0].get('name')).toEqual(t1.get('name')); }); + test('$match with $and', async () => { + const filter1 = await Test.repository.find({ + filter: { + $and: [ + { + selected: { $match: [2, 1, 'a', 'b'] }, + }, + ], + }, + }); + + expect(filter1.length).toEqual(1); + expect(filter1[0].get('name')).toEqual(t1.get('name')); + }); + test('$notMatch', async () => { const filter2 = await Test.repository.find({ filter: { diff --git a/packages/database/src/__tests__/respsitory/find.test.ts b/packages/database/src/__tests__/respsitory/find.test.ts index 2d6480840..ddb37aef4 100644 --- a/packages/database/src/__tests__/respsitory/find.test.ts +++ b/packages/database/src/__tests__/respsitory/find.test.ts @@ -8,6 +8,7 @@ describe('repository find', () => { let User: Collection; let Post: Collection; let Comment: Collection; + let Tag: Collection; afterEach(async () => { await db.close(); @@ -15,6 +16,7 @@ describe('repository find', () => { beforeEach(async () => { db = mockDatabase(); + User = db.collection<{ id: number; name: string }, { name: string }>({ name: 'users', fields: [ @@ -36,6 +38,11 @@ describe('repository find', () => { type: 'hasMany', name: 'comments', }, + { + type: 'belongsToMany', + name: 'abc1', + target: 'tags', + }, ], }); @@ -47,6 +54,16 @@ describe('repository find', () => { ], }); + Tag = db.collection({ + name: 'tags', + fields: [ + { + type: 'string', + name: 'name', + }, + ], + }); + await db.sync(); const repository = User.repository; @@ -55,7 +72,7 @@ describe('repository find', () => { { name: 'u1', age: 10, - posts: [{ title: 'u1t1', comments: [{ content: 'u1t1c1' }] }], + posts: [{ title: 'u1t1', comments: [{ content: 'u1t1c1' }], abc1: [{ name: 't1' }] }], }, { name: 'u2', @@ -216,5 +233,15 @@ describe('repository find', () => { expect(results.length).toEqual(1); }); + + test('find with association with $and', async () => { + const results = await Post.repository.find({ + filter: { + 'abc1.name': 't1', + }, + }); + + expect(results.length).toEqual(1); + }); }); }); diff --git a/packages/database/src/filter-parser.ts b/packages/database/src/filter-parser.ts index 9dcee2d1b..5a33ea30c 100644 --- a/packages/database/src/filter-parser.ts +++ b/packages/database/src/filter-parser.ts @@ -81,6 +81,7 @@ export default class FilterParser { let skipPrefix = null; const associations = model.associations; + debug('associations %O', associations); for (let [key, value] of Object.entries(flattenedFilter)) { @@ -138,7 +139,7 @@ export default class FilterParser { } // firstKey is number - if (/\d+/.test(firstKey)) { + if (!lodash.isNaN(parseInt(firstKey))) { paths.push(firstKey); continue; } @@ -182,6 +183,7 @@ export default class FilterParser { } assoc.push(associationKey); }); + _.set(include, assoc, { association: attr, attributes: [],