diff --git a/packages/brick_offline_first_with_supabase/README.md b/packages/brick_offline_first_with_supabase/README.md index 626ad5ce..788fe7fe 100644 --- a/packages/brick_offline_first_with_supabase/README.md +++ b/packages/brick_offline_first_with_supabase/README.md @@ -4,6 +4,62 @@ The `OfflineFirstWithSupabase` domain uses all the same configurations and annotations as `OfflineFirst`. +## Repository + +Adding offline support to Supabase is slightly more complicated than the average [../brick_offline_first_with_rest/README.md](repository process). Feedback is welcome on a smoother and more intuitive integration. + +````dart +class MyRepository extends OfflineFirstWithSupabaseRepository { + static late MyRepository? _singleton; + + MyRepository._({ + required super.supabaseProvider, + required super.sqliteProvider, + required super.migrations, + required super.offlineRequestQueue, + super.memoryCacheProvider, + }); + + factory MyRepository() => _singleton!; + + static void configure({ + required String supabaseUrl, + required String apiKey, + required Set migrations, + }) { + // Convenience method `.clientQueue` makes creating the queue and client easy. + final (client, queue) = OfflineFirstWithSupabaseRepository.clientQueue( + databaseFactory: databaseFactory, + ); + + final provider = SupabaseProvider( + // It's important to pass the offline client to your Supabase#Client instantiation. + // If you're using supabase_flutter, make sure you initialize with the clientQueue's client + // before passing it here. For example: + // ```dart + // await Supabase.initialize(httpClient: client) + // SupabaseProvider(Supabase.instance.client, modelDictionary: ...) + // ``` + SupabaseClient(supabaseUrl, apiKey, httpClient: client), + modelDictionary: supabaseModelDictionary, + ); + + // Finally, initialize the repository as normal. + _singleton = MyRepository._( + supabaseProvider: provider, + sqliteProvider: SqliteProvider( + 'my_repository.sqlite', + databaseFactory: databaseFactory, + modelDictionary: sqliteModelDictionary, + ), + migrations: migrations, + offlineRequestQueue: queue, + memoryCacheProvider: MemoryCacheProvider(), + ); + } +} +```` + ## Models ### ConnectOfflineFirstWithSupabase diff --git a/packages/brick_supabase/example/example.dart b/packages/brick_supabase/example/example.dart new file mode 100644 index 00000000..044a91ca --- /dev/null +++ b/packages/brick_supabase/example/example.dart @@ -0,0 +1,76 @@ +import 'package:brick_core/core.dart'; +import 'package:brick_supabase/brick_supabase.dart'; +import 'package:supabase/supabase.dart'; + +/// This class and code is always generated. +/// It is included here as an illustration. +/// Supabase adapters are generated by domains that utilize the brick_supabase_generators package, +/// such as brick_offline_first_with_supabase_build +class UserAdapter extends SupabaseAdapter { + @override + Future fromSupabase(data, {required provider, repository}) async { + return User( + name: data['name'], + ); + } + + @override + Future> toSupabase(instance, {required provider, repository}) async { + return { + 'name': instance.name, + }; + } + + @override + final defaultToNull = false; + + @override + final fieldsToSupabaseColumns = { + 'name': RuntimeSupabaseColumnDefinition(columnName: 'name'), + }; + + @override + final ignoreDuplicates = false; + + @override + final onConflict = null; + + @override + final tableName = 'users'; + + @override + final uniqueFields = {}; +} + +/// This value is always generated. +/// It is included here as an illustration. +/// Import it from `lib/brick/brick.g.dart` in your application. +final dictionary = SupabaseModelDictionary({ + User: UserAdapter(), +}); + +/// A model is unique to the end implementation (e.g. a Flutter app) +class User extends SupabaseModel { + final String name; + + User({ + required this.name, + }); +} + +class MyRepository extends SingleProviderRepository { + MyRepository(String apiUrl, String apiKey) + : super( + SupabaseProvider( + SupabaseClient(apiUrl, apiKey), + modelDictionary: dictionary, + ), + ); +} + +void main() async { + final repository = MyRepository('http://localhost:8080', 'YOUR_API_KEY_HERE'); + + final users = await repository.get(); + print(users); +}