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

Class Method 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

Class Method Details

.open(**kwargs) ⇒ Object

Block form: yields the client, then closes it. Use to scope a client to a discrete unit of work (matches py’s ‘with SyncPostgrestClient(…)`).



85
86
87
88
89
90
91
92
93
94
# File 'lib/supabase/postgrest/client.rb', line 85

def self.open(**kwargs)
  client = new(**kwargs)
  return client unless block_given?

  begin
    yield client
  ensure
    client.close
  end
end

Instance Method Details

#auth(token, username: nil, password: "") ⇒ Client

Set the Authorization header to either Bearer (token) or Basic (username/password) authentication. Bearer wins if both are supplied. Mirrors supabase-py’s BasePostgrestClient.auth().

Parameters:

  • token (String, nil)
  • username (String, nil) (defaults to: nil)
  • password (String) (defaults to: "")

Returns:

  • (Client)

    self, so callers can chain.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/supabase/postgrest/client.rb', line 56

def auth(token, username: nil, password: "")
  if token && !token.empty?
    @headers["Authorization"] = "Bearer #{token}"
  elsif username
    credentials = ["#{username}:#{password}"].pack("m0")
    @headers["Authorization"] = "Basic #{credentials}"
  else
    raise ArgumentError, "Neither bearer token nor basic authentication credentials were provided"
  end
  self
end

#closeObject

Release the underlying HTTP connection. After close, subsequent requests will lazily rebuild the connection on the next call. Mirrors supabase-py’s BasePostgrestClient.aclose() / __exit__ hook — Ruby callers use it via ‘client.close` or `Postgrest::Client.new(…) { |c| … }` (see ::open below).



73
74
75
76
77
78
79
80
81
# File 'lib/supabase/postgrest/client.rb', line 73

def close
  # Faraday connections own a sub-connection per host through their
  # adapter. We can't force a hard close on most adapters, but dropping
  # our reference frees the connection for GC and ensures the next call
  # rebuilds.
  @session = nil
  @http_client = nil
  self
end

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

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



110
111
112
# File 'lib/supabase/postgrest/client.rb', line 110

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:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/supabase/postgrest/client.rb', line 126

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:



99
100
101
102
103
104
105
106
# File 'lib/supabase/postgrest/client.rb', line 99

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