Skip to content

Commit

Permalink
feat: Add optional "referencedTable" parameter to OR PostGREST filter (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpross512 authored Feb 29, 2024
1 parent 328430a commit c06aa18
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Sources/PostgREST/PostgrestFilterBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public class PostgrestFilterBuilder: PostgrestTransformBuilder {
return self
}

public func or(_ filters: URLQueryRepresentable) -> PostgrestFilterBuilder {
public func or(_ filters: URLQueryRepresentable, referencedTable: String? = nil) -> PostgrestFilterBuilder {
let key = referencedTable.map { "\($0).or" } ?? "or"
mutableState.withValue {
$0.request.query.append(URLQueryItem(name: "or", value: "(\(filters.queryValue.queryValue))"))
$0.request.query.append(URLQueryItem(name: key, value: "(\(filters.queryValue.queryValue))"))
}
return self
}
Expand Down
5 changes: 5 additions & 0 deletions Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ final class BuildURLRequestTests: XCTestCase {
.select()
.contains("name", value: ["is:online", "faction:red"])
},
TestCase(name: "test or filter with referenced table") { client in
await client.from("users")
.select("*, messages(*)")
.or("public.eq.true,recipient_id.eq.1", referencedTable: "messages")
},
TestCase(name: "test upsert not ignoring duplicates") { client in
try await client.from("users")
.upsert(User(email: "[email protected]"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--header "X-Client-Info: postgrest-swift/x.y.z" \
"https://example.supabase.co/users?messages.or=(public.eq.true,recipient_id.eq.1)&select=*,messages(*)"

0 comments on commit c06aa18

Please sign in to comment.