Skip to content

Commit a844b21

Browse files
Merge pull request #2 from thecoolwinter/master
Start adding documentation
2 parents 9bdbb17 + b11d39e commit a844b21

File tree

3 files changed

+141
-12
lines changed

3 files changed

+141
-12
lines changed

README.md

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,114 @@
1-
# `supabase-swift`
1+
# supabase-swift
22

3-
Swift Client library to interact with Supabase.
3+
Supabase client for swift. Mirrors the design of [supabase-js](https://github.com/supabase/supabase-js/blob/master/README.md)
44

5-
- Documentation: https://supabase.io/docs/reference/swift/supabase-client
5+
## Installation
6+
7+
Swift Package Manager:
8+
9+
Add the following lines to your `Package.swift` file:
10+
```swift
11+
let package = Package(
12+
...
13+
dependencies: [
14+
...
15+
.package(name: "Supabase", url: "https://github.com/supabase/supabase-swift.git", .branch("main")), // Add the package
16+
],
17+
targets: [
18+
.target(
19+
name: "YourTargetName",
20+
dependencies: ["Supabase"] // Add as a dependency
21+
)
22+
]
23+
)
24+
```
25+
26+
If you're using Xcode, [use this guide](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app) to add `supabase-swift` to your project. Use `https://github.com/supabase/supabase-swift.git` for the url when Xcode asks.
27+
28+
## Usage
29+
30+
For all requests made for supabase, you will need to initialize a `SupabaseClient` object.
31+
```swift
32+
let client = SupabaseClient(supabaseUrl: "{ Supabase URL }", supabaseKey: "{ Supabase anonymous Key }")
33+
```
34+
This client object will be used for all the following examples.
35+
36+
### Database
37+
38+
Query todo table for all completed todos.
39+
```swift
40+
do {
41+
let query = try client.database.from("todos")
42+
.select()
43+
.eq(column: "isDone", value: "true")
44+
45+
try query.execute { [weak self] (results) in
46+
guard let self = self else { return }
47+
48+
// Handle results
49+
}
50+
} catch {
51+
print("Error querying for todos: \(error)")
52+
}
53+
```
54+
55+
Insert a todo into the database.
56+
```swift
57+
struct Todo: Codable {
58+
var id: UUID = UUID()
59+
var label: String
60+
var isDone: Bool = false
61+
}
62+
63+
let todo = Todo(label: "Example todo!")
64+
65+
do {
66+
let jsonData: Data = try JSONEncoder().encode(todo)
67+
let jsonDict: [String: Any] = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments))
68+
69+
try client.database.from("todos")
70+
.insert(values: jsonDict)
71+
.execute { results in
72+
// Handle response
73+
}
74+
} catch {
75+
print("Error inserting the todo: \(error)")
76+
}
77+
```
78+
79+
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.
80+
81+
Execute an RPC
82+
```swift
83+
do {
84+
try client.database.rpc(fn: "testFunction", parameters: nil).execute { result in
85+
// Handle result
86+
}
87+
} catch {
88+
print("Error executing the RPC: \(error)")
89+
}
90+
```
91+
92+
### Realtime
93+
94+
> Realtime docs coming soon
95+
96+
### Auth
97+
98+
> Auth docs coming soon
99+
100+
### Storage
101+
102+
> Storage docs coming soon
103+
104+
105+
## Contributing
106+
107+
- Fork the repo on GitHub
108+
- Clone the project to your own machine
109+
- Commit changes to your own branch
110+
- Push your work back up to your fork
111+
- Submit a Pull request so that we can review your changes and merge
6112

7113
## Sponsors
8114

Sources/Supabase/Supabase.swift renamed to Sources/Supabase/SupabaseClient.swift

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,57 @@ import PostgREST
33
import Realtime
44
import SupabaseStorage
55

6+
/**
7+
The main class for accessing Supabase functionality
8+
9+
Initialize this class using `.init(supabaseURL: String, supabaseKey: String)`
10+
11+
There are four main classes contained by the `Supabase` class.
12+
1. `auth`
13+
2. `database`
14+
3. `realtime`
15+
4. `storage`
16+
Each class listed is available under `Supabase.{name}`, eg: `Supabase.auth`
17+
18+
For more usage information read the README.md
19+
*/
620
public class SupabaseClient {
7-
var supabaseUrl: String
8-
var supabaseKey: String
9-
var schema: String
10-
var restUrl: String
11-
var realtimeUrl: String
12-
var authUrl: String
13-
var storageUrl: String
21+
private var supabaseUrl: String
22+
private var supabaseKey: String
23+
private var schema: String
24+
private var restUrl: String
25+
private var realtimeUrl: String
26+
private var authUrl: String
27+
private var storageUrl: String
1428

29+
/// Auth client for Supabase
1530
public var auth: GoTrueClient
1631

32+
/// Storage client for Supabase.
1733
public var storage: SupabaseStorageClient {
1834
var headers: [String: String] = [:]
1935
headers["apikey"] = supabaseKey
2036
headers["Authorization"] = "Bearer \(auth.session?.accessToken ?? supabaseKey)"
2137
return SupabaseStorageClient(url: storageUrl, headers: headers)
2238
}
2339

40+
/// Database client for Supabase.
2441
public var database: PostgrestClient {
2542
var headers: [String: String] = [:]
2643
headers["apikey"] = supabaseKey
2744
headers["Authorization"] = "Bearer \(auth.session?.accessToken ?? supabaseKey)"
2845
return PostgrestClient(url: restUrl, headers: headers, schema: schema)
2946
}
3047

48+
/// Realtime client for Supabase
3149
private var realtime: RealtimeClient
3250

51+
/// Init `Supabase` with the provided parameters.
52+
/// - Parameters:
53+
/// - supabaseUrl: Unique Supabase project url
54+
/// - supabaseKey: Supabase anonymous API Key
55+
/// - schema: Database schema name, defaults to `public`
56+
/// - autoRefreshToken: Toggles whether `Supabase.auth` automatically refreshes auth tokens. Defaults to `true`
3357
public init(
3458
supabaseUrl: String,
3559
supabaseKey: String,

Tests/SupabaseTests/SupabaseTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import XCTest
44
@testable import Supabase
55

66
final class SupabaseTests: XCTestCase {
7-
var supabase = SupabaseClient(
8-
supabaseUrl: SupabaseTests.supabaseUrl(), supabaseKey: SupabaseTests.supabaseKey())
7+
var supabase = SupabaseClient(supabaseUrl: SupabaseTests.supabaseUrl(), supabaseKey: SupabaseTests.supabaseKey())
98

109
static func supabaseUrl() -> String {
1110
if let token = ProcessInfo.processInfo.environment["supabaseUrl"] {

0 commit comments

Comments
 (0)