fluent-kit icon indicating copy to clipboard operation
fluent-kit copied to clipboard

Passing subqueries to filter

Open JaapWijnen opened this issue 5 years ago • 1 comments

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)

JaapWijnen avatar Nov 02 '20 14:11 JaapWijnen

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.

fananek avatar Mar 04 '21 21:03 fananek