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.
Instance Method Summary collapse
-
#from(table) ⇒ RequestBuilder
(also: #table)
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 |
Instance Method Details
#from(table) ⇒ RequestBuilder Also known as: table
Returns 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.
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.
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 |