spring_boot_starter_supabase

spring_boot_starter_supabase connects a Spring for Dart application to a Supabase project, on a server and in a Flutter app. The project's database is reached through Spring Data repositories, its Auth through Spring Security, Realtime gives streams, Storage is a template and declared buckets, Edge Functions are declared interfaces.

Why

A Supabase project is several services behind one address: a REST API over PostgreSQL, Auth, Realtime, Storage, Edge Functions. Each wants the project's key and the user's token on every call, and each has its own errors. The starter gives them the shape the rest of the application already has: repositories, @PreAuthorize, streams, beans with AOP. One bean, SupabaseCredentials, gives every call its key and its token, so all parts act as the same user, and what a user may read and write is decided by the database (row-level security).

The client is the starter's own, over the HttpTransport and the WebSocket starter: it uses none of Supabase's packages, and it compiles to WebAssembly.

Setup

# pubspec.yaml
dependencies:
  spring_boot_starter_supabase: ^0.9.0

dev_dependencies:
  build_runner: ^2.17.0
  spring_core_dart_generator: ^0.9.0   # required: writes the repositories and the declared interfaces

One import: package:spring_boot_starter_supabase/spring_boot_starter_supabase.dart re-exports the data, cache, Feign, WebSocket and logging starters, spring_security_core and spring_web_dart. The starter does not depend on the security starters:

Example

# lib/resources/application.yml
spring:
  supabase:
    url: https://abcd.supabase.co
    anon-key: ${SUPABASE_ANON_KEY}
@Table('todos')
class Todo {
  Todo({required this.title, this.done = false, this.id, this.createdAt});

  @Id()
  int? id;

  String title;
  bool done;
  DateTime? createdAt;
}

// A Spring Data repository: on the project's REST API, as the current user.
abstract interface class Todos implements ListCrudRepository<Todo, int> {
  Future<List<Todo>> findByDoneOrderByCreatedAtDesc(bool done);
}

@StorageBucket('avatars')
abstract interface class Avatars {
  @Upload('#{authentication.name}/avatar.png', upsert: true)
  Future<void> put(Uint8List bytes);
}

@EdgeFunctions()
abstract interface class Functions {
  @EdgeFunction('send-invite')
  Future<InviteResult> sendInvite(@RequestBody() Invite invite);
}

@Service()
class Account {
  Account(this.auth, this.todos);

  final SupabaseAuth auth;
  final Todos todos;

  Future<List<Todo>> open(String email, String password) async {
    await auth.signInWithPassword(email: email, password: password);
    // From here every call is made with the user's token.
    return todos.findByDoneOrderByCreatedAtDesc(false);
  }
}

The generator writes the beans of Todos, Avatars and Functions. The query is one request, GET /rest/v1/todos?done=eq.false&order=created_at.desc, with the anon key as apikey and the user's token as the bearer token.

What starts

Each bean steps back for a bean of its type declared by the application.

What is inside

Not there yet

Compared with Supabase's own packages

supabase_flutter Here
Supabase.initialize(url:, anonKey:) spring.supabase.url, spring.supabase.anon-key
supabase.from('todos').select().eq('done', false) a repository method, findByDone(false); there is no query builder
supabase.rpc('name', params: …) a repository method with @Procedure('name') (Data)
supabase.auth.signInWithPassword(…) SupabaseAuth.signInWithPassword(…); the user is Spring Security's Authentication
supabase.auth.onAuthStateChange authenticationChanges, AuthenticationSuccessEvent, LogoutSuccessEvent
supabase.storage.from('avatars').upload(…) StorageTemplate, or a @StorageBucket interface
supabase.functions.invoke('name') an @EdgeFunctions interface (a Feign client)
supabase.from('todos').stream(primaryKey: ['id']) a repository method watch…By, a Stream<List<Todo>> kept up to date
supabase.channel('room')…subscribe() SupabaseRealtime.channel('room') or a @RealtimeChannel interface: streams, joined while used
AuthException, PostgrestException Spring Security's AuthenticationExceptions, the data starter's DataAccessExceptions

In Spring (Java)

Spring has no Supabase support. The parts follow what Spring has for other services: Spring Data repositories, Spring Security's resource server and Authentication, S3Template of Spring Cloud AWS for Storage, Spring Cloud OpenFeign for the functions.

See also