Skip to content

Commit aaf7ef2

Browse files
authored
Fix upsert (#44)
1 parent 737dc52 commit aaf7ef2

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

Sources/PostgREST/PostgrestQueryBuilder.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,13 @@ public final class PostgrestQueryBuilder: PostgrestBuilder {
7979
onConflict: String? = nil,
8080
returning: PostgrestReturningOptions = .representation,
8181
count: CountOption? = nil,
82-
ignoreDuplicates: Bool? = nil
82+
ignoreDuplicates: Bool = false
8383
) -> PostgrestFilterBuilder {
8484
method = "POST"
8585
var prefersHeaders = [
86-
ignoreDuplicates.map { "resolution=\($0 ? "ignore" : "merge")-duplicates" },
86+
"resolution=\(ignoreDuplicates ? "ignore" : "merge")-duplicates",
8787
"return=\(returning.rawValue)",
8888
]
89-
.compactMap { $0 }
9089
if let onConflict = onConflict {
9190
appendSearchParams(name: "on_conflict", value: onConflict)
9291
}

Tests/PostgRESTTests/BuildURLRequestTests.swift

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@
7878
.select()
7979
.contains(column: "name", value: ["is:online", "faction:red"])
8080
},
81+
TestCase(name: "test upsert not ignoring duplicates") { client in
82+
client.from("users")
83+
.upsert(values: ["email": "[email protected]"])
84+
},
85+
TestCase(name: "test upsert ignoring duplicates") { client in
86+
client.from("users")
87+
.upsert(values: ["email": "[email protected]"], ignoreDuplicates: true)
88+
},
8189
]
8290

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

0 commit comments

Comments
 (0)