Skip to content

Commit

Permalink
eng(supabase): add example file (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
tshedor authored Aug 24, 2024
1 parent 4eaa688 commit 8340f84
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
56 changes: 56 additions & 0 deletions packages/brick_offline_first_with_supabase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Migration> 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
Expand Down
76 changes: 76 additions & 0 deletions packages/brick_supabase/example/example.dart
Original file line number Diff line number Diff line change
@@ -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<User> {
@override
Future<User> fromSupabase(data, {required provider, repository}) async {
return User(
name: data['name'],
);
}

@override
Future<Map<String, dynamic>> 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<SupabaseModel> {
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<User>();
print(users);
}

0 comments on commit 8340f84

Please sign in to comment.