Skip to content

Commit 06ed83e

Browse files
committed
parent 20986b6
author Khan Winter <[email protected]> 1627658421 -0500 committer Khan Winter <[email protected]> 1638459904 -0600 Fixed RPC builder, removed testing framework Fixed a bug with the RPC builder setting the query builder's method to the RPC's schema. Added a test for RPC functions. Removed unnecessary testing framework. Updated README.md Add Test for RPC function call Fix RPC builder bug RPC builder was passing `schema` to the `method` property. Rename `form` to `from` Update PostgrestBuilder.swift Make body content-type explicit Make body content-type explicit Actually make content-type explicit for JSON httpbody Update PostgrestFilterBuilder.swift Add return= option Adds an enum `PostgrestReturningOptions` that specifies either a `minimal` or `representation` return from the PostgREST query. This option is added to `insert`, `update`, and `delete`. In relation to issue #18
1 parent 20986b6 commit 06ed83e

8 files changed

+61
-43
lines changed

Package.resolved

-16
This file was deleted.

README.md

+32-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Installation
44

5-
Swift client for [PostgREST](https://postgrest.org). The goal of this library is to make an "ORM-like" restful interface.
5+
Swift client for [PostgREST](https://postgrest.org). The goal of this library is to make an "ORM-like" restful interface.
66

77
### Swift Package Manager
88

@@ -77,13 +77,39 @@ do {
7777
semaphore.wait()
7878
```
7979

80+
Execute an RPC
81+
82+
```swift
83+
let client = PostgrestClient(url: "https://example.supabase.co", schema: nil)
84+
85+
do {
86+
try client.rpc(fn: "testFunction", parameters: nil).execute { result in
87+
// Handle result
88+
}
89+
} catch {
90+
print("Error executing the RPC: \(error)")
91+
}
92+
```
93+
94+
## Auth
95+
96+
You can add authentication to the databases requests by using the `client.headers` property. For example to add a `Bearer` auth header, simply set the headers dictionary to:
97+
98+
```swift
99+
let client = PostgrestClient(url: "https://example.supabase.co",
100+
headers: ["Bearer": "{ Insert Token Here }"]
101+
schema: nil)
102+
```
103+
104+
All requests made using this client will be sent with the `Bearer Token` header.
105+
80106
## Contributing
81107

82-
- Fork the repo on GitHub
83-
- Clone the project to your own machine
84-
- Commit changes to your own branch
85-
- Push your work back up to your fork
86-
- Submit a Pull request so that we can review your changes and merge
108+
- Fork the repo on GitHub
109+
- Clone the project to your own machine
110+
- Commit changes to your own branch
111+
- Push your work back up to your fork
112+
- Submit a Pull request so that we can review your changes and merge
87113

88114
## License
89115

Sources/PostgREST/PostgrestBuilder.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,19 @@ public class PostgrestBuilder {
142142
}
143143

144144
var request = URLRequest(url: url)
145-
request.httpMethod = method
146-
request.allHTTPHeaderFields = headers
145+
147146
if let body = body {
148147
if let httpBody = body as? Data {
149148
request.httpBody = httpBody
150149
} else {
151150
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
151+
headers["Content-Type"] = "application/json"
152152
}
153153
}
154+
155+
request.httpMethod = method
156+
request.allHTTPHeaderFields = headers
157+
154158
return request
155159
}
156160

Sources/PostgREST/PostgrestQueryBuilder.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
1818
)
1919
}
2020

21-
public func insert(values: Any, upsert: Bool = false, onConflict: String? = nil) -> PostgrestBuilder {
21+
public func insert(values: Any, upsert: Bool = false, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation) -> PostgrestBuilder {
2222
method = "POST"
23-
headers["Prefer"] = upsert ? "return=representation,resolution=merge-duplicates" : "return=representation"
23+
headers["Prefer"] = upsert ? "return=\(returning.rawValue),resolution=merge-duplicates" : "return=\(returning.rawValue)"
2424
if let onConflict = onConflict {
2525
appendSearchParams(name: "on_conflict", value: onConflict)
2626
}
@@ -29,9 +29,9 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
2929
return self
3030
}
3131

32-
public func upsert(values: Any, onConflict: String? = nil) -> PostgrestBuilder {
32+
public func upsert(values: Any, onConflict: String? = nil, returning: PostgrestReturningOptions = .representation) -> PostgrestBuilder {
3333
method = "POST"
34-
headers["Prefer"] = "return=representation,resolution=merge-duplicates"
34+
headers["Prefer"] = "return=\(returning.rawValue),resolution=merge-duplicates"
3535
if let onConflict = onConflict {
3636
appendSearchParams(name: "on_conflict", value: onConflict)
3737
}
@@ -40,19 +40,19 @@ public class PostgrestQueryBuilder: PostgrestBuilder {
4040
return self
4141
}
4242

43-
public func update(values: Any) -> PostgrestFilterBuilder {
43+
public func update(values: Any, returning: PostgrestReturningOptions = .representation) -> PostgrestFilterBuilder {
4444
method = "PATCH"
45-
headers["Prefer"] = "return=representation"
45+
headers["Prefer"] = "return=\(returning.rawValue)"
4646
body = values
4747
return PostgrestFilterBuilder(
4848
url: url, queryParams: queryParams, headers: headers, schema: schema, method: method,
4949
body: body
5050
)
5151
}
5252

53-
public func delete() -> PostgrestFilterBuilder {
53+
public func delete(returning: PostgrestReturningOptions = .representation) -> PostgrestFilterBuilder {
5454
method = "DELETE"
55-
headers["Prefer"] = "return=representation"
55+
headers["Prefer"] = "return=\(returning.rawValue)"
5656
return PostgrestFilterBuilder(
5757
url: url, queryParams: queryParams, headers: headers, schema: schema, method: method,
5858
body: body
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// Enum of options representing the ways PostgREST can return values from the server.
2+
/// Options are:
3+
/// - minimal => Returns nothing from the server
4+
/// - representation => Returns a copy of the updated data.
5+
///
6+
/// https://postgrest.org/en/v9.0/api.html?highlight=PREFER#insertions-updates
7+
public enum PostgrestReturningOptions: String {
8+
case minimal = "minimal"
9+
case representation = "representation"
10+
}

Tests/PostgRESTTests/BuildURLRequestTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ final class BuildURLRequestTests: XCTestCase {
2828
.insert(values: ["email": "[email protected]"])
2929
.buildURLRequest(head: false, count: nil)
3030
},
31-
TestCase(name: "call rpc") { client in
31+
TestCase(name: "call rpc", build: { client in
3232
try client.rpc(fn: "test_fcn", parameters: ["KEY": "VALUE"])
3333
.buildURLRequest(head: false, count: nil)
34-
}
34+
})
3535
]
3636

3737
for testCase in testCases {

Tests/PostgRESTTests/PostgRESTTests.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
import XCTest
33

44
final class PostgRESTTests: XCTestCase {
5-
func testExample() {
6-
// This is an example of a functional test case.
7-
// Use XCTAssert and related functions to verify your tests produce the correct
8-
// results.
9-
// XCTAssertEqual(PostgREST().text, "Hello, World!")
10-
}
5+
6+
func testBuildRPCRequest() throws {
7+
let client = PostgrestClient(url: url, schema: nil)
118

129
static var allTests = [
1310
("testExample", testExample),

Tests/PostgRESTTests/__Snapshots__/BuildURLRequestTests/testBuildURLRequest.select-all-users-where-email-ends-with-supabase-co.txt

-3
This file was deleted.

0 commit comments

Comments
 (0)