Class: Supabase::Postgrest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/supabase/postgrest/client.rb

Overview

Sync PostgREST client. Constructed once per project; reused across requests.

“‘ruby client = Supabase::Postgrest::Client.new(

base_url: "https://project.supabase.co/rest/v1",
headers: { "apikey" => key, "Authorization" => "Bearer #{token}" }

)

users = client.from(“users”).select(“id, name”).eq(“status”, “active”).execute users.data # => [{ “id” => “…”, “name” => “…” }, …] users.count # => nil unless a count: was requested “‘

Direct Known Subclasses

Async::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, schema: "public", headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • base_url (String)

    full URL to the PostgREST endpoint (e.g. “…/rest/v1”)

  • schema (String) (defaults to: "public")

    postgres schema (default “public”)

  • headers (Hash) (defaults to: {})

    static request headers (apikey, Authorization)

  • http_client (Faraday::Connection, nil) (defaults to: nil)

    inject a pre-built Faraday for tests/custom adapters

  • verify (Boolean) (defaults to: true)

    TLS cert verification

  • proxy (String, nil) (defaults to: nil)

    HTTP proxy URL

  • timeout (Numeric, nil) (defaults to: nil)

    per-request timeout (seconds)



37
38
39
40
41
42
43
44
45
46
# File 'lib/supabase/postgrest/client.rb', line 37

def initialize(base_url:, schema: "public", headers: {}, http_client: nil,
               verify: true, proxy: nil, timeout: nil)
  @base_url = base_url.to_s.chomp("/")
  @schema_name = schema
  @headers = build_default_headers(schema, headers)
  @http_client = http_client
  @verify = verify
  @proxy = proxy
  @timeout = timeout
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



28
29
30
# File 'lib/supabase/postgrest/client.rb', line 28

def base_url
  @base_url
end

#headersObject (readonly)

Returns the value of attribute headers.



28
29
30
# File 'lib/supabase/postgrest/client.rb', line 28

def headers
  @headers
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



28
29
30
# File 'lib/supabase/postgrest/client.rb', line 28

def schema_name
  @schema_name
end

Instance Method Details

#from(table) ⇒ RequestBuilder Also known as: table

Returns entry point for select/insert/update/upsert/delete on this table.

Parameters:

  • table (String)

Returns:

  • (RequestBuilder)

    entry point for select/insert/update/upsert/delete on this table



62
63
64
# File 'lib/supabase/postgrest/client.rb', line 62

def from(table)
  RequestBuilder.new(session, "#{base_path}/#{table}", @headers.dup)
end

#rpc(func, params = {}, count: nil, head: false, get: false) ⇒ RPCFilterRequestBuilder

Stored procedure call.

Parameters:

  • func (String)

    function name

  • params (Hash) (defaults to: {})

    arguments to the function

  • count (String, nil) (defaults to: nil)

    one of “exact” / “planned” / “estimated”

  • head (Boolean) (defaults to: false)

    HEAD method (count only, no body)

  • get (Boolean) (defaults to: false)

    GET method (read-only)

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/supabase/postgrest/client.rb', line 75

def rpc(func, params = {}, count: nil, head: false, get: false)
  method = if head
             "HEAD"
           elsif get
             "GET"
           else
             "POST"
           end

  headers = @headers.dup
  headers["Prefer"] = "count=#{count}" if count

  if %w[HEAD GET].include?(method)
    query = stringify_keys(params)
    body = nil
  else
    query = {}
    body = params
  end

  request = RequestConfig.new(
    session: session, path: "#{base_path}/rpc/#{func}",
    http_method: method, headers: headers, params: query, json: body
  )
  RPCFilterRequestBuilder.new(request)
end

#schema(name) ⇒ Client

Switch schemas. Returns a new client that points at a different postgres schema.

Parameters:

  • name (String)

Returns:



51
52
53
54
55
56
57
58
# File 'lib/supabase/postgrest/client.rb', line 51

def schema(name)
  # self.class so async subclasses return an async client, not a sync one.
  self.class.new(
    base_url: @base_url, schema: name,
    headers: @headers.reject { |k, _| %w[Accept-Profile Content-Profile].include?(k) },
    http_client: @http_client, verify: @verify, proxy: @proxy, timeout: @timeout
  )
end