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.
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_url ⇒ Object (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 |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
28 29 30 |
# File 'lib/supabase/postgrest/client.rb', line 28 def headers @headers end |
#schema_name ⇒ Object (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().
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 |
#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).
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.
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.
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.
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 |