Originally created by @zachwaugh on GitHub (Mar 26, 2020).
I'm wondering the best way to build a query with conditionals. It looks something like this where we optional want to limit the query based on some state.
That seems to work, but feels like maybe I'm missing a simpler way? Also, this is a contrived example, but I'd like to be able to pass in an existing FetchChainBuilder<Post> from elsewhere and append Where clauses using AND as well. Ideally, we'd have an API like:
Originally created by @zachwaugh on GitHub (Mar 26, 2020).
I'm wondering the best way to build a query with conditionals. It looks something like this where we optional want to limit the query based on some state.
```swift
var query = From<Post>()
if unseen {
query = query.where(\.unread == true)
}
if let date = updatedSince {
query = query.where(\.updatedAt > date)
}
```
That will do an `OR`, but I want it to be an `AND`. The closest I've found is doing this:
```swift
var query = From<Post>()
var filter = Where<Post>()
if unseen {
filter = filter && Where(\.unread == true)
}
if let date = updatedSince {
filter = filter && Where(\.updatedAt > date)
}
query.where(filter)
```
That seems to work, but feels like maybe I'm missing a simpler way? Also, this is a contrived example, but I'd like to be able to pass in an existing `FetchChainBuilder<Post>` from elsewhere and append `Where` clauses using `AND` as well. Ideally, we'd have an API like:
```swift
func filter(query: FetchChainBuilder<Post>) -> FetchChainBuilder<Post> {
var filters: [Where<Post>()] = []
if unseen {
filters.append(Where(\.unread == true))
}
if let date = updatedSince {
filters.append(Where(\.updatedAt > date))
}
return query.where(filters)
}
```
Thanks!
adam
added the question label 2025-12-29 15:28:50 +01:00
@JohnEstropia commented on GitHub (Mar 27, 2020):
You can use an array of `Where` clauses:
```swift
var filters: [Where<Post>] = []
if unseen {
filters.append(\.unread == true)
}
if let date = updatedSince {
filters.append(\.updatedAt > date)
}
return query.where(fitlers.combinedByAnd())
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @zachwaugh on GitHub (Mar 26, 2020).
I'm wondering the best way to build a query with conditionals. It looks something like this where we optional want to limit the query based on some state.
That will do an
OR, but I want it to be anAND. The closest I've found is doing this:That seems to work, but feels like maybe I'm missing a simpler way? Also, this is a contrived example, but I'd like to be able to pass in an existing
FetchChainBuilder<Post>from elsewhere and appendWhereclauses usingANDas well. Ideally, we'd have an API like:Thanks!
@JohnEstropia commented on GitHub (Mar 27, 2020):
You can use an array of
Whereclauses:@zachwaugh commented on GitHub (Mar 27, 2020):
Wow, that’s exactly what I was looking for, not sure I how I missed it. Thank you!