Skip to content

Commit

Permalink
Fix upsert (supabase-community#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev authored Jul 14, 2023
1 parent 737dc52 commit aaf7ef2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions Sources/PostgREST/PostgrestQueryBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ public final class PostgrestQueryBuilder: PostgrestBuilder {
onConflict: String? = nil,
returning: PostgrestReturningOptions = .representation,
count: CountOption? = nil,
ignoreDuplicates: Bool? = nil
ignoreDuplicates: Bool = false
) -> PostgrestFilterBuilder {
method = "POST"
var prefersHeaders = [
ignoreDuplicates.map { "resolution=\($0 ? "ignore" : "merge")-duplicates" },
"resolution=\(ignoreDuplicates ? "ignore" : "merge")-duplicates",
"return=\(returning.rawValue)",
]
.compactMap { $0 }
if let onConflict = onConflict {
appendSearchParams(name: "on_conflict", value: onConflict)
}
Expand Down
8 changes: 8 additions & 0 deletions Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@
.select()
.contains(column: "name", value: ["is:online", "faction:red"])
},
TestCase(name: "test upsert not ignoring duplicates") { client in
client.from("users")
.upsert(values: ["email": "[email protected]"])
},
TestCase(name: "test upsert ignoring duplicates") { client in
client.from("users")
.upsert(values: ["email": "[email protected]"], ignoreDuplicates: true)
},
]

for testCase in testCases {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curl \
--request POST \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--header "Prefer: resolution=ignore-duplicates,return=representation" \
--data "{\"email\":\"[email protected]\"}" \
"https://example.supabase.co/users"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curl \
--request POST \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--header "Prefer: resolution=merge-duplicates,return=representation" \
--data "{\"email\":\"[email protected]\"}" \
"https://example.supabase.co/users"

0 comments on commit aaf7ef2

Please sign in to comment.