From caf61cab7205209f6744c9c0f8f1b9c53627c08d Mon Sep 17 00:00:00 2001 From: Michael Ross Date: Tue, 27 Feb 2024 11:23:12 -0600 Subject: [PATCH 1/2] Added optional "referencedTable" parameter to OR PostGREST filter --- Sources/PostgREST/PostgrestFilterBuilder.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/PostgREST/PostgrestFilterBuilder.swift b/Sources/PostgREST/PostgrestFilterBuilder.swift index 7781eef7f..6e23243ed 100644 --- a/Sources/PostgREST/PostgrestFilterBuilder.swift +++ b/Sources/PostgREST/PostgrestFilterBuilder.swift @@ -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 } From cf82f85bab057974bf955197e2430a1a95d14a37 Mon Sep 17 00:00:00 2001 From: Michael Ross Date: Wed, 28 Feb 2024 17:42:34 -0600 Subject: [PATCH 2/2] Added test case --- Tests/PostgRESTTests/BuildURLRequestTests.swift | 5 +++++ ...testBuildRequest.test-or-filter-with-referenced-table.txt | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildRequest.test-or-filter-with-referenced-table.txt diff --git a/Tests/PostgRESTTests/BuildURLRequestTests.swift b/Tests/PostgRESTTests/BuildURLRequestTests.swift index 89b23c9a2..1a0ae87bb 100644 --- a/Tests/PostgRESTTests/BuildURLRequestTests.swift +++ b/Tests/PostgRESTTests/BuildURLRequestTests.swift @@ -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: "johndoe@supabase.io")) diff --git a/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildRequest.test-or-filter-with-referenced-table.txt b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildRequest.test-or-filter-with-referenced-table.txt new file mode 100644 index 000000000..7f79742ce --- /dev/null +++ b/Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildRequest.test-or-filter-with-referenced-table.txt @@ -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(*)" \ No newline at end of file