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

feature: query filter support for common SQL keywords #2366

Conversation

michael-genson
Copy link
Collaborator

@michael-genson michael-genson commented Apr 29, 2023

What type of PR is this?

(REQUIRED)

  • feature

What this PR does / why we need it:

(REQUIRED)

This extends our pagination query filter parsing to include various common SQL keywords:

  • IS (only in conjunction with NULL)
  • IS NOT (only in conjunction with NULL)
  • IN
  • NOT IN
  • LIKE
  • NOT LIKE

And an additional "fake" keyword:

  • CONTAINS ALL

To accomplish this, two major changes were made:

  1. The filter string parser needed additions to correctly understand lists (i.e. values between brackets)
  2. The query builder was completely redone to leverage SQL Alchemy's ORM. Previously it constructed the raw (parametrized) SQL text

With these changes, you can make some powerful queries. Here are a few example Recipe queries:

  • recipeCategory.name IN ["American", "German"]
  • tags.name CONTAINS ALL ["soup", "chicken", "easy"]
  • tags.name NOT IN ["small", "medium"]

You can also completely replace the custom filters we made for frontend filtering (more on that below):

  • recipeCategory.id CONTAINS ALL ["category-id-1", "category-id-2"] AND tags.id IN ["tag-id-1", "tag-id-2", "tag-id-3"] AND tools.id IN ["tool-id-1"]

Which issue(s) this PR fixes:

(REQUIRED)

N/A

Special notes for your reviewer:

(fill-in or delete this section)

I always hated the workaround I did in #1508 and #1560 to get filters working for categories/tags/tools, but the existing parser couldn't handle joins (e.g. recipe.tag.id) and assumed all values were singular (rather than lists).

There were a few other parser challenges, but those were the two main ones. I fixed the joins in #2265 and fixed the remaining issues here.

Testing

(fill-in or delete this section)

I added a bunch of tests prior to the refactor, then more after the refactor. God bless tests.

Once this PR is accepted, there are two tasks that should be done sooner or later:

  1. Deprecate the old category/tag/tool filter params I made because they're bad
  2. Update the documentation to include examples with the new keywords

Release Notes

(REQUIRED)

improved the backend filter API to support additional SQL keywords

@hay-kot hay-kot self-requested a review May 6, 2023 18:02
hay-kot
hay-kot previously approved these changes May 6, 2023
@hay-kot hay-kot merged commit 5d87b7e into mealie-recipes:mealie-next May 6, 2023
@michael-genson michael-genson deleted the feat/filter-support-for-sql-keywords branch May 6, 2023 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants