Swift: Fetch data

Examples

Getting your data

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

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

Selecting specific columns

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

let countries: [Country] = try await supabase.database
  .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.database
  .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.database
    .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.database
  .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.database
  .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.database
  .from("countries")
  .select("*, cities(count)")
  .execute()
  .value

Querying with count option

let count = try await supabase.database
  .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.database
  .from("users")
  .select(
    """
      id, name,
      address->city
    """
  )
  .execute()
  .value

Querying foreign table with inner join

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

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

let cities: [City] = try await supabase.database
  .from("cities")
  .select("name, countries!inner(name)")
  .eq("countries.name", value: "Indonesia")
  .execute()
  .value