Alex Andrews
Alex Andrews

Reputation: 1498

Chaining Multiple Query Filters in Vapor - Server Side Swift

We can apply a simple query filter in vapor with:

// User is my model object connecting corresponding MySQL database table
let aUser = try User.query().filter("user_email", "[email protected]")

How can we chain multiple query filters with AND or OR conditions like we do in SQL queries?

Say for example, if we need to join filter("user_email", "[email protected]") and filter("user_password", "123456") with an AND condition, how can we achieve that?

Upvotes: 1

Views: 1542

Answers (1)

procra
procra

Reputation: 555

As I know the .filter function throws a bool after checking the condition to filter. So you can try to combine two conditions with the logical AND-operator. For a proper solution it would be good to know the structure and properties of your object User: but a suggestion would be:

    let aUser = User.query().filter { condition1 && condition2 }

Filter would only pass an element of User.query() to aUser, if BOTH conditions are true. Make sure, that User.query() is an array and that you refer to the current object with $0 in your conditions. The filter function will also return an Array.

Upvotes: 2

Related Questions