Show HN: Erlang/Elixir library to work with Supabase
3 weeks ago
2
esupa is an Erlang/OTP library that provides a comprehensive client interface for Supabase, supporting both REST API operations and real-time WebSocket connections for PostgreSQL change streams.
# Get a client connection{:ok,client}=:esupa.get_client()# Build and execute a queryresult=:esupa.request(client,get,'public')|>:esupa.supa_from('users')|>:esupa.supa_select(['id','name','email'])|>:esupa.supa_eq('active',true)|>:esupa.supa_order('created_at',desc)|>:esupa.supa_range(0,10)|>:esupa.execute()
3. Real-time WebSocket Usage
# Subscribe to table changes:esupa_websocket_service.subscribe(self(),# Receiver PID{'public','users','INSERT',''},# {schema, table, event, filter}1# Number of subscribers)# Handle incoming changesreceive%{"event"=>"postgres_changes","payload"=>payload}->do_something(payload)end
HTTP API Reference (examples in Erlang)
get_client() -> {ok, pid()} | {error, string()}
Retrieves an available HTTP client from the connection pool.
{ok, Client} =esupa:get_client().
request(Client, Method, Schema) -> request()
Creates a new request targeting the specified schema.
Request=esupa:request(Client, get, "public").
Parameters:
Client: HTTP client PID
Method: HTTP method (get, post, patch, delete)
Schema: Database schema name
supa_from(Request, Table) -> request()
Specifies the target table for the operation.
Request2=esupa:supa_from(Request, "users").
supa_select(Request, Columns) -> request()
Defines which columns to return. Use [] for all columns.
%% Select specific columnsRequest3=esupa:supa_select(Request2, ["id", "name", "email"]),
%% Select all columnsRequest3=esupa:supa_select(Request2, []).
supa_join(Request, Joins) -> request()
Adds JOIN operations with related tables.
%% Join with profiles tableJoins= [{"profiles", ["avatar_url", "bio"]}],
Request4=esupa:supa_join(Request3, Joins).
%% EqualityRequest=esupa:supa_eq(Request, "status", "active"),
%% Greater thanRequest=esupa:supa_gt(Request, "age", 18),
%% Greater than or equalRequest=esupa:supa_gte(Request, "score", 100),
%% Less thanRequest=esupa:supa_lt(Request, "price", 50),
%% Less than or equalRequest=esupa:supa_lte(Request, "quantity", 10),
%% IN clauseRequest=esupa:supa_in(Request, "category", ["electronics", "books"]).
Implements pagination by limiting the result range.
%% Get records 0-19 (first page, 20 items)Request=esupa:supa_range(Request, 0, 19),
%% Get records 20-39 (second page, 20 items)Request=esupa:supa_range(Request, 20, 39).
execute(Request) -> term()
Executes the built request and returns the response.
# Local development release
$ rebar3 as local release
$ ./_build/local/rel/esupa/bin/esupa console
# Test environment
$ rebar3 as test release
$ ./_build/test/rel/esupa/bin/esupa console
# Production release
$ rebar3 as prod release
$ ./_build/prod/rel/esupa/bin/esupa daemon
TBD
Generate documentation using ExDoc:
This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details.