Skip to content

Commit a3c97f7

Browse files
maailgrdsdev
andauthored
Update README with Insert and Select Queries (#61)
* Update ReadMe with Insert and Select Queries Added in Insert and Select Queries for Supabase Swift * Updated Insert Example to use the type and return single value Co-authored-by: Guilherme Souza <[email protected]> * Updated Select Model to return single and use the model for response directly --------- Co-authored-by: Guilherme Souza <[email protected]>
1 parent 45c6562 commit a3c97f7

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,65 @@ Task {
173173

174174
- Other Social Logins if using a webview will be similar to above and just follow the [Supabase's Documentation](https://supabase.com/docs/guides/auth/) for their setup
175175

176+
## Basic CRUD Implementation
177+
178+
Import and Initialize the Supabase client
179+
180+
```swift
181+
let client = SupabaseClient(supabaseURL: "{ Supabase URL }", supabaseKey: "{ Supabase anonymous Key }")
182+
```
183+
184+
### Insert Data
185+
186+
Create a model which follows the data structure of your table.
187+
188+
```swift
189+
struct InsertModel: Encodable {
190+
let id: Int? // you can choose to omit this depending on how you've setup your table
191+
let title: String?
192+
let desc: String?
193+
}
194+
195+
let insertData = InsertModel(title: "Test", desc: "Test Desc")
196+
let query = client.database
197+
.from("{ Your Table Name }")
198+
.insert(values: insertData,
199+
returning: .representation) // you will need to add this to return the added data
200+
.select(columns: "id") // specifiy which column names to be returned. Leave it empty for all columns
201+
.single() // specify you want to return a single value.
202+
203+
Task {
204+
do {
205+
let response: InsertModel = try await query.execute().value
206+
print("### Returned: \(response)")
207+
} catch {
208+
print("### Insert Error: \(error)")
209+
}
210+
}
211+
```
212+
213+
### Select Data
214+
215+
Using the same model as before
216+
217+
```swift
218+
let insertData = InsertModel(title: "Test", desc: "Test Desc")
219+
let query = client.database
220+
.from("{ Your Table Name }")
221+
.select() // keep it empty for all, else specify returned data
222+
.match(query: ["title" : insertData.title, "desc": insertData.desc])
223+
.single()
224+
225+
Task {
226+
do {
227+
let response: InsertModel = try await query.execute().value
228+
print("### Returned: \(response)")
229+
} catch {
230+
print("### Insert Error: \(error)")
231+
}
232+
}
233+
```
234+
176235
## Contributing
177236

178237
- Fork the repo on GitHub

0 commit comments

Comments
 (0)