Skip to content

Commit 67d675a

Browse files
authored
Add further CRUD and RPC documentation (#79)
* additional CRUD operations + RPC documentation * add info on a (probably) common mistake
1 parent c5d5e3e commit 67d675a

File tree

1 file changed

+105
-7
lines changed

1 file changed

+105
-7
lines changed

README.md

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,30 @@ Task {
6161
do {
6262
try await client.auth.signUp(email: email, password: password)
6363
let session = try await client.auth.session
64-
print("### Session Info: \(session)")
64+
print("### Session Info: \(session)")
65+
} catch {
66+
print("### Sign Up Error: \(error)")
67+
}
68+
}
69+
```
70+
71+
If you wish to add metadata for the user, you can pass it as part of the `data` parameter, just be sure to `import GoTrue` first to use the User metadata values.
72+
73+
```swift
74+
Task {
75+
do {
76+
try await client.auth.signUp(
77+
email: email,
78+
password: password,
79+
data: [
80+
"name": .string("John Doe"),
81+
"age": .number(25),
82+
"some_boolean_parameter": .bool(true)
83+
]
84+
)
85+
86+
let session = try await client.auth.session
87+
print("### Session Info: \(session)")
6588
} catch {
6689
print("### Sign Up Error: \(error)")
6790
}
@@ -75,7 +98,7 @@ Task {
7598
do {
7699
try await client.auth.signIn(email: email, password: password)
77100
let session = try await client.auth.session
78-
print("### Session Info: \(session)")
101+
print("### Session Info: \(session)")
79102
} catch {
80103
print("### Sign Up Error: \(error)")
81104
}
@@ -154,7 +177,7 @@ NotificationCenter.default.addObserver(
154177
selector: #selector(self.oAuthCallback(_:)),
155178
name: NSNotification.Name(rawValue: "OAuthCallBack"),
156179
object: nil)
157-
180+
158181
@objc func oAuthCallback(_ notification: NSNotification){
159182
guard let url = notification.userInfo?["url"] as? URL else { return }
160183
Task {
@@ -171,7 +194,7 @@ NotificationCenter.default.addObserver(
171194

172195
### Apple Sign In
173196

174-
- Setup Apple Auth as per [Supabase's Documentation](https://supabase.com/docs/guides/auth/social-login/auth-apple).
197+
- Setup Apple Auth as per [Supabase's Documentation](https://supabase.com/docs/guides/auth/social-login/auth-apple).
175198
- For Sign in with Apple follow the above as per Google Sign In and just replace the provider.
176199
- Once the user moves to the `SFSafariViewController`, an Apple native pop-up will slide up to continue with the sign in.
177200

@@ -197,8 +220,10 @@ First, import and initialize `SupabaseClient`, as explained in "Usage" section.
197220

198221
### Insert Data
199222

223+
- You can either use `Codable` or `Encodable` and `Decodable` protocols for the model's struct. However without either, you will get an error saying `Cannot convert value of type 'Void' to specified type 'InsertModel'` when trying to cast the response to your model.
200224
- Create a model which follows your table's data structure:
201225

226+
202227
```swift
203228
struct InsertModel: Encodable, Decodable {
204229
let id: Int? // you can choose to omit this depending on how you've setup your table
@@ -213,7 +238,7 @@ let query = client.database
213238
returning: .representation) // you will need to add this to return the added data
214239
.select(columns: "id") // specifiy which column names to be returned. Leave it empty for all columns
215240
.single() // specify you want to return a single value.
216-
241+
217242
Task {
218243
do {
219244
let response: [InsertModel] = try await query.execute().value
@@ -235,13 +260,86 @@ let query = client.database
235260
.select() // keep it empty for all, else specify returned data
236261
.match(query: ["title" : insertData.title, "desc": insertData.desc])
237262
.single()
238-
263+
239264
Task {
240265
do {
241266
let response: [InsertModel] = try await query.execute().value
242267
print("### Returned: \(response)")
243268
} catch {
244-
print("### Insert Error: \(error)")
269+
print("### Select Error: \(error)")
270+
}
271+
}
272+
```
273+
274+
### Update Data
275+
276+
- Using the same model as before:
277+
278+
```swift
279+
// Assuming the record above was inserted with id 1
280+
let updateData = InsertModel(id: 1, title: "Test Edited", desc: "Test Desc Edited")
281+
let query = client.database
282+
.from("{ Your Table Name }")
283+
.update(values: updateData,
284+
returning: .representation) // you will need to add this to return the updated data
285+
.select(columns: "id") // specifiy which column names to be returned. Leave it empty for all columns
286+
.single() // specify you want to return a single value.
287+
288+
Task {
289+
do {
290+
let response: [InsertModel] = try await query.execute().value
291+
print("### Returned: \(response)")
292+
} catch {
293+
print("### Update Error: \(error)")
294+
}
295+
}
296+
```
297+
298+
### Delete Data
299+
300+
```swift
301+
let query = client.database
302+
.from("{ Your Table Name }")
303+
.delete(returning: .representation) // you will need to add this to return the deleted data
304+
.match(
305+
query: ["id" : 1] // assuming the record above was inserted with id 1
306+
// You can add additional conditions here
307+
)
308+
.select() // specifiy which column names to be returned. Leave it empty for all columns
309+
.single()
310+
311+
Task {
312+
do {
313+
let response: [InsertModel] = try await query.execute().value
314+
print("### Returned: \(response)")
315+
} catch {
316+
print("### Delete Error: \(error)")
317+
}
318+
}
319+
```
320+
321+
## Postgres Functions
322+
323+
- Unlike the JavaScript library, you can't use the `rpc` method on the `SupabaseClient`, instead you need to use the `rpc` method on the `PostgresClient`:
324+
325+
```swift
326+
struct YourFunctionNameParams: Codable {
327+
let param1: String
328+
let param2: String
329+
}
330+
331+
let query = client.database.rpc(
332+
fn: "your_function_name",
333+
params: YourFunctionNameParams(param1: "param1", param2: "param2")
334+
)
335+
// Like in Supabase-js, you can use the `.single` method to return a single value.
336+
337+
Task {
338+
do {
339+
let response: [DataModel] = try await query.execute().value // Where DataModel is the model of the data returned by the function
340+
print("### Returned: \(response)")
341+
} catch {
342+
print("### RPC Error: \(error)")
245343
}
246344
}
247345
```

0 commit comments

Comments
 (0)