Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Commit

Permalink
refactor: use list of unique columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Jan 8, 2022
1 parent 7f04a97 commit 2ebe18a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ Future<void> main() async {
// stream
final streamSubscription = client
.from('countries')
.stream('id')
.stream(['id'])
.order('name')
.limit(10)
.execute()
.listen((snapshot) {
print('snapshot: $snapshot');
});
print('snapshot: $snapshot');
});

// remember to remove subscription
streamSubscription.cancel();
Expand Down
6 changes: 4 additions & 2 deletions lib/src/supabase_query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class SupabaseQueryBuilder extends PostgrestQueryBuilder {

/// Notifies of data at the queried table
///
/// [uniqueColumns] can be either the primary key or a combination of unique columns.
///
/// ```dart
/// supabase.from('chats').stream('my_primary_key').execute().listen(_onChatsReceived);
/// ```
Expand All @@ -50,11 +52,11 @@ class SupabaseQueryBuilder extends PostgrestQueryBuilder {
/// ```dart
/// supabase.from('chats:room_id=eq.123').stream('my_primary_key').order('created_at').limit(20).execute().listen(_onChatsReceived);
/// ```
SupabaseStreamBuilder stream(String primaryKey) {
SupabaseStreamBuilder stream(List<String> uniqueColumns) {
return SupabaseStreamBuilder(
this,
streamFilter: _streamFilter,
primaryKey: primaryKey,
uniqueColumns: uniqueColumns,
);
}
}
12 changes: 6 additions & 6 deletions lib/src/supabase_stream_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class SupabaseStreamBuilder {
late final StreamPostgrestFilter? _streamFilter;

/// Used to identify which row has changed
final String _primaryKey;
final List<String> _uniqueColumns;

SupabaseStreamBuilder(
SupabaseQueryBuilder queryBuilder, {
required StreamPostgrestFilter? streamFilter,
required String primaryKey,
required List<String> uniqueColumns,
}) : _queryBuilder = queryBuilder,
_streamFilter = streamFilter,
_primaryKey = primaryKey;
_uniqueColumns = uniqueColumns;

/// Which column to order by and whether it's ascending
_Order? _orderBy;
Expand All @@ -72,7 +72,7 @@ class SupabaseStreamBuilder {
/// Limits the result with the specified `count`.
///
/// ```dart
/// supabase.from('users').stream('id).limit(10);
/// supabase.from('users').stream('id').limit(10);
/// ```
SupabaseStreamBuilder limit(int count) {
_limit = count;
Expand Down Expand Up @@ -160,8 +160,8 @@ class SupabaseStreamBuilder {
} else if (payload.eventType == 'DELETE') {
targetRecord = payload.oldRecord!;
}

return record[_primaryKey] == targetRecord[_primaryKey];
return _uniqueColumns
.every((column) => record[column] == targetRecord[column]);
}

void _sortData() {
Expand Down
8 changes: 4 additions & 4 deletions test/mock_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void main() {
});

test('stream() emits data', () {
final stream = client.from('todos').stream('id').execute();
final stream = client.from('todos').stream(['id']).execute();
expect(
stream,
emitsInOrder([
Expand All @@ -154,7 +154,7 @@ void main() {
});

test('Can filter stream results with eq', () {
final stream = client.from('todos:status=eq.true').stream('id').execute();
final stream = client.from('todos:status=eq.true').stream(['id']).execute();
expect(
stream,
emitsInOrder([
Expand All @@ -170,7 +170,7 @@ void main() {
});

test('stream() with order', () {
final stream = client.from('todos').stream('id').order('id').execute();
final stream = client.from('todos').stream(['id']).order('id').execute();
expect(
stream,
emitsInOrder([
Expand All @@ -189,7 +189,7 @@ void main() {

test('stream() with limit', () {
final stream =
client.from('todos').stream('id').order('id').limit(2).execute();
client.from('todos').stream(['id']).order('id').limit(2).execute();
expect(
stream,
emitsInOrder([
Expand Down

0 comments on commit 2ebe18a

Please sign in to comment.