spring_boot_starter_supabase

spring_boot_starter_supabase は、Spring for Dart のアプリケーションを Supabase のプロジェクトにつなぎます。サーバーでも Flutter アプリでも使えます。プロジェクトのデータベースには Spring Data のリポジトリで、Auth には Spring Security でアクセスし、Realtime はストリームを返し、Storage はテンプレートと宣言したバケット、Edge Functions は宣言したインターフェースです。

使う理由

Supabase のプロジェクトは、一つのアドレスの後ろにあるいくつかのサービスです。PostgreSQL の上の REST API、Auth、Realtime、Storage、Edge Functions です。どれも呼び出しのたびにプロジェクトのキーとユーザーのトークンを必要とし、それぞれ独自のエラーを返します。スターターはそれらに、アプリケーションの他の部分がすでに持っている形を与えます。リポジトリ、@PreAuthorize、ストリーム、AOP の効く Bean です。一つの Bean、SupabaseCredentials がすべての呼び出しにキーとトークンを渡すので、どの部分も同じユーザーとして動作します。ユーザーが何を読み書きできるかは、データベース (行レベルセキュリティ) が決めます。

クライアントはスターター自身のもので、HttpTransport と WebSocket スターターの上に作られています。Supabase のパッケージは一つも使わず、WebAssembly にコンパイルできます。

セットアップ

# 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

インポートは一つです。package:spring_boot_starter_supabase/spring_boot_starter_supabase.dart は、データ、キャッシュ、Feign、WebSocket、ロギングの各スターター、spring_security_core、spring_web_dart を再エクスポートします。このスターターはセキュリティのスターターには依存しません。

例

# 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);
  }
}

ジェネレーターが Todos、Avatars、Functions の Bean を書きます。このクエリは一つのリクエスト GET /rest/v1/todos?done=eq.false&order=created_at.desc で、anon キーが apikey、ユーザーのトークンが Bearer トークンになります。

起動するもの

どの Bean も、アプリケーションが同じ型の Bean を宣言していれば、そちらに譲ります。

内容

まだないもの

Supabase 自身のパッケージとの比較

supabase_flutter ここでは
Supabase.initialize(url:, anonKey:) spring.supabase.url、spring.supabase.anon-key
supabase.from('todos').select().eq('done', false) リポジトリのメソッド findByDone(false)。クエリビルダーはありません
supabase.rpc('name', params: …) @Procedure('name') の付いたリポジトリのメソッド (データ)
supabase.auth.signInWithPassword(…) SupabaseAuth.signInWithPassword(…)。ユーザーは Spring Security の Authentication です
supabase.auth.onAuthStateChange authenticationChanges、AuthenticationSuccessEvent、LogoutSuccessEvent
supabase.storage.from('avatars').upload(…) StorageTemplate、または @StorageBucket のインターフェース
supabase.functions.invoke('name') @EdgeFunctions のインターフェース (Feign クライアント)
supabase.from('todos').stream(primaryKey: ['id']) リポジトリのメソッド watch…By。最新に保たれる Stream<List<Todo>>
supabase.channel('room')…subscribe() SupabaseRealtime.channel('room') または @RealtimeChannel のインターフェース。ストリームで、使われている間だけ参加します
AuthException、PostgrestException Spring Security の AuthenticationException、データスターターの DataAccessException

Spring (Java) との対応

Spring には Supabase のサポートがありません。各部分は、Spring が他のサービスのために持っているものに従っています。Spring Data のリポジトリ、Spring Security のリソースサーバーと Authentication、Storage には Spring Cloud AWS の S3Template、関数には Spring Cloud OpenFeign です。

関連項目