Skip to content

Update Readme with examples from test cases #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 39 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Swift client for [PostgREST](https://postgrest.org). The goal of this library is
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).

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

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

let semaphore = DispatchSemaphore(value: 0)
struct Todo: Codable, Hashable {
let id: UUID
var description: String
var isComplete: Bool

struct Todo: Codable {
var id: Int?
var task: String?
var completed: Bool?
enum CodingKeys: String, CodingKey {
case id
case description
case isComplete = "is_complete"
}
}

database.from("todo").select().execute { result in
switch result {
case let .success(response):
do {
let todos = try response.decoded(to: [Todo].self)
print(todos)
} catch {
print(error.localizedDescription)
}
case let .failure(error):
print(error.localizedDescription)
}
}
struct NewTodo: Codable, Hashable {
var description: String
var isComplete: Bool = false

do {
let todo = Todo(task: "fix some issues in postgrest-swift", completed: true)
let jsonData: Data = try JSONEncoder().encode(todo)

database.from("todo").insert(values: jsonData).execute { result in
switch result {
case let .success(response):
do {
let todos = try response.decoded(to: [Todo].self)
print(todos)
} catch {
print(error.localizedDescription)
}
case let .failure(error):
print(error.localizedDescription)
}
}

} catch {
print(error.localizedDescription)
enum CodingKeys: String, CodingKey {
case description
case isComplete = "is_complete"
}
}

semaphore.wait()
// Get todos
var todos = try await client
.from("todo")
.select()
.execute()
.decoded(to: [Todo].self)

// Insert a todo
let insertedTodo = try await client.from("todo")
.insert(values: NewTodo(description: "Implement integration tests for postgrest-swift"))
.execute()
.decoded(to: [Todo].self)[0]

// Insert multiple todos
let insertedTodos = try await client.from("todo")
.insert(values: [
NewTodo(description: "Make supabase swift libraries production ready"),
NewTodo(description: "Drink some coffee"),
])
.execute()
.decoded(to: [Todo].self)

```

## Contributing
Expand Down