fix: filter parser with number in key (#219)

* fix: filter parser with number in key

* fix:test
This commit is contained in:
ChengLei Shao 2022-03-03 22:02:15 +08:00 committed by GitHub
parent 2efbe84fa6
commit b4dc7f6199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 2 deletions

View File

@ -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: {

View File

@ -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);
});
});
});

View File

@ -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: [],