Swift: Fetch data

Examples

Getting your data

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

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

Selecting specific columns

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

let instruments: [Instrument] = try await supabase.database
  .from("instruments")
  .select("name")
  .execute()
  .value

Query foreign tables

struct OrchestralSection: Decodable \{
  let name: String
  let instruments: [Instrument]
\}

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

let orchestralSections: [OrchestralSection] = try await supabase.database
  .from("orchestral_sections")
  .select(
    """
      name,
      instruments (
        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 Instrument: Decodable \{
  let name: String
  let orchestral_sections: [OrchestralSection]?
\}

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

let instruments: [Instrument] = try await supabase.database
  .from("instruments")
  .select("name, orchestral_sections(*)")
  .eq("orchestral_sections.name", value: "percussion")
  .execute()
  .value

Querying foreign table with count

struct OrchestralSection: Decodable \{
  let id: UUID
  let name: String
  let instruments: [Instrument]
\}

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

let orchestralSections: [OrchestralSection] = try await supabase.database
  .from("orchestral_sections")
  .select("*, instruments(count)")
  .execute()
  .value

Querying with count option

let count = try await supabase.database
  .from("instruments")
  .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 Instrument: Decodable \{
  let name: String
  let sections: [Section]
\}

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

let instruments: [Instrument] = try await supabase.database
  .from("instruments")
  .select("name, sections!inner(name)")
  .eq("sections.name", value: "strings")
  .execute()
  .value