Swift: Fetch data

Examples

Getting your data

struct Country: Decodable \{
  let id: Int
  let name: String
\}

let countries: [Country] = try await supabase
  .from("countries")
  .select()
  .execute()
  .value

Selecting specific columns

struct Country: Decodable \{
  let name: String
\}

let countries: [Country] = try await supabase
  .from("countries")
  .select("name")
  .execute()
  .value

Query foreign tables

struct Country: Decodable \{
  let name: String
  let cities: [City]
\}

struct City: Decodable \{
  let name: String
\}

let countries: [Country] = try await supabase
  .from("countries")
  .select(
    """
      name,
      cities (
        name
      )
    """
  )
  .execute()
  .value

Query foreign tables through a join table

  struct User: Decodable \{
    let name: String
    let teams: [Team]
  \}
  
  struct Team: Decodable \{
    let name: String
  \}

  let users: [User] = try await supabase
    .from("users")
    .select(
      """
        name,
        teams (
          name
        )
      """
    )
    .execute()
    .value

Query the same foreign table multiple times

struct Message: Decodable \{
  let content: String
  let from: User
  let to: User
\}

struct User: Decodable \{
  let name: String
\}

let messages: [Message] = try await supabase
  .from("messages")
  .select(
    """
      content,
      from:sender_id(name),
      to:sended_id(name)
    """
  )
  .execute()
  .value

Filtering through foreign tables

struct City: Decodable \{
  let name: String
  let countries: [Country]?
\}

struct Country: Decodable \{
  let name: String
\}

let cities: [City] = try await supabase
  .from("cities")
  .select("name, countries(*)")
  .eq("countries.name", value: "Estonia")
  .execute()
  .value

Querying foreign table with count

struct Country: Decodable \{
  let id: UUID
  let name: String
  let cities: [City]
\}

struct City: Decodable \{
  let count: Int
\}

let countries: [Country] = try await supabase
  .from("countries")
  .select("*, cities(count)")
  .execute()
  .value

Querying with count option

let count = try await supabase
  .from("countries")
  .select("*", head: true, count: .exact)
  .execute()
  .count

Querying JSON data

struct User: Decodable \{
  let id: Int
  let name: String
  let city: String
\}

let users: [User] = try await supabase
  .from("users")
  .select(
    """
      id, name,
      address->city
    """
  )
  .execute()
  .value

Querying foreign table with inner join

struct City: Decodable \{
  let name: String
\}

struct Country: Decodable \{
  let name: String
  let cities: [City]
\}

let countries: [Country] = try await supabase
  .from("countries")
  .select("name, cities!inner(name)")
  .eq("name", value: "united kingdom")
  .execute()
  .value

Switching schemas per query

try await supabase
  .schema("myschema")
  .from("mytable")
  .select()