Skip to content

Commit c2b1168

Browse files
authored
Split Supabase into packages and implement high level functionality (#8)
* feat: split supabase into more packages * feat: docs about supabase_potion * feat: extensive documentation and examples
1 parent 0df49b4 commit c2b1168

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1977
-53
lines changed

README.md

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,135 @@
22

33
> Complete SDK and APIs integrations with Supabase
44
5-
This is a monorepo with all integrations.
5+
This monorepo houses the collection of Elixir SDK packages for integrating with Supabase, the open-source Firebase alternative. Our goal is to offer developers a seamless integration experience with Supabase services using Elixir.
66

7-
## Installation
7+
## Packages Overview
8+
9+
- **Supabase**: Main entrypoint for the Supabase SDK library, providing easy management for Supabase clients and connections.
10+
- **Supabase Connection**: Handles individual connections to Supabase, encapsulating the API endpoint and credentials.
11+
- **Supabase Storage**: Offers developers a way to store large objects like images, videos, and other files.
12+
- **Supabase PostgREST**: Directly turns your PostgreSQL database into a RESTful API using PostgREST.
13+
- **Supabase Realtime**: Provides a realtime websocket API, enabling listening to database changes.
14+
- **Supabase Auth**: A comprehensive user authentication system, complete with email sign-in, password recovery, session management, and more.
15+
- **Supabase UI**: UI components to help build Supabase-powered applications quickly.
16+
- **Supabase Fetcher**: Customized HTTP client for making requests to Supabase APIs.
17+
18+
## Getting Started
19+
20+
### Installation
21+
22+
To install the complete SDK:
823

924
```elixir
1025
def deps do
1126
[
12-
{:supabase_potion, "~> 0.1.0"}
27+
{:supabase_potion, "~> 0.1"}
1328
]
1429
end
1530
```
1631

17-
Notice that this would install ALL existing Supabase integrations of this project.
18-
If you only one or two, prefer to install specific integrations, like:
32+
Or, install specific packages as needed:
1933

2034
```elixir
2135
def deps do
2236
[
23-
{:supabase_storage, "~> 0.1.0"},
24-
{:supabase_auth, "~> 0.1.0"},
37+
{:supabase_storage, "~> 0.1"},
38+
{:supabase_realtime, "~> 0.1"},
39+
# ... add other packages
2540
]
2641
end
2742
```
2843

44+
### Clients vs Connections
45+
46+
A `Supabase.Client` is an Agent that holds general information about Supabase, that can be used to intereact with any of the children integrations, for example: `Supabase.Storage` or `Supabase.UI`.
47+
48+
Also a `Supabase.Client` holds a list of `Supabase.Connection` that can be used to perform operations on different buckets, for example.
49+
50+
`Supabase.Client` is defined as:
51+
52+
- `:name` - the name of the client, started by `start_link/1`
53+
- `:connections` - a list of `%{conn_alias => conn_name}`, where `conn_alias` is the alias of the connection and `conn_name` is the name of the connection.
54+
- `:db` - default database options
55+
- `:schema` - default schema to use, defaults to `"public"`
56+
- `:global` - global options config
57+
- `:headers` - additional headers to use on each request
58+
- `:auth` - authentication options
59+
- `:auto_refresh_token` - automatically refresh the token when it expires, defaults to `true`
60+
- `:debug` - enable debug mode, defaults to `false`
61+
- `:detect_session_in_url` - detect session in URL, defaults to `true`
62+
- `:flow_type` - authentication flow type, defaults to `"web"`
63+
- `:persist_session` - persist session, defaults to `true`
64+
- `:storage` - storage type
65+
- `:storage_key` - storage key
66+
67+
68+
On the other side, a `Supabase.Connection` is an Agent that holds the connection information and the current bucket, being defined as:
69+
70+
- `:base_url` - The base url of the Supabase API, it is usually in the form `https://<app-name>.supabase.io`.
71+
- `:api_key` - The API key used to authenticate requests to the Supabase API.
72+
- `:access_token` - Token with specific permissions to access the Supabase API, it is usually the same as the API key.
73+
- `:name` - Simple field to track the name of the connection, started by `start_link/1`.
74+
- `:alias` - Field to easily manage multiple connections on a `Supabase.Client` Agent.
75+
- `:bucket` - The current bucket to perform operations on.
76+
77+
In simple words, a `Supabase.Client` is a container for multiple `Supabase.Connection`, and each `Supabase.Connection` is a container for a single bucket.
78+
79+
### Establishing a Connection
80+
81+
To start a Supabase connection:
82+
83+
```elixir
84+
Supabase.init_connection(%{base_url: "https://myapp.supabase.io", api_key: "my_api_key", name: :my_conn, alias: :conn})
85+
```
86+
87+
This will automatically adds the Connection instance to a `DynamicSupervisor` that can be found in the [`Supabase.Connection`](./apps/supabase_connection/lib/supabase/connection_supervisor.ex) specific module documentation.
88+
89+
For manually Connection creation and management, please refer to the corresponding documentation:
90+
91+
- [Supabase.Connection](./apps/supabase_connection/lib/supabase/connection.ex)
92+
93+
### Creating a Client
94+
95+
To start a Supabase Client:
96+
97+
```elixir
98+
Supabase.init_client(%{name: :my_client}, [conn_list])
99+
```
100+
101+
This will automatically adds the Client instance to a `DynamicSupervisor` that can be found in the [`Supabase.ClientSupervisor`](./apps/supabase/lib/supabase/client_supervisor.ex) specific module documentation.
102+
103+
For manually Client creation and management, please refer to the corresponding documentation:
104+
105+
- [Supabase.Client](./apps/supabase/lib/supabase/client.ex)
106+
107+
108+
## Configuration
109+
110+
Ensure your Supabase configurations are set:
111+
112+
```elixir
113+
import Config
114+
115+
config :supabase_fetch,
116+
supabase_url: System.fetch_env!("SUPABASE_BASE_URL"),
117+
supabase_key: System.fetch_env!("SUPABASE_API_KEY"),
118+
```
119+
120+
Make sure to set the environment variables `SUPABASE_BASE_URL` and `SUPABASE_API_KEY`.
121+
122+
Detailed information on locating your Supabase base URL and API key can be found in the `Supabase.MissingSupabaseConfig` module.
123+
29124
## General Roadmap
30125

31126
If you want to track integration-specific roadmaps, check their own README.
32127

33128
- [x] Fetcher to interact with the Supabase API in a low-level way
34-
- [ ] Supabase Storage integration
129+
- [x] Supabase Storage integration
35130
- [ ] Supabase Postgrest integration
36131
- [ ] Supabase Auth integration
37132
- [ ] Supabase Realtime API integration
38133

39-
## Developing
40-
41-
42134

43135
## Why another Supabase package?
44136

@@ -50,3 +142,15 @@ Also I would like to contribute to OSS in some way and gain more experience with
50142

51143
- [supabase-elixir](https://github.com/treebee/supabase-elixir)
52144
- [storage-js](https://github.com/supabase/storage-js)
145+
146+
## Contributing
147+
148+
Contributions, issues, and feature requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
149+
150+
## Acknowledgements
151+
152+
This SDK is a comprehensive representation of Supabase's client integrations. Thanks to the Supabase community for their support and collaboration.
153+
154+
## License
155+
156+
[MIT](LICENSE)

apps/supabase/.formatter.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

apps/supabase/.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
supabase-*.tar
24+
25+
# Temporary files, for example, from tests.
26+
/tmp/

apps/supabase/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Supabase Potion
2+
3+
![Supabase Logo](https://supabase.io/img/supabase-logo.svg)
4+
5+
The Supabase Elixir SDK is a powerful library that enables seamless integration with [Supabase](https://supabase.io/), a cloud-based platform that provides a suite of tools and services for building modern web and mobile applications. This SDK allows you to interact with various Supabase services, such as storage, authentication, and realtime functionality, directly from your Elixir application.
6+
7+
## Installation
8+
9+
To get started with the Supabase Elixir SDK, you need to add it to your Elixir project's dependencies. Open your `mix.exs` file and add the following line to the `deps` function:
10+
11+
```elixir
12+
def deps do
13+
[
14+
{:supabase_potion, "~> 0.1"}
15+
]
16+
end
17+
```
18+
19+
After adding the dependency, run `mix deps.get` to fetch and install the SDK.
20+
21+
## Usage
22+
23+
The Supabase Elixir SDK provides a flexible way to manage `Supabase.Client` instances, which can, in turn, manage multiple `Supabase.Connection` instances. Here's a brief overview of the key concepts:
24+
25+
- **Supabase.Client**: This represents a container for multiple connections and holds general information about your Supabase setup. It can be used to interact with various Supabase services.
26+
27+
- **Supabase.Connection**: A connection holds information about the connection to the Supabase API, including the base URL, API key, and access token. Each connection can be associated with a specific bucket for performing operations.
28+
29+
### Starting a Connection
30+
31+
To start a new connection, you can use the `Supabase.Connection.start_link/1` function. For example:
32+
33+
```elixir
34+
iex> Supabase.Connection.start_link(name: :my_conn, conn_info: %{base_url: "https://myapp.supabase.io", api_key: "my_api_key"})
35+
{:ok, #PID<0.123.0>}
36+
```
37+
38+
Alternatively, you can use the higher-level API provided by the `Supabase` module, using the `Supabase.init_connection/1` function:
39+
40+
```elixir
41+
iex> Supabase.init_connection(%{base_url: "https://myapp.supabase.io", api_key: "my_api_key", name: :my_conn, alias: :conn1})
42+
{:ok, #PID<0.123.0>}
43+
```
44+
45+
### Starting a Client
46+
47+
After starting one or more connections, you can start a client using the `Supabase.Client.start_link/1` function. However, it's recommended to use `Supabase.init_client/2`, which allows you to pass client options and a list of connections that the client will manage. For example:
48+
49+
```elixir
50+
iex> Supabase.Client.init_client(%{db: %{schema: "public"}}, conn_list)
51+
{:ok, #PID<0.123.0>}
52+
```
53+
54+
## Acknowledgements
55+
56+
This SDK package represents the complete SDK for Supabase, encompassing all the functionality of various Supabase client integrations, including:
57+
58+
- [supabase-storage](https://hex.pm/packages/supabase_storage)
59+
- [supabase-postgrest](https://hex.pm/packages/supabase_postgrest)
60+
- [supabase-realtime](https://hex.pm/packages/supabase_realtime)
61+
- [supabase-auth](https://hex.pm/packages/supabase_auth)
62+
- [supabase-ui](https://hex.pm/packages/supabase_ui)
63+
- [supabase-fetcher](https://hex.pm/packages/supabase_fetcher)
64+
65+
You can choose to install only specific packages if you don't need the complete functionality. Just add the desired packages to your `deps` list in the `mix.exs` file.
66+
67+
For more detailed documentation, refer to the [supabase_connection documentation](https://hexdocs.pm/supabase_connection).
68+
69+
## Supabase Services
70+
71+
The Supabase Elixir SDK allows you to interact with various Supabase services:
72+
73+
### Supabase Storage
74+
75+
Supabase Storage is a service for storing large objects like images, videos, and other files. It provides a simple API with strong consistency, similar to AWS S3.
76+
77+
### Supabase PostgREST
78+
79+
PostgREST is a web server that turns your PostgreSQL database into a RESTful API. It automatically generates API endpoints and operations based on your database's structure and permissions.
80+
81+
### Supabase Realtime
82+
83+
Supabase Realtime offers a realtime WebSocket API powered by PostgreSQL notifications. You can use it to listen to changes in your database and receive updates instantly as they happen.
84+
85+
### Supabase Auth
86+
87+
Supabase Auth is a comprehensive user authentication system that includes features like email and password sign-in, email verification, password recovery, session management, and more, out of the box.
88+
89+
### Supabase UI
90+
91+
Supabase UI provides a set of UI components to help you build Supabase-powered applications quickly. It's built on top of Tailwind CSS and Headless UI, and it's fully customizable. The package even includes `Phoenix.LiveView` components!
92+
93+
### Supabase Fetcher
94+
95+
Supabase Fetcher is a customized HTTP client for Supabase, mainly used in Supabase Potion. It gives you complete control over how you make requests to any Supabase API.
96+
97+
---
98+
99+
With the Supabase Elixir SDK, you have the tools you need to supercharge your Elixir applications by seamlessly integrating them with Supabase's powerful cloud services. Happy coding! 😄

0 commit comments

Comments
 (0)