Skip to content

Commit

Permalink
eng(supabase): add example file
Browse files Browse the repository at this point in the history
  • Loading branch information
tshedor committed Aug 24, 2024
1 parent e511fc8 commit 3600f3e
Showing 1 changed file with 76 additions and 0 deletions.
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 3600f3e

Please sign in to comment.