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

Passing subqueries to filter #403

Open
JaapWijnen opened this issue Nov 2, 2020 · 1 comment
Open

Passing subqueries to filter #403

JaapWijnen opened this issue Nov 2, 2020 · 1 comment

Comments

@JaapWijnen
Copy link
Contributor

This could be a nice addition (might apply to more places than just .filter())
Ie passing queries as arguments to functions such as filter and sort.

let pivotsWeDontWant = [pivotID1, pivotID2, pivotID3]
// grabs pivotIDs we want to exclude from final query
let subquery = PivotModel.query(on: db).field(\.$mainModel.$id)filter(\.$id ~~ pivotsWeDontWant) 
// passes the subquery to a filter to exclude the collected IDs
let query = MainModel.query(on: db).filter(\.$id !~ subquery)
@fananek
Copy link
Contributor

fananek commented Mar 4, 2021

It is actually possible to pass filters, sorts... etc. as arguments.

func allFiltered(_ filters: [DatabaseQuery.Filter]) -> EventLoopFuture<[MyModel]> {
      let query = MyModel(on: database)
      for filter in filters {
          query.filter(filter)
      }
      return query.all()
}

Check this gist for detailed example. Please note it uses repository pattern. Without a repository it's quite straightforward.

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

No branches or pull requests

2 participants