Class: Supabase::Postgrest::Client
- Inherits:
-
Object
- Object
- Supabase::Postgrest::Client
- 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
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#schema_name ⇒ Object
readonly
Returns the value of attribute schema_name.
Class Method Summary collapse
-
.open(**kwargs) ⇒ Object
Block form: yields the client, then closes it.
Instance Method Summary collapse
-
#auth(token, username: nil, password: "") ⇒ Client
Set the Authorization header to either Bearer (token) or Basic (username/password) authentication.
-
#close ⇒ Object
Release the underlying HTTP connection.
-
#from(table) ⇒ RequestBuilder
(also: #table, #from_)
Entry point for select/insert/update/upsert/delete on this table.
-
#initialize(base_url:, schema: "public", headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ Client
constructor
A new instance of Client.
-
#rpc(func, params = {}, count: nil, head: false, get: false) ⇒ RPCFilterRequestBuilder
Stored procedure call.
-
#schema(name) ⇒ Client
Switch schemas.
Constructor Details
#initialize(base_url:, schema: "public", headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/supabase/postgrest/client.rb', line 41 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.nil? ? DEFAULT_POSTGREST_TIMEOUT : timeout end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
32 33 34 |
# File 'lib/supabase/postgrest/client.rb', line 32 def base_url @base_url end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
32 33 34 |
# File 'lib/supabase/postgrest/client.rb', line 32 def headers @headers end |
#schema_name ⇒ Object (readonly)
Returns the value of attribute schema_name.
32 33 34 |
# File 'lib/supabase/postgrest/client.rb', line 32 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(…)`).
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/supabase/postgrest/client.rb', line 89 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().
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/supabase/postgrest/client.rb', line 60 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 |
#close ⇒ Object
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).
77 78 79 80 81 82 83 84 85 |
# File 'lib/supabase/postgrest/client.rb', line 77 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.
114 115 116 |
# File 'lib/supabase/postgrest/client.rb', line 114 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.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/supabase/postgrest/client.rb', line 130 def rpc(func, params = {}, count: nil, head: false, get: false) method = if head "HEAD" elsif get "GET" else "POST" end headers = {} headers["Prefer"] = "count=#{count}" if count headers.merge!(@headers) 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.
103 104 105 106 107 108 109 110 |
# File 'lib/supabase/postgrest/client.rb', line 103 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 |