You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48-51Lines changed: 48 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,74 +18,71 @@ You can also install the [ `supabase-swift`](https://github.com/supabase/supabas
18
18
19
19
## Usage
20
20
21
-
Query todo table for all completed todos.
22
21
```swift
23
-
let client =PostgrestClient(url: "https://example.supabase.co", schema: nil)
22
+
importFoundation
23
+
importPostgREST
24
24
25
-
do {
26
-
let query =try client.from("todos")
27
-
.select()
28
-
.eq(column: "isDone", value: "true")
29
-
try query.execute { [weakself] (results) in
30
-
guardletself=selfelse { return }
31
-
32
-
// Handle results
33
-
}
34
-
} catch {
35
-
print("Error querying for todos: \(error)")
36
-
}
37
-
```
25
+
let supabaseUrl =""
26
+
let supabaseKey =""
38
27
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)
42
34
43
35
structTodo: Codable {
44
-
var id: UUID =UUID()
45
-
varlabel: String
46
-
varisDone: Bool=false
36
+
var id: Int?
37
+
vartask: String?
38
+
varcompleted: Bool?
47
39
}
48
40
49
-
let todo =Todo(label: "Example todo!")
50
-
51
-
do {
52
-
let jsonData: Data =tryJSONEncoder().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
+
caselet .success(response):
44
+
guardlet data = response.body as? Data else {
45
+
return
46
+
}
47
+
do {
48
+
let todos =tryJSONDecoder().decode([Todo].self, from: data)
49
+
print(todos)
50
+
} catch {
51
+
print(error.localizedDescription)
52
+
}
53
+
caselet .failure(error):
54
+
print(error.localizedDescription)
59
55
}
60
-
} catch {
61
-
print("Error inserting the todo: \(error)")
62
56
}
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)
70
57
71
58
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 =tryJSONEncoder().encode(todo)
61
+
62
+
database.from("todo").insert(values: jsonData).execute { result in
63
+
switch result {
64
+
caselet .success(response):
65
+
guardlet data = response.body as? Data else {
66
+
return
67
+
}
68
+
do {
69
+
let todos =tryJSONDecoder().decode([Todo].self, from: data)
70
+
print(todos)
71
+
} catch {
72
+
print(error.localizedDescription)
73
+
}
74
+
caselet .failure(error):
75
+
print(error.localizedDescription)
76
+
}
74
77
}
78
+
75
79
} catch {
76
-
print("Error executing the RPC: \(error)")
80
+
print(error.localizedDescription)
77
81
}
78
-
```
79
82
80
-
## Auth
83
+
semaphore.wait()
81
84
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)
87
85
```
88
-
All requests made using this client will be sent with the `Bearer Token` header.
0 commit comments