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 8f80738
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
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

0 comments on commit 8f80738

Please sign in to comment.