Skip to content

Commit ecabbbc

Browse files
2 parents a896674 + 5603b4e commit ecabbbc

File tree

1 file changed

+48
-51
lines changed

1 file changed

+48
-51
lines changed

README.md

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,74 +18,71 @@ You can also install the [ `supabase-swift`](https://github.com/supabase/supabas
1818

1919
## Usage
2020

21-
Query todo table for all completed todos.
2221
```swift
23-
let client = PostgrestClient(url: "https://example.supabase.co", schema: nil)
22+
import Foundation
23+
import PostgREST
2424

25-
do {
26-
let query = try client.from("todos")
27-
.select()
28-
.eq(column: "isDone", value: "true")
29-
try query.execute { [weak self] (results) in
30-
guard let self = self else { return }
31-
32-
// Handle results
33-
}
34-
} catch {
35-
print("Error querying for todos: \(error)")
36-
}
37-
```
25+
let supabaseUrl = ""
26+
let supabaseKey = ""
3827

39-
Insert a todo into the database.
40-
```swift
41-
let client = PostgrestClient(url: "https://example.supabase.co", schema: nil)
28+
var database = PostgrestClient(
29+
url: "\(supabaseUrl)/rest/v1",
30+
headers: ["apikey": supabaseKey],
31+
schema: "public")
32+
33+
let semaphore = DispatchSemaphore(value: 0)
4234

4335
struct Todo: Codable {
44-
var id: UUID = UUID()
45-
var label: String
46-
var isDone: Bool = false
36+
var id: Int?
37+
var task: String?
38+
var completed: Bool?
4739
}
4840

49-
let todo = Todo(label: "Example todo!")
50-
51-
do {
52-
let jsonData: Data = try JSONEncoder().encode(todo)
53-
let jsonDict: [String: Any] = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments))
54-
55-
try client.from("todos")
56-
.insert(values: jsonDict)
57-
.execute { results in
58-
// Handle response
41+
database.from("todo").select().execute { result in
42+
switch result {
43+
case let .success(response):
44+
guard let data = response.body as? Data else {
45+
return
46+
}
47+
do {
48+
let todos = try JSONDecoder().decode([Todo].self, from: data)
49+
print(todos)
50+
} catch {
51+
print(error.localizedDescription)
52+
}
53+
case let .failure(error):
54+
print(error.localizedDescription)
5955
}
60-
} catch {
61-
print("Error inserting the todo: \(error)")
6256
}
63-
```
64-
65-
For more query examples visit [the Javascript docs](https://supabase.io/docs/reference/javascript/select) to learn more. The API design is a near 1:1 match.
66-
67-
Execute an RPC
68-
```swift
69-
let client = PostgrestClient(url: "https://example.supabase.co", schema: nil)
7057

7158
do {
72-
try client.rpc(fn: "testFunction", parameters: nil).execute { result in
73-
// Handle result
59+
let todo = Todo(task: "fix some issues in postgrest-swift", completed: true)
60+
let jsonData: Data = try JSONEncoder().encode(todo)
61+
62+
database.from("todo").insert(values: jsonData).execute { result in
63+
switch result {
64+
case let .success(response):
65+
guard let data = response.body as? Data else {
66+
return
67+
}
68+
do {
69+
let todos = try JSONDecoder().decode([Todo].self, from: data)
70+
print(todos)
71+
} catch {
72+
print(error.localizedDescription)
73+
}
74+
case let .failure(error):
75+
print(error.localizedDescription)
76+
}
7477
}
78+
7579
} catch {
76-
print("Error executing the RPC: \(error)")
80+
print(error.localizedDescription)
7781
}
78-
```
7982

80-
## Auth
83+
semaphore.wait()
8184

82-
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:
83-
```swift
84-
let client = PostgrestClient(url: "https://example.supabase.co",
85-
headers: ["Bearer": "{ Insert Token Here }"]
86-
schema: nil)
8785
```
88-
All requests made using this client will be sent with the `Bearer Token` header.
8986

9087
## Contributing
9188

0 commit comments

Comments
 (0)