Skip to content

Commit ada9d8b

Browse files
authored
Update Readme with examples from test cases (#27)
1 parent cb1c8a7 commit ada9d8b

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

README.md

+39-40
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Swift client for [PostgREST](https://postgrest.org). The goal of this library is
99
Add `postgrest-swift` as a dependency to your `Package.swift` file. For more information, please see the [Swift Package Manager documentation](https://github.com/apple/swift-package-manager/tree/master/Documentation).
1010

1111
```swift
12-
.package(url: "https://github.com/supabase/postgrest-swift", .exact("0.0.1"))
12+
.package(url: "https://github.com/supabase/postgrest-swift", .exact("0.0.2"))
1313
```
1414

1515
### Supabase
@@ -30,51 +30,50 @@ var database = PostgrestClient(
3030
headers: ["apikey": supabaseKey],
3131
schema: "public")
3232

33-
let semaphore = DispatchSemaphore(value: 0)
33+
struct Todo: Codable, Hashable {
34+
let id: UUID
35+
var description: String
36+
var isComplete: Bool
3437

35-
struct Todo: Codable {
36-
var id: Int?
37-
var task: String?
38-
var completed: Bool?
38+
enum CodingKeys: String, CodingKey {
39+
case id
40+
case description
41+
case isComplete = "is_complete"
42+
}
3943
}
4044

41-
database.from("todo").select().execute { result in
42-
switch result {
43-
case let .success(response):
44-
do {
45-
let todos = try response.decoded(to: [Todo].self)
46-
print(todos)
47-
} catch {
48-
print(error.localizedDescription)
49-
}
50-
case let .failure(error):
51-
print(error.localizedDescription)
52-
}
53-
}
45+
struct NewTodo: Codable, Hashable {
46+
var description: String
47+
var isComplete: Bool = false
5448

55-
do {
56-
let todo = Todo(task: "fix some issues in postgrest-swift", completed: true)
57-
let jsonData: Data = try JSONEncoder().encode(todo)
58-
59-
database.from("todo").insert(values: jsonData).execute { result in
60-
switch result {
61-
case let .success(response):
62-
do {
63-
let todos = try response.decoded(to: [Todo].self)
64-
print(todos)
65-
} catch {
66-
print(error.localizedDescription)
67-
}
68-
case let .failure(error):
69-
print(error.localizedDescription)
70-
}
71-
}
72-
73-
} catch {
74-
print(error.localizedDescription)
49+
enum CodingKeys: String, CodingKey {
50+
case description
51+
case isComplete = "is_complete"
52+
}
7553
}
7654

77-
semaphore.wait()
55+
// Get todos
56+
var todos = try await client
57+
.from("todo")
58+
.select()
59+
.execute()
60+
.decoded(to: [Todo].self)
61+
62+
// Insert a todo
63+
let insertedTodo = try await client.from("todo")
64+
.insert(values: NewTodo(description: "Implement integration tests for postgrest-swift"))
65+
.execute()
66+
.decoded(to: [Todo].self)[0]
67+
68+
// Insert multiple todos
69+
let insertedTodos = try await client.from("todo")
70+
.insert(values: [
71+
NewTodo(description: "Make supabase swift libraries production ready"),
72+
NewTodo(description: "Drink some coffee"),
73+
])
74+
.execute()
75+
.decoded(to: [Todo].self)
76+
7877
```
7978

8079
## Contributing

0 commit comments

Comments
 (0)