Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array_type_field_filter: Add new contains filter to search on Array type field #460

Merged
merged 4 commits into from
Oct 6, 2020

Conversation

shubhisood
Copy link
Contributor

@shubhisood shubhisood commented Sep 4, 2020

  • Adds new filter named as contains.
  • This filter will search on Array type fields in Postgres DB.

Include references to all related GitHub issues and other pull requests, for example:

Fixes #342

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • Commit messages are following our guidelines

@shubhisood shubhisood force-pushed the array_type_field_filter branch 2 times, most recently from 5a5d62f to 7566064 Compare September 5, 2020 04:26
@bajtos bajtos added community-contribution Patches contributed by community feature labels Sep 7, 2020
@shubhisood shubhisood force-pushed the array_type_field_filter branch 2 times, most recently from c3f9a69 to 1fcf79a Compare September 14, 2020 16:37
@@ -263,6 +263,18 @@ describe('postgresql connector', function() {
});
});

it('should support where filter for array type field', function(done) {
Post.dataSource.settings.allowExtendedOperators = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After adding this we do not need juggler related PR.
PFB PR Link:
loopbackio/loopback-datasource-juggler#1865

@shubhisood shubhisood force-pushed the array_type_field_filter branch from 1fcf79a to 17f56b7 Compare September 14, 2020 17:46
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how it's possible to implement your desired feature inside the connector only 👍

I have few comments on implementation details, PTAL above.

Please add a new section to README to explain what extended operators are supported (e.g. @>) and what is needed to allow the app to use them (set allowExtendedOperators in model and/or datasource settings).

test/postgresql.test.js Outdated Show resolved Hide resolved
categories: {'@>': ['AA']},
},
]}}, function(err, post) {
should.not.exist(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
should.not.exist(err);
if (err) return done(err);

That way the original error details and the stack trace is preserved.

Copy link
Contributor Author

@shubhisood shubhisood Sep 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How we would be handling this with async await type test case? @bajtos
I was thinking to write test case this way for handling failure of testcase with proper details.

it('should support where filter for array type field', async () => {
      Post.dataSource.settings.allowExtendedOperators = true;
      try {
        const post = await Post.find({where: {
          categories: {'contains': ['AA']},
        }});
        should.exist(post);
        post.length.should.equal(1);
      } catch (err) {
        should.not.exist(err);
      }
    });

Please tell me if it is correct, I will update current test case in this way only.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a rule of thumb, the tests should pass any unexpected error to the test runner:

  • Use done(err) when using callback
  • Use regular error propagation when using async functions
it('should support where filter for array type field', async () => {
      Post.dataSource.settings.allowExtendedOperators = true;
        const post = await Post.find({where: {
          categories: {'contains': ['AA']},
        }});
        should.exist(post);
        post.length.should.equal(1);
    });

test/postgresql.test.js Outdated Show resolved Hide resolved
lib/postgresql.js Outdated Show resolved Hide resolved
@shubhisood shubhisood force-pushed the array_type_field_filter branch from 17f56b7 to ecf676c Compare September 15, 2020 17:53
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new version looks pretty good.

  1. Please change the commit message to use the new operator name

  2. Please add a new section to README to explain what extended operators are supported (e.g. @>) and what is needed to allow the app to use them (set allowExtendedOperators in model and/or datasource settings). Put yourself in the shoes of other LoopBack users - where would you look for information about contains operator and what info would you want to see to help you understand how to use this feature in your app?

@shubhisood shubhisood force-pushed the array_type_field_filter branch from ecf676c to 5fc5d1e Compare September 21, 2020 09:09
@shubhisood
Copy link
Contributor Author

shubhisood commented Sep 21, 2020

@bajtos Thanks for review..

For Point#1 I have done the update.
For Point#2, I have following queries:

  • In which section of readme should I add this operator, As in current readme I couldn't find currently supported operators of postgres in this connector.

@bajtos
Copy link
Member

bajtos commented Sep 21, 2020

In which section of readme should I add this operator, As in current readme I couldn't find currently supported operators of postgres in this connector.

That's a good question. I believe the postgres connector so far supports only built-in LoopBack operators supported by all (or most?) connectors, so there was no need to explicitly list extended operators.

Now that you are implementing the first extended operator, I think you should simply add a new section. You can draw inspiration from MongoDB connector README:

  • The section Additional properties mentions the configuration setting allowExtendedOperators and the extended operators unlocked by this option
  • The section Update operators adds more details

In the case of PostgreSQL and your new contains operator, we can either call the section something like Query operators or perhaps a more generic Extended operators.

@shubhisood
Copy link
Contributor Author

shubhisood commented Sep 21, 2020

In which section of readme should I add this operator, As in current readme I couldn't find currently supported operators of postgres in this connector.

That's a good question. I believe the postgres connector so far supports only built-in LoopBack operators supported by all (or most?) connectors, so there was no need to explicitly list extended operators.

Now that you are implementing the first extended operator, I think you should simply add a new section. You can draw inspiration from MongoDB connector README:

  • The section Additional properties mentions the configuration setting allowExtendedOperators and the extended operators unlocked by this option
  • The section Update operators adds more details

In the case of PostgreSQL and your new contains operator, we can either call the section something like Query operators or perhaps a more generic Extended operators.

@bajtos Added the readme section for the new contains operator, Can you review it...

Implement support for an extended operator `contains` that can
be used to filter records that have an array property containing
the selected items.

Signed-off-by: shubhisood <shubhi.sood@sourcefuse.com>
@bajtos bajtos force-pushed the array_type_field_filter branch from d8be8fc to 55c1003 Compare October 6, 2020 09:35
@bajtos
Copy link
Member

bajtos commented Oct 6, 2020

Thank you @shubhisood for the update. I pushed few more commits to fix the failing builds and improve README content. Let's wait for CI results before landing.

bajtos added 3 commits October 6, 2020 11:42
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
Array properties are represented as juggler `List` instance, we need
to modify test assertions to convert them via `toArray()` before
applying `.should.eql` check.

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
@bajtos bajtos force-pushed the array_type_field_filter branch from 55c1003 to 8f2da81 Compare October 6, 2020 09:44
@bajtos
Copy link
Member

bajtos commented Oct 6, 2020

@slnode test please

@bajtos
Copy link
Member

bajtos commented Oct 6, 2020

@slnode test please

@bajtos bajtos merged commit dfb394b into loopbackio:master Oct 6, 2020
@bajtos
Copy link
Member

bajtos commented Oct 6, 2020

Landed, thank you @shubhisood for the contribution ❤️

@shubhisood
Copy link
Contributor Author

Landed, thank you @shubhisood for the contribution

Thanks for merging @bajtos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Patches contributed by community feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Malformed Array Literal
2 participants