Skip to content

Commit

Permalink
fix: add columns query param to insert and upsert methods
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Dec 20, 2023
1 parent 65fd412 commit 7a99c47
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 94 deletions.
42 changes: 0 additions & 42 deletions Sources/PostgREST/NullEncodable.swift

This file was deleted.

11 changes: 11 additions & 0 deletions Sources/PostgREST/PostgrestQueryBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public final class PostgrestQueryBuilder: PostgrestBuilder {
if !prefersHeaders.isEmpty {
$0.request.headers["Prefer"] = prefersHeaders.joined(separator: ",")
}
if let body = $0.request.body, let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]] {
let allKeys = jsonObject.flatMap(\.keys)
let uniqueKeys = Set(allKeys).sorted()
$0.request.query.append(URLQueryItem(name: "columns", value: uniqueKeys.joined(separator: ",")))
}
}

return PostgrestFilterBuilder(self)
Expand Down Expand Up @@ -109,6 +114,12 @@ public final class PostgrestQueryBuilder: PostgrestBuilder {
if !prefersHeaders.isEmpty {
$0.request.headers["Prefer"] = prefersHeaders.joined(separator: ",")
}

if let body = $0.request.body, let jsonObject = try JSONSerialization.jsonObject(with: body) as? [[String: Any]] {
let allKeys = jsonObject.flatMap(\.keys)
let uniqueKeys = Set(allKeys).sorted()
$0.request.query.append(URLQueryItem(name: "columns", value: uniqueKeys.joined(separator: ",")))
}
}
return PostgrestFilterBuilder(self)
}
Expand Down
29 changes: 26 additions & 3 deletions Tests/PostgRESTTests/BuildURLRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import XCTest
import FoundationNetworking
#endif

struct User: Encodable {
var email: String
var username: String?
}

@MainActor
final class BuildURLRequestTests: XCTestCase {
let url = URL(string: "https://example.supabase.co")!
Expand Down Expand Up @@ -55,7 +60,16 @@ final class BuildURLRequestTests: XCTestCase {
},
TestCase(name: "insert new user") { client in
try await client.from("users")
.insert(["email": "[email protected]"])
.insert(User(email: "[email protected]"))
},
TestCase(name: "bulk insert users") { client in
try await client.from("users")
.insert(
[
User(email: "[email protected]"),
User(email: "[email protected]", username: "johndoe2"),
]
)
},
TestCase(name: "call rpc") { client in
try await client.rpc("test_fcn", params: ["KEY": "VALUE"])
Expand Down Expand Up @@ -89,11 +103,20 @@ final class BuildURLRequestTests: XCTestCase {
},
TestCase(name: "test upsert not ignoring duplicates") { client in
try await client.from("users")
.upsert(["email": "[email protected]"])
.upsert(User(email: "[email protected]"))
},
TestCase(name: "bulk upsert") { client in
try await client.from("users")
.upsert(
[
User(email: "[email protected]"),
User(email: "[email protected]", username: "johndoe2"),
]
)
},
TestCase(name: "test upsert ignoring duplicates") { client in
try await client.from("users")
.upsert(["email": "[email protected]"], ignoreDuplicates: true)
.upsert(User(email: "[email protected]"), ignoreDuplicates: true)
},
TestCase(name: "query with + character") { client in
await client.from("users")
Expand Down
49 changes: 0 additions & 49 deletions Tests/PostgRESTTests/NullEncodableTests.swift

This file was deleted.

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 "X-Client-Info: postgrest-swift/x.y.z" \
--data "[{\"email\":\"[email protected]\"},{\"email\":\"[email protected]\",\"username\":\"johndoe2\"}]" \
"https://example.supabase.co/users?columns=email,username"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
curl \
--request POST \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--header "Prefer: resolution=merge-duplicates,return=representation" \
--header "X-Client-Info: postgrest-swift/x.y.z" \
--data "[{\"email\":\"[email protected]\"},{\"email\":\"[email protected]\",\"username\":\"johndoe2\"}]" \
"https://example.supabase.co/users?columns=email,username"

0 comments on commit 7a99c47

Please sign in to comment.