|
| 1 | +--- |
| 2 | +title: Supabase Client |
| 3 | +description: Use the Supabase Client in Dart Edge. |
| 4 | +--- |
| 5 | + |
| 6 | +# Supabase Client |
| 7 | + |
| 8 | +The Dart [`supabase`](https://pub.dev/packages/supabase) package provides a client for connecting and interacting with your Supabase project. The client can be used directly inside of Dart Edge, however requires a few additional steps to get started. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +First, install the [`supabase`](https://pub.dev/packages/supabase) package from pub.dev: |
| 13 | + |
| 14 | +```sh |
| 15 | +dart pub add supabase |
| 16 | +``` |
| 17 | + |
| 18 | +Next, install the Dart Edge HTTP Client: |
| 19 | + |
| 20 | +```sh |
| 21 | +dart pub add edge_http_client |
| 22 | +``` |
| 23 | + |
| 24 | +Next, install the `yet_another_json_isolate` package (more on this next...): |
| 25 | + |
| 26 | +```sh |
| 27 | +dart pub add yet_another_json_isolate |
| 28 | +``` |
| 29 | + |
| 30 | +### Create an Isolate stub |
| 31 | + |
| 32 | +> Please note; this code will be extracted out at some point so this step will be easier. |
| 33 | +
|
| 34 | +The Supabase client uses isolates to parse JSON off the main thread. This is not supported in Dart Edge, so we need to create a stub that will allow the client to run it in the main thread. |
| 35 | + |
| 36 | +```dart |
| 37 | +import 'package:yet_another_json_isolate/yet_another_json_isolate.dart'; |
| 38 | +
|
| 39 | +class EdgeIsolate implements YAJsonIsolate { |
| 40 | + @override |
| 41 | + Future decode(String json) { |
| 42 | + return Future.value(jsonDecode(json)); |
| 43 | + } |
| 44 | +
|
| 45 | + @override |
| 46 | + Future<void> dispose() async {} |
| 47 | +
|
| 48 | + @override |
| 49 | + Future<String> encode(Object? json) { |
| 50 | + return Future.value(jsonEncode(json)); |
| 51 | + } |
| 52 | +
|
| 53 | + @override |
| 54 | + Future<void> initialize() async {} |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Usage |
| 59 | + |
| 60 | +Within your `lib/main` file, import the `supabase` package and create a new instance of the client: |
| 61 | + |
| 62 | +```dart |
| 63 | +import 'package:supabase_functions/supabase_functions.dart'; |
| 64 | +import 'package:edge_http_client/edge_http_client.dart'; |
| 65 | +import 'package:supabase/supabase.dart'; |
| 66 | +
|
| 67 | +void main() { |
| 68 | + final client = SupabaseClient( |
| 69 | + 'https://<your-project-id>.supabase.co', |
| 70 | + 'your-anon-key', |
| 71 | + httpClient: EdgeHttpClient(), |
| 72 | + isolate: EdgeIsolate(), |
| 73 | + ); |
| 74 | +
|
| 75 | + SupabaseFunctions(fetch: (request) async { |
| 76 | + List users = await client.from('users').select(); |
| 77 | + return Response.json(users); |
| 78 | + }); |
| 79 | +} |
| 80 | +``` |
0 commit comments